Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pivoting a table (sort of) through a bunch of joins

Status
Not open for further replies.

iwease

IS-IT--Management
Sep 4, 2006
104
CA
This post is an extension of my post
I want to pivot data but I don't think I want to pivot it in a method that follows SQL's PIVOT/UNPIVOT transformation.

Basically, suppose I have a view that looks like

View: StockRetreivedData
col1: (pk)FK_DataTypeID (this is the type of data retrieved. Could be a price, p/e ratio, 52 week high, etc)
col2: (pk)FK_QuoteID (this is the ID of a DateTime value and a stock value...basically represents the time several pieces of data for a stock were retrieved)
col3: Value (value for the data)

Now what I want is to create a second view to show all the data for each stock

View: StockPivotData
col1: QuoteID
col2: FK_DAtaTypeID =1 (ie, price)
col3: FK_DAtaTypeID =2 (ie, p/e ratio)
col4: FK_DAtaTypeID =3 (ie, 52 week high)
etc

I have about 60 different data types. Is it OK to create view 2 via the following SQL, or is this just way to slow

SELECT c1.FK_QuoteID ,
c1.Value as price,
c2.Value as peRatio,
c3.Value as High52Week,
...up to c60...
FROM
(SELECT Value FROM StockRetreivedData WHERE FK_DAtaType = 1) as c1
LEFT OUTER JOIN
(SELECT Value FROM StockRetreivedData WHERE FK_DAtaType = 2) as c2
ON c1.FK_QuoteID = c2.FK_QuoteID
LEFT OUTER JOIN
(SELECT Value FROM StockRetreivedData WHERE FK_DAtaType = 3) as c3
ON c1.FK_QuoteID = c3.FK_QuoteID
...and so on up to c60

I would likely use dynamic SQL to create this as well since it seems awefully long and messy to write out by hand

Thanks in advance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top