SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


QUERY HELP


QUERY HELP

Author
Message
davdam8
davdam8
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 668
Hi geniuses!

I have this query

SELECT DISTINCT chapter,  Sum(dbo.FGG.Value) AS FGG2010
FROM FGG
WHERE (FGG.YEAR= 2010) and (dbo.FGG.valueType = '02 - FGG01l')
Group by chapter



It retrieves :

chapter | FGG2010
A | 1$
B | 2$
C | 3$


What I want is to add in 'Sum(dbo.FGG.Value) AS FGG2011' where FGG.YEAR=2011 in the same query, in order to retrive:

chapter | FGG2010 | FGG2011
A | 1$ | 2$
B | 2$ | 33$
C | 3$ | 11$


Thanks in advance!
Regards
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14279 Visits: 15974
Use a CASE expression in your SUM statement, something like this:

...
SUM(CASE WHEN Year = 2010 THEN Amount ELSE 0 END) AS Total2010,
SUM(CASE WHEN Year = 2011 THEN Amount ELSE 0 END) AS Total2011
...


Alternatively, you could use PIVOT. Or you could write a report in Reporting Services to display your data in the way you need.

John
davdam8
davdam8
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 668
THANKS MEN!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search