Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Two Different Totals

Author  Topic 

armesjr84
Starting Member

1 Post

Posted - 2013-05-06 : 17:08:37
I have a table that has Procedure, Hospital, City_State, and a Volume_Flag (Volume >5 = '1', volume <6 = '0') column. I am having issues writing a query that will provide me with the volumes when the volume flag = 1 or when the volume flag = 0. Thanks in advance for any help.



Table:
Procedure Hospital City_State Volume Volume_Flag
A Hospital A Richmond, VA 5 1
A Hospital B Richmond, VA 7 1
A Hospital C Midlothian, Va 5 0
B Hospital A Richmond, VA 34 1
B Hospital B Richmond, VA 27 1
B Hospital C Midlothian, Va 5 0
C Hospital A Richmond, VA 2 1
C Hospital B Richmond, VA 9 1
C Hospital C Midlothian, Va 25 1

Desired Results:

Procedure City, State Total Vol VF =1 Total Vol VF = 0
Procedure A Richmond, VA 7 5
Procedure A Midlothian, VA 0 5
Procedure B Richmond, VA 61 0
Procedure B Midlothian, VA 0 5
Procedure C Richmond, VA 9 2
Procedure C Midlothian, VA 25 0


James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-06 : 17:48:23
[code]SELECT
[Procedure],
City,
STATE,
SUM(CASE WHEN Volume_Flag = 1 THEN Volume ELSE 0 END) AS [Total Vol VF = 1],
SUM(CASE WHEN Volume_Flag = 0 THEN Volume ELSE 0 END) AS [Total Vol VF = 0]
FROM
TABLE
GROUP BY
[Procedure],
City,
STATE[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-07 : 00:32:15
sorry your sample data doesnt make much sense. Can you expplain these lines?


Procedure A Richmond, VA 7 5

Procedure C Richmond, VA 9 2


both the records for these two procedures have Volume_Flagn as 1 so didnt understand how you will show one under 1 and other under 0 for VF

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -