Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select group by and .......

Author  Topic 

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2010-09-27 : 06:33:02
hi Admin and all talent,

I had a table as below:

Id1 Year Jan Feb March .... Sept Oct Nov Dec Remark1
xxx 2010 100 200 300 ...... 400 500 600 700 old Remark
xxx 2011 150 150 0 0,0,0,0.. 0 0 0 0 new Remark


Data that i want is as below:
Id1 Jan Feb March .... Sept Oct Nov Dec Remark1
xxx 150 150 0 ............ 0 500 600 700 new Remark

My Condition is:
1)Now year=2010, Month=9
2)i want sum data which above this month.(year=2010 and month=Oct,Nov and Dec) and (all the month data on the following year=2011) and the remark use the latest one.

Can anyone showing the select statement? thank you.

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2010-09-27 : 23:29:28
I found the solution for the query but i can't get the updated Remark1.
My query without remark1 as below.

SELECT Id1,
SUM(CASE WHEN CAST(YEAR AS int) > 2010 THEN JAN WHEN CAST(YEAR AS int) >= 2010 AND 1 > datepart(month, getdate()) THEN JAN ELSE 0 END) AS JAN_data
SUM(CASE WHEN CAST(YEAR AS int) > 2010 THEN FEB WHEN CAST(YEAR AS int) >= 2010 AND 2 > datepart(month, getdate()) THEN FEB ELSE 0 END) AS Feb_data
:
:
:
FROM table
GROUP BY id1


How do i using the updated remark to replace the previous remark
?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-30 : 12:38:42
[code]
...
MAX(CASE WHEN CAST(YEAR AS int) > 2010 THEN Remark1 ELSE NULL END) AS Remark1
...
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -