Hi everyone,
Overall problem:
I currently have a nice sproc that returns the "right" data but in the "wrong" format. For example,
Current format -
Desired format -
Points to note:
There is no way to determine the number of Mnemonic's (ie. the number of cols that will need to be returned) until the initial SELECT statement has been run (ie. to determine which Mnemonic's are in the set).
The query that returns this data set is rather complicated already - merging results off various servers and dbs. Hence, the approach (in my sproc) I am trying to get work atm (which won't be the most efficient, but is a nice starting place) is:
****
****
In other words, have the right _data_ in a temp table, and then figure out how to transform it into the right _format_.
Problem is that I have no idea how to do this. I've been reading all the threads I have been able to find. I don't believe a Pivot Table is the answer. Perhaps a cursor?
The first step I have been trying with the above code is simply to create the table with the right column headings. However, not even -
****
****
Will work. I am guessing that that is because the temp table will only exist for the duration of the execute statement?
Any ideas, links, solutions would be much appreciated. I am very much stuck on this one!
Much TIA
Overall problem:
I currently have a nice sproc that returns the "right" data but in the "wrong" format. For example,
Current format -
Code:
AsAtDate Mnemonic Amount
-------- -------- ------
Date1 M1 A1
Date1 M2 A2
Date1 M3 A3
Date2 M1 A4
Date2 M3 A5
Date2 M4 A6
Date3 M1 A7
Date3 M2 A8
Date3 M4 A9
Code:
AsAtDate M1 M2 M3 M4
-------- -- -- -- --
Date1 A1 A2 A3 <NULL>
Date2 A4 <NULL> A5 A6
Date3 A7 A8 <NULL> A9
There is no way to determine the number of Mnemonic's (ie. the number of cols that will need to be returned) until the initial SELECT statement has been run (ie. to determine which Mnemonic's are in the set).
The query that returns this data set is rather complicated already - merging results off various servers and dbs. Hence, the approach (in my sproc) I am trying to get work atm (which won't be the most efficient, but is a nice starting place) is:
****
Code:
CREATE PROCEDURE dbo.spByTypeTesting AS
DECLARE @sql varchar(1500)
CREATE TABLE #tblData (AsAtDate datetime, Mnemonic varchar(4), Amount float)
/* Table to hold the "right" data temporarily */
INSERT INTO #tblData exec spByType
/* We put the RIGHT DATA into it */
/* The below query finds the "column headings".... but how do I make them "be" the column headings? */
SELECT Mnemonic
FROM #tblData
/* Let's create the Table itself... only problem is, need to create it dynamically. */
CREATE TABLE #tblRedoneData (AsAtDate datetime)
/* Insert some code that:
Iterates through each row of #tblData. For each row:
If AsAtDate IS IN @tblRedoneData
Insert Amount into Column "Mnemonic" (There will only ever be one Amount for each Mnemonic on a date)
Else
Insert AsAtDate into AsAtDate Column, Amount into Column "Mnemonic"
*/
SELECT *
FROM #tblRedoneData
DROP table #tblRedoneData
GO
In other words, have the right _data_ in a temp table, and then figure out how to transform it into the right _format_.
Problem is that I have no idea how to do this. I've been reading all the threads I have been able to find. I don't believe a Pivot Table is the answer. Perhaps a cursor?
The first step I have been trying with the above code is simply to create the table with the right column headings. However, not even -
****
Code:
SET @sql='CREATE TABLE #tblRedoneData (AsAtDate datetime)'
print(@sql)
exec(@sql)
SELECT *
FROM #tblRedoneData
Will work. I am guessing that that is because the temp table will only exist for the duration of the execute statement?
Any ideas, links, solutions would be much appreciated. I am very much stuck on this one!
Much TIA