Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Grouping help
Message
From
15/11/2007 09:42:16
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Miscellaneous
Thread ID:
01269131
Message ID:
01269142
Views:
9
Sergey,
Thanks for the sample. I needed to count the number of customer instead of summing the sales, so I tried to modify your snipped like below:
...
COUNT( CASE WHEN status = 1 THEN status END ) AS Status1Count,
COUNT( CASE WHEN status = 2 THEN status END ) AS Status2Count,
...
... and it seemed to work properly. Thanks for the help. Have you ever thought about going into teaching?

>Mike,
>
>You can use CASE function to implement conditional summing
>
>  ...
>  SUM( CASE WHEN status = 1 THEN Sales ELSE 0 END ) AS Sales1,
>  SUM( CASE WHEN status = 2 THEN Sales ELSE 0 END ) AS Sales2,
>  ...
>
>>Can I do a conditional COUNT in a query? For example, I have Suppliers that have Customers that have Transactions. For each Supplier I am summing the Amount of each Transaction to show total sales for a Supplier. Pretty basic stuff, and am getting a result set like so:
>>
>>Supplier   Sales
>>--------   -----
>>1          1000.00
>>2          750.00
>>3          12000.00
>>4          100.00
>>5          5.00
>>
>>
>>However, the Customer also has a Status - for simplicity we'll say a Status of 1 or 2. I want to show the same result set as above, but I want to add two columns - one for the count of Customers with a Status of 1, and one for the count of Customers with a Status of 2.
>>
>>It almost feels like I need to do two levels of grouping. It would be easier if I could add the Status to the existing grouping, but that's not what the user wants.
>>
>>Any advice?
Very fitting: http://xkcd.com/386/
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform