| Author |
Topic |
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2012-01-03 : 23:16:33
|
| Hi, Here we exporting an CSV file by OPENROWSET and getting those results in a Temp table.Ex: Temp Table --> #TempCOLIn the above table columns will be in 'n' numbers, with some sample values like[Col1] [Col2] [Col3] .... [coln] A B C .... Z a b c .... zNeed the above results Vertically, like Column names in Rows and with their respective values like[COLUMN] [VALUE1] [VALUE2] Col1 A a Col2 B b Col3 C c . . . . . . . . . Coln Z zPlease help me in this to get the Solution. Its a major requirement for us.Regards,Kalaiselvan RLove Yourself First.... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-04 : 06:09:32
|
use UNPIVOTSELECT *FROM (SELECT ROW_NUMBER() OVER (ORDER BY Col1) AS Rn,* FROM Table) tUNPIVOT (Value FOR COlumn IN ([Col1], [Col2], [Col3], .... [coln]))u ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learntsql
524 Posts |
Posted - 2012-01-04 : 06:50:49
|
| Hi Visakhm,Is this possible to get the values dynamically for IN clause?TIA. |
 |
|
|
FischMan2
Yak Posting Veteran
59 Posts |
Posted - 2012-01-04 : 11:25:56
|
quote: Originally posted by learntsql Hi Visakhm,Is this possible to get the values dynamically for IN clause?TIA.
Two caveats:1) I have not tested the SQL below, so I can't guarantee no syntax errors.2) I do this a lot with PIVOT, but am not familiar with UNPIVOT. Assuming they work the same (the syntax looks the same), then this should work.OK, enough of that. Here's my response:Yes. You will need to use coalesce. As in: DECLARE @PivotColumnHeaders VARCHAR(MAX) DECLARE @PivotTableSQL NVARCHAR(MAX) SELECT @PivotColumnHeaders = COALESCE( @PivotColumnHeaders + ', [' + RTRIM(HeadersColName) + ']', '[' + RTRIM(HeadersColName) + ']' ) FROM ( SELECT DISTINCT HeadersColName FROM (TempTable TempTable -- Insert Joins and SQL here WHERE (MyCol = @Constraint) ) AS t SET @PivotTableSQL = N'SELECT *FROM (SELECT ROW_NUMBER() OVER (ORDER BY Col1) AS Rn,* FROM Table) tUNPIVOT (Value FOR Column IN (' + @PivotColumnHeaders + '))u'--PRINT(@PivotTableSQL) --Uncomment for editingEXECUTE(@PivotTableSQL)FischMan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
FischMan2
Yak Posting Veteran
59 Posts |
|
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2012-01-04 : 23:11:33
|
| All that you guys mentioning the column names as col1 and col2. But here I doesnt know the column names. It might be varied each and every time. Thats why I have specified that its n number of columns.All the above answers suits only If Column names is known.Regards,Kalaiselvan RLove Yourself First.... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
FischMan2
Yak Posting Veteran
59 Posts |
Posted - 2012-01-05 : 11:56:18
|
quote: Originally posted by Kalaiselvan All that you guys mentioning the column names as col1 and col2. But here I doesnt know the column names. It might be varied each and every time. Thats why I have specified that its n number of columns.All the above answers suits only If Column names is known.Regards,Kalaiselvan RLove Yourself First....
quote: but unless you know the values to be unpivoted before hand how can avoid dynamic sql?
The COALESCE statement generates the random number of columns, and generates the random column names. When my pivot queries are run, the resulting columns could be one or, literally, several hundred. The coalesce statement handles all of this nicely and neatly. Unless I am missing something here, it should work. The only place I use "col1" is in the "ORDER BY" clause of the ROW_NUMBER() function. You will need to provide something there to order by. What it is, is up to you. There should be some column you can use to order by. I suppose, worst case scenario, you could throw in your header columns. Or maybe create a new variable (@OrderByCols) and populate it based on a separate COALESCE query.If, by "dynamic SQL," you mean generating the SQL on the fly as the query is run, you'll note that I am generating dynamic SQL. Hands shaking. Too much coffee.FischMan |
 |
|
|
|