Help in query [please]

  • Hai I am new in this forum.I have a problem.I will discribe it with my query."select sum(total) from tablename group by year". Here the results will be shown in rows ie sum of total for each year.But I want the result to be printed in columns.ie only one record should be returned.Each year total can be seperated by "," or some thing like that.The ultimate goal is that the query should return only one row and it should contain all the totals.

    Is there is any body who knows  this plz help me

    Thanks in Advance

  • You could use something like this.

    SELECT

    SUM(CASE WHEN DATEPART(YY, SaleDate) = 2003 THEN TOTAL ELSE 0 END) AS [2003],

    SUM(CASE WHEN DATEPART(YY, SaleDate) = 2004 THEN TOTAL ELSE 0 END) AS [2004],

    SUM(CASE WHEN DATEPART(YY, SaleDate) = 2005 THEN TOTAL ELSE 0 END) AS [2005]

    FROM SalesTable

  • This query is good and working fine.Lots of thanks for U.Looking forward for more bye for now

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

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