QUERY HELP

  • 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

  • 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

  • THANKS MEN!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply