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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 transpose of data

Author  Topic 

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 03:41:45
Hi all,

I have a table1 & table2 as below, I want to transpose data from table1 to table two as below, could you plz help me in writing a stored proc to carry out this.

table1





unique_id tab_nm field1 field2 field3 fvalue1 fvalue2 fvalue3
---------------------------------------------------------------------
1 tab1 eno ename seq 121 aak 1
2 tab1 eno ename 122 shb
3 tab1 acode INTO
4 tab2 eno ename seq 123 sob 1

like wise many records





[/hr]







Table 2

ch_id rec_id rec_no field_nm new_val eno tab_name
----------------------------------------------
1 1 1 eno 121 121 tab1
2 1 2 ename aak 121 tab1
3 1 3 seq 1 121 tab1

4 2 1 eno 122 122 tab1
5 2 2 ename shb 122 tab1

6 3 1 acode INTO tab1


7 1 1 eno 123 123 tab2
8 1 2 ename sob 123 tab2
9 1 3 seq 1 123 tab2



[/hr]



aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 05:16:36
If any one of you helps me with the logic, that wud be great help.




quote:
Originally posted by aakcse

Hi all,

I have a table1 & table2 as below, I want to transpose data from table1 to table two as below, could you plz help me in writing a stored proc to carry out this.

table1





unique_id tab_nm field1 field2 field3 fvalue1 fvalue2 fvalue3
---------------------------------------------------------------------
1 tab1 eno ename seq 121 aak 1
2 tab1 eno ename 122 shb
3 tab1 acode INTO
4 tab2 eno ename seq 123 sob 1

like wise many records





[/hr]







Table 2

ch_id rec_id rec_no field_nm new_val eno tab_name
----------------------------------------------
1 1 1 eno 121 121 tab1
2 1 2 ename aak 121 tab1
3 1 3 seq 1 121 tab1

4 2 1 eno 122 122 tab1
5 2 2 ename shb 122 tab1

6 3 1 acode INTO tab1


7 1 1 eno 123 123 tab2
8 1 2 ename sob 123 tab2
9 1 3 seq 1 123 tab2



[/hr]





Go to Top of Page

ra.shinde
Posting Yak Master

103 Posts

Posted - 2009-03-23 : 05:47:00
SELECT rank() over (order by rec_id,rec_no),*
FROM
(
SELECT unique_id as rec_id, 1 as rec_no, field1 as field_nm,fvalue1 as new_val,
CASE WHEN field1 = 'eno' THEN fvalue1 else '' END as ENO,tab_nm as tab_name
FROM @TAB1
union
SELECT unique_id as rec_id,2 as rec_no, field2 as field_nm,fvalue2 as new_val,
CASE WHEN field1 = 'eno' THEN fvalue1 else '' END as ENO,tab_nm as tab_name
FROM @TAB1 where field2 <> ''
union
SELECT unique_id as rec_id,3 as rec_no, field3 as field_nm,fvalue3 as new_val,
CASE WHEN field1 = 'eno' THEN fvalue1 else '' END as ENO,tab_nm as tab_name
FROM @TAB1 where field3 <> ''

)a



Rahul Shinde
Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 06:43:03
Rahul Sir,

will this work, if I want to add some more column from table1 to table2

Regards,
aak


quote:
Originally posted by ra.shinde

SELECT rank() over (order by rec_id,rec_no),*
FROM
(
SELECT unique_id as rec_id, 1 as rec_no, field1 as field_nm,fvalue1 as new_val,
CASE WHEN field1 = 'eno' THEN fvalue1 else '' END as ENO,tab_nm as tab_name
FROM @TAB1
union
SELECT unique_id as rec_id,2 as rec_no, field2 as field_nm,fvalue2 as new_val,
CASE WHEN field1 = 'eno' THEN fvalue1 else '' END as ENO,tab_nm as tab_name
FROM @TAB1 where field2 <> ''
union
SELECT unique_id as rec_id,3 as rec_no, field3 as field_nm,fvalue3 as new_val,
CASE WHEN field1 = 'eno' THEN fvalue1 else '' END as ENO,tab_nm as tab_name
FROM @TAB1 where field3 <> ''

)a



Rahul Shinde

Go to Top of Page

ra.shinde
Posting Yak Master

103 Posts

Posted - 2009-03-23 : 07:00:18
it will work but You will need to do some modifications in it

Rahul Shinde
Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 07:18:10
Rahul Sir,
(Rahul one of my child hood close friends(chaddi dost) name he is from mumbai vashi sry for being personal)

So far it looks to me, like working, I have added 5filds and correspounding values to it also.

few questions.

1. Is the case stm remains same for all union
2. Is the where condition not required for first select
3. I have field(1 to 5) and fvalue(1 to 5) both these col
are from 1 to 5,hence 5 select stm, with correspounding
where clause field2<>''...field5<>''(is this not req for field1 above 2nd question also same)
4. apart from above 5 col, rest of the col remains nothing much
just taking values from them to insert into table2

Regards,
aak




quote:
Originally posted by ra.shinde

it will work but You will need to do some modifications in it

Rahul Shinde

Go to Top of Page

ra.shinde
Posting Yak Master

103 Posts

Posted - 2009-03-23 : 07:40:10
While writing the query, I assumed few things;
1. First field will never be empty.
If it can be empty, add the where condition to first select also.
2. I am expecting eno to apper in first field only.
If it can appear in other fields also, Please modify the case statement acco to add more cases for other fields. But it will remain same for all statements.

Rahul Shinde
Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 10:07:22
Hi Rahul,

Shall I need to add case stm for other columns also?

yes first field will never be empty, it can contain
four distinct values. eno,dcode,acode,alcode.
and these are appearing in other field(1-5)also & their correspounding values can be empty(fvalue 1-5).

but in field2 we have ename and we also have ename
inother field also.

Also other field(2-5) may or may not have values in it.

Regards,
aak.








quote:
Originally posted by ra.shinde

While writing the quey, I assumed few things;
1. First field will never be empty.
If it can be empty, add the where condition to first select also.
2. I am expecting eno to apper in first field only.
If it can appear in other fields also, Please modify the case statement acco to add more cases for other fields. But it will remain same for all statements.

Rahul Shinde

Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 10:45:06
Hi Rahul,

can you plz elaborate on this plz.

2. I am expecting eno to apper in first field only.
If it can appear in other fields also, Please modify the case statement acco to add more cases for other fields. But it will remain same for all statements.


As the eno number appears in other fields also, what modification
is needed in case statement for other fields..

unique_id in tab1 and ch_id in tab2 will be identity field.
so need not insert into them

regards.

Go to Top of Page

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-03-23 : 16:05:42
for first row of table1, there will be 3 correspounding rows in table2,

for second row of table1 there will be 2 correspounding rows in table2

for third row of table1 there will be 1 correspounding row in table2

for fourth row of table1 there will be 3 correspounding rows in table2


(tab1 tab2 are different from table1 and table2)


Regards,
aak


Go to Top of Page
   

- Advertisement -