I have a table:ID FK Num Color1 A 5 Red2 A 2 Blue3 B 2 BlueI need a query that will give me the result set of:Number Red Number Blue Total5 4 9
3 thoughts on “Advanced T-SQL Query Needed?”
Leave a Reply to Serge M Cancel reply
You must be logged in to post a comment.
select (select count(*) from table where color=’Red’) Red, (Select count(*) from table where color=’Blue’) Blue, (Select count(*) from table where color=’Red’ or color=’Blue’) Total
select sum (a) Colorfrom tablegroup by Colorunionselect sum (a), ‘Total’from table
One-passage method:
select sum(case when color=’Red’
then Num else 0 end) [Number Red],
sum(case when color=’Blue’
then Num else 0 end) [Number Blue],
sum(Num) [Total]
from table_name