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.
| Author |
Topic |
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-06-16 : 13:59:25
|
| rollno section time score101 1A 2007 65101 1A 2008 80101 1A 200901 70101 1A 200902 90101 1A 200903 80101 1A 200904 90101 1A 200905 70102 2C 2007 65102 2C 2008 80102 2C 200901 70102 2C 200902 90102 2C 200903 80102 2C 200904 90102 2C 200905 70103 3A 2007 65103 3A 2008 80103 3A 200901 70103 3A 200902 90103 3A 200903 80103 3A 200904 90103 3A 200905 70105 3D 2007 65105 3D 2008 80105 3D 200901 70105 3D 200902 90105 3D 200903 80105 3D 200904 90105 3D 200905 70need a cross tab queryrollNo Section year-2 year-1 month1 month2 month3 month4 month5 month6 month7 month8 month9 month10 month11 month12 101 1A 65 80 70 90 80 90 70 0 0 0 0 0 0i tried like this select(case when c= datepart(year, DATEADD(year,-2,GETDATE())) then d else null )as YearN2,(case when c= datepart(year, DATEADD(year,-1,GETDATE())) then d else null )as YearN1,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '01' then d else null ) as month1,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '02' then d else null ) as month2,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '03' then d else null ) as month3,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '04' then d else null ) as month4,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '05' then d else null ) as month5,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '06' then d else null ) as month6,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '07' then d else null ) as month7,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '08' then d else null ) as month8,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '09' then d else null ) as month9,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '10' then d else null ) as month10,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '11' then d else null ) as month11,(case when c= CAST(YEAR(getdate()) AS VARCHAR(4)) + '12' then d else null ) as month12from (-- i ve the result set here as the above example)Chandragupta Mourya |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-16 : 14:07:42
|
| will the time values be static (same) always? |
 |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-06-16 : 14:10:13
|
| did not understand your question. The case statement i used to filter the last 2 years , and all months of this year.Chandragupta Mourya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-16 : 14:12:54
|
| but wont the year/month values vary ? will it be always current year month & previous two years? |
 |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-06-16 : 14:14:23
|
| yes .Chandragupta Mourya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-16 : 14:23:05
|
| [code]SELECT rollno,section,SUM(CASE WHEN time=YEAR(GETDATE())-2 THEN score ELSE 0 END) AS year1,SUM(CASE WHEN time=YEAR(GETDATE())-1 THEN score ELSE 0 END) AS year2,SUM(CASE WHEN time=DATENAME(yyyy,GETDATE())+ '01' THEN score ELSE 0 END) AS month1,SUM(CASE WHEN time=DATENAME(yyyy,GETDATE())+ '02' THEN score ELSE 0 END) AS month2,SUM(CASE WHEN time=DATENAME(yyyy,GETDATE())+ '03' THEN score ELSE 0 END) AS month3,...SUM(CASE WHEN time=DATENAME(yyyy,GETDATE())+ '12' THEN score ELSE 0 END) AS month12 FROM(SELECT ROW_NUMBER() OVER (PARTITION BY rollno,section ORDER BY time) AS Seq,* FROM YourTable)tGROUP BY rollno,section[/code] |
 |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-06-16 : 17:23:34
|
| . |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-18 : 02:42:16
|
quote: Originally posted by konark .
What did you want to say?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|