[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


SQL Statement using Case

Posted on 2005-05-16
Medium Priority
Last Modified: 2010-03-19
I have a stored procedure that returns 12 rows each time I run it.  The database was designed that it is storing a record for each Month in rows for example:

Table:  MonthAmounts
MonthID,    MonthAmt
1               5.00
2               10.00
3               17.50
4                 3.50

I want to get everything in one row if possible.  I will always be pulling 12 rows.  Do I have to write a cursor to handle this or can I get it all into 1 record across for all months?  Thanks
Question by:sbornstein2
1 Comment
LVL 28

Accepted Solution

rafrancisco earned 1000 total points
ID: 14011708
Try this:

            SUM(CASE WHEN MonthID = 2 THEN MonthAmt ELSE 0 END) AS Month2,
            SUM(CASE WHEN MonthID = 3 THEN MonthAmt ELSE 0 END) AS Month3,
            SUM(CASE WHEN MonthID = 4 THEN MonthAmt ELSE 0 END) AS Month4,
            SUM(CASE WHEN MonthID = 5 THEN MonthAmt ELSE 0 END) AS Month5,
            SUM(CASE WHEN MonthID = 6 THEN MonthAmt ELSE 0 END) AS Month6,
            SUM(CASE WHEN MonthID = 7 THEN MonthAmt ELSE 0 END) AS Month7,
            SUM(CASE WHEN MonthID = 8 THEN MonthAmt ELSE 0 END) AS Month8,
            SUM(CASE WHEN MonthID = 9 THEN MonthAmt ELSE 0 END) AS Month9,
            SUM(CASE WHEN MonthID = 10 THEN MonthAmt ELSE 0 END) AS Month10,
            SUM(CASE WHEN MonthID = 11 THEN MonthAmt ELSE 0 END) AS Month11,
            SUM(CASE WHEN MonthID = 12 THEN MonthAmt ELSE 0 END) AS Month12,
FROM MonthAmounts

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question