| Author |
Topic |
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-18 : 21:53:44
|
| Hi all I am new to SQL SERVER.Please help me how to write a cursor/ procedure to convert rows into columns. I am using SQL SERVER 2000.i am having 3 tables namely emp,dept,joiningdate.emp table contains columns with valuesempid, empname1 Smith2 john,3 david4 Ann. . . . .20 stephendept table contains columns with values likeempid, deptid, dept name,value 1 100 Prod 12 101 dev 13 102 support 14 103 sales 1. . . . ' . .20 120 security 1 and year table containsdeptid, year100 1900101 1910102 1920103 1930.. 120 1950 Now using these 3 tables i want to write a querry to get the result like this. empname 1900 1910 1920 1930.....................1950Smith 1 0 0 0....................... 0john 0 1 0 0....................... 0 david 0 0 1 0....................... 0 Ann 0 0 0 1........................0 Please help me out. Its a urgent requirement.Thanks & regardsThejesh KumarApplication Developer |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-19 : 00:52:38
|
| If you are new to SQL Server how could your seniors just hand you such a requirement?Even if we give you a solution it's of no use for you to understand the logic behind the code.Piece of advice :-"DONT BE A COPY PASTE MASTER."PBUH |
 |
|
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-19 : 02:00:25
|
| Please help me out.Thejesh KumarApplication Developer |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-19 : 02:31:04
|
| Ok.Can you post the query where it brings all the related records from the three table?PBUH |
 |
|
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-19 : 05:12:11
|
| here is the querrySELECT E.EMPNAME,D.VALUE,Y.JOININGYEAR FROM EMP ELEFT JOIN (SELECT * FROM DEPT) DON E.EMPID=D.EMPIDINNER JOIN YEARS YON D.DEPTID=Y.DEPTIDbut i want the result as below. how to do thisempname 1900 1910 1920 1930.....................1950Smith 1 0 0 0....................... 0john 0 1 0 0....................... 0david 0 0 1 0....................... 0Ann 0 0 0 1........................0Thejesh KumarApplication Developer |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-19 : 05:19:30
|
| Are the years fixed?Will the minimum year always be 1900 & max 1950?PBUH |
 |
|
|
kishore_pen
Starting Member
49 Posts |
|
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-19 : 05:47:58
|
| No years are not fixed. I have just shown upto 1950 but Max value will be greater than 1950. Thejesh KumarApplication Developer |
 |
|
|
Sachin.Nand
2937 Posts |
|
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-19 : 08:26:17
|
| Thanks a lot for the link. i did it using the crosstab procedure.Thejesh KumarApplication Developer |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-19 : 08:29:42
|
quote: Originally posted by Theju Thanks a lot for the link. i did it using the crosstab procedure.Thejesh KumarApplication Developer
Welcome.However you could had easily acheieved it had you used any reporting tool.PBUH |
 |
|
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-19 : 09:05:12
|
| NoThejesh KumarApplication Developer |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-19 : 09:10:29
|
quote: Originally posted by Theju NoThejesh KumarApplication Developer
Why????PBUH |
 |
|
|
Theju
Starting Member
7 Posts |
Posted - 2010-10-20 : 08:44:38
|
| we are not using any reporting tools. v just export it to excel.Thejesh KumarApplication Developer |
 |
|
|
Piyush
Starting Member
8 Posts |
Posted - 2011-07-25 : 01:59:54
|
| I have put a complete article, in which you will find 3 methods using which the rows can be transformed to column attributes:unspammedSo i will suggest you to go through this carefully!!Piyush Bajaj |
 |
|
|
|