Group by and Sum Problems

  • I have the following piece of SQL code which runs without any problems:

    SELECT year, Q1Amt + Q2Amt + Q3Amt + Q4Amt AS Actual

    FROM Table1

    WHERE (buildingID = @buildingID)

    ORDER BY year

    I get around 400 records in my result set.

    What I now need to do is group by Year & Actual, so I only end up with one figure for each year.

    As soon as I add something like the following, everything comes unstuck.

    GROUP BY finyear, SUM(Q1Amt + Q2Amt + Q3Amt + Q4Amt)

  • Kerrie Jones (9/22/2008)


    I have the following piece of SQL code which runs without any problems:

    SELECT year, Q1Amt + Q2Amt + Q3Amt + Q4Amt AS Actual

    FROM Table1

    WHERE (buildingID = @buildingID)

    ORDER BY year

    I get around 400 records in my result set.

    What I now need to do is group by Year & Actual, so I only end up with one figure for each year.

    As soon as I add something like the following, everything comes unstuck.

    GROUP BY finyear, SUM(Q1Amt + Q2Amt + Q3Amt + Q4Amt)

    See if this is what you wanted

    SELECT year, SUM(Q1Amt + Q2Amt + Q3Amt + Q4Amt) AS Actual

    FROM Table1

    WHERE (buildingID = @buildingID)

    GROUP BY year

    ORDER BY year

    or post some sample data with expected result


    Madhivanan

    Failing to plan is Planning to fail

  • That works like a bought one. Thankyou for the quick response.:D

  • Kerrie Jones (9/22/2008)


    That works like a bought one. Thankyou for the quick response.:D

    You are welcome 🙂


    Madhivanan

    Failing to plan is Planning to fail

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

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