| Author |
Topic |
|
murthybsnvsn
Starting Member
3 Posts |
Posted - 2007-05-01 : 09:34:42
|
| Sql Command to Convert Row of Data Into Column Assume that you are having the data like this SalesMan Year SaleAmount ================================== Carole 2001 1,500 Carole 2002 2,500 Carole 2003 3,500 Carole 2004 4,500 Angle 2001 5,000 Angle 2002 7,500 Angle 2003 10,500 Angle 2004 12,750 Now You Want to Convert the Above Data Like Below :- ================================================= SalesMan Sal2001 Sal2002 Sal2003 Sal2004 ========================================================= Carol 1,500 2,500 3,500 4,500 Angle 5,000 7,500 10,500 12,750please help me in this regardThanks and Regards,Murthy. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-01 : 10:15:49
|
[code]select SalesMan, Sal2001 = sum(case when Year = 2001 then SalesAmount else 0 end), Sal2002 = sum(case when Year = 2002 then SalesAmount else 0 end), Sal2003 = sum(case when Year = 2003 then SalesAmount else 0 end), Sal2004 = sum(case when Year = 2004 then SalesAmount else 0 end)from tablegroup by SalesMan[/code] KH |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-01 : 17:12:02
|
SQL SERVER 2005 StyleDeclare @table Table ( Salesman varchar(100), Years int, SaleAmount int)Insert @table Select 'Carole',2001,1500Union allSelect 'Carole',2002,2500Union allSelect 'Carole',2003,3500Union allSelect 'Carole',2004,4500Union allSelect 'Angle',2001,5000Union allSelect 'Angle',2002,7500Union allSelect 'Angle',2003,10500Union allSelect 'Angle',2004,12750SELECT * FROM ( SELECT SalesMan,SaleAmount,Years FROM @Table ) o PIVOT ( Sum(SaleAmount) FOR Years IN ( [2001],[2002],[2003],[2004] )) p Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-01 : 18:10:41
|
| Look at the two execution plans.One is more efficient than the other.Peter LarssonHelsingborg, SwedenEDIT: Never mind. There is no difference on this small set of data. |
 |
|
|
murthybsnvsn
Starting Member
3 Posts |
Posted - 2007-05-02 : 01:33:49
|
| hi peter,Greetings and Very Good Morning,thank you very much,thanks for your valuable timeThanks and RegardsMurthy. |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-02 : 01:37:09
|
I am amazed no one ever pointed out one of the most popular answers of all time for such type of questions:DO IT IN THE FRONT-END !! Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-02 : 01:48:04
|
quote: Originally posted by harsh_athalye I am amazed no one ever pointed out one of the most popular answers of all time for such type of questions:DO IT IN THE FRONT-END !! Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
SO YOU DID IT Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 01:48:50
|
| We are waiting for Madhi.Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-02 : 01:50:51
|
| Hi Chirag,Nice to see you back in action on SQLTeam.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-02 : 02:14:11
|
| Thanks..!!!Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
|