all groups > sql server programming > august 2007 >
You're in the

sql server programming

group:

table transformation hint


Re: table transformation hint Roy Harvey
8/16/2007 8:30:06 PM
sql server programming: INSERT commands are very hard to work with without the corresponding
CREATE TABLE commands.

Roy Harvey
Beacon Falls, CT


[quoted text, click to view]
table transformation hint GB
8/16/2007 11:28:44 PM
Hello,
I have the table:

INSERT INTABLE(DATADATE,K1,K2,K3,IND) VALUES(convert(datetime,'2000-01-01
00:00:00.000',121),'33','21','45','A ')
INSERT INTABLE(DATADATE,K1,K2,K3,IND) VALUES(convert(datetime,'2000-02-01
00:00:00.000',121),'28','28','41','A ')
INSERT INTABLE(DATADATE,K1,K2,K3,IND) VALUES(convert(datetime,'2000-01-01
00:00:00.000',121),'11','35','50','B ')
INSERT INTABLE(DATADATE,K1,K2,K3,IND) VALUES(convert(datetime,'2000-02-01
00:00:00.000',121),'21','38','43','B ')

I need a query to transform the table to the following:

INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-01-01
00:00:00.000',121),'K1 ','33','A ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-02-01
00:00:00.000',121),'K1 ','28','A ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-01-01
00:00:00.000',121),'K2 ','21','A ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-02-01
00:00:00.000',121),'K2 ','28','A ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-01-01
00:00:00.000',121),'K1 ','11','B ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-02-01
00:00:00.000',121),'K1 ','21','B ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-01-01
00:00:00.000',121),'K2 ','35','B ')
INSERT OUTTABLE(DATADATE,K,VAL,IND) VALUES(convert(datetime,'2000-02-01
00:00:00.000',121),'K2 ','38','B ')

Thanks,
GB

RE: table transformation hint Bob
8/17/2007 2:10:01 AM
In SQL 2005 you can UNPIVOT:

SELECT *
FROM INTABLE
UNPIVOT( K FOR xval In ( K1, K2, K3 ) ) upvt

I get extra records than in your outtable - don't know if you missed them out?

Let me know how you get on.

wBob

[quoted text, click to view]
Re: table transformation hint GB
8/17/2007 3:52:22 PM
Thank you Bob, it works fine.

GB


[quoted text, click to view]

AddThis Social Bookmark Button