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 |
rocknpop
Posting Yak Master
201 Posts |
Posted - 2009-09-03 : 12:20:19
|
HiI will first post the sample data as verbally it is a bit diffcult for me to explain the resultset I want. Here is the sample data:CREATE TABLE TESTING(id int IDENTITY(1,1), [1] VARCHAR(50) NULL,[2] VARCHAR(50) NULL,[3] VARCHAR(50) NULL,[4] VARCHAR(50) NULL)INSERT INTO TESTING SELECT NULL,'A2','S3','S4'UNIONSELECT 'A','S2','S3','S4'UNIONSELECT 'B','X2','X3','X4'UNIONSELECT 'C','Y2','Y3','Y4'UNIONSELECT 'D','Z2','Z3','Z4'SELECT * FROM TESTING This is the table data as created above:ID 1 2 3 41 null A2 B3 C42 A S2 S3 S43 B X2 null null4 C Y2 Y3 Y45 D Z2 Z3 nullAnd this should be the final output:COL1 COL2 COL3 COL4A2 S2 X2 BA2 S2 Y2 CA2 S2 Z2 DB3 S3 Y3 CB3 S3 Z3 DC4 S4 Y4 CAnd now the problem I am facing. This table has numbered columns 1,2,3...and so on and number of columns is not fixed, it could increase or decrease any time. This table is the result of a DTS, pulled from a file. The DTS cannot be changed and the DB is Sql 2k.This is one query I could think of:SELECT (SELECT TOP 1 COL1 FROM T1 WHERE ID<=2) AS A,(SELECT TOP 1 COL2 FROM T1 WHERE ID<=2 ORDER BY ID DESC) AS B,COL1, COL2 FROM T1 WHERE COL2 IS NOT NULLAND ID>2UNIONSELECT (SELECT TOP 1 COL3 FROM T1 WHERE ID<=2) AS A,(SELECT TOP 1 COL3 FROM T1 WHERE ID<=2 ORDER BY ID DESC) AS B,COL1, COL3 FROM T1 WHERE COL3 IS NOT NULLAND ID>2.......and so onIn the above I have assumed COL2,COL3 to be as column names 1,2 in the table. So even if I know the column names maybe I could build this query looping the information_schema.columns table but then again the problem is it will be dynamic as I would not know the number of columns. Thus there are 2 problems, one the column name and second the no. of those columns.If possible I do not want any loops (would be great if avoidable) but more importantly I do not want any dynamic code. Can anyone please offer a solution to this problem, without any dynamic query. Do you think it is possible.Thanks--------------------Rock n Roll with SQL |
|
hadi teo
Starting Member
4 Posts |
Posted - 2009-09-16 : 18:54:07
|
Hi,I am not sure whether this approach is the correct one. Alternatively, you can use the "temporary table" concept. Google for the keyword "temporary table sql server". By using the temporary table, you can build the columns definition dynamically. I am not sure about the performance impact. |
|
|
rocknpop
Posting Yak Master
201 Posts |
Posted - 2009-09-17 : 13:42:09
|
Thanks for the reply. Yes I know I can do this by temp tables but then the query would become dynamic. Given this problem, is there any way to avoid dynamic sql.--------------------Rock n Roll with SQL |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2009-09-17 : 13:44:07
|
quote: Originally posted by rocknpop Thanks for the reply. Yes I know I can do this by temp tables but then the query would become dynamic. Given this problem, is there any way to avoid dynamic sql.
Given the earlier statement:quote: This table has numbered columns 1,2,3...and so on and number of columns is not fixed, it could increase or decrease any time.
I'd say no. |
|
|
rocknpop
Posting Yak Master
201 Posts |
Posted - 2009-09-17 : 14:05:23
|
Thanks robvolk--------------------Rock n Roll with SQL |
|
|
|
|
|
|
|