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 |
legacyvbc
Starting Member
37 Posts |
Posted - 2008-01-25 : 15:41:21
|
I have two tables and need to create a pivot.Tbl1 = Date, Ticker, IndId12/31/2006, Msft, 112/31/2006, GE, 212/31/2006, WFC, 31/31/2007, Msft, 11/31/2007, GE, 212/31/2007, WFC, 3Tbl2 = IndId, IndName1, Tech2, Cons3, BankI have this data for 20 years so the industries change as tickers are moved in/outI want to have a summary table with distinct ticker and a column for each date (monthly for 20 years) with the data holding the indname from tbl2. It would look like:Ticker 12/06 01/07Msft Tech TechGE Cons ConsWFC Bank BankAny help is appreciated as I can't seem to figure this one out.I was using cases for each date but I would think there would be an easier way.Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-26 : 11:24:52
|
It seems like you are trying to show the data on some kind of report. If thats the case you are do this more easily at front end. Can i ask what front end you are using? |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2008-01-28 : 10:58:09
|
I am just bringing it into excel. If that is the easier way to do it then I can just proceed as such but if there is a way to write a query to do it any help is appreciated. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-28 : 11:03:25
|
quote: Originally posted by legacyvbc I am just bringing it into excel. If that is the easier way to do it then I can just proceed as such but if there is a way to write a query to do it any help is appreciated.
Pivoting in SQL 2000 is a bit of pain. I think it would be nice if you could try to bring data to excel and then try to Pivot. Excel has a in built pivot functionality. You may find this link helpful:-[url]http://www.microsoft.com/dynamics/using/excel_pivot_tables_collins.mspx[/url] |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-28 : 11:11:14
|
That's over 240 columns! E 12°55'05.25"N 56°04'39.16" |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2008-01-28 : 16:19:54
|
i know it is a lot of columns. I would actually be creating a procedure so it would typically be a subset of dates. as an example, Jun-06 - Jun-07 which would only be 12 columns. That is why I am trying to do this. Thanks anyway. |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2008-01-28 : 16:19:58
|
i know it is a lot of columns. I would actually be creating a procedure so it would typically be a subset of dates. as an example, Jun-06 - Jun-07 which would only be 12 columns. That is why I am trying to do this. Thanks anyway. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-29 : 02:11:33
|
It is quite easy to write a query that will show the next 12 months in a PIVOT style. E 12°55'05.25"N 56°04'39.16" |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2008-01-29 : 13:43:36
|
how would I write the query? do I just use the case for each month. Also, how do I join the tables so instead of the id showing it will display the name? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-29 : 16:38:58
|
Use something like this and name the column at the front-end!DECLARE @Tbl1 TABLE (Date DATETIME, Ticker VARCHAR(10), IndID INT)SET DATEFORMAT MDYINSERT @Tbl1SELECT '12/31/2006', 'Msft', 1 UNION ALLSELECT '12/31/2006', 'GE', 2 UNION ALLSELECT '12/31/2006', 'WFC', 3 UNION ALLSELECT '1/31/2007', 'Msft', 1 UNION ALLSELECT '1/31/2007', 'GE', 2 UNION ALLSELECT '12/31/2007', 'WFC', 3DECLARE @Tbl2 TABLE (IndID INT, IndName VARCHAR(10))INSERT @Tbl2SELECT 1, 'Tech' UNION ALLSELECT 2, 'Cons' UNION ALLSELECT 3, 'Bank'DECLARE @FirstDate DATETIMESET @FirstDate = '20061203'SELECT t1.Ticker, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 0 THEN t2.IndName ELSE '' END) AS m00, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 1 THEN t2.IndName ELSE '' END) AS m01, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 2 THEN t2.IndName ELSE '' END) AS m02, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 3 THEN t2.IndName ELSE '' END) AS m03, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 4 THEN t2.IndName ELSE '' END) AS m04, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 5 THEN t2.IndName ELSE '' END) AS m05, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 6 THEN t2.IndName ELSE '' END) AS m06, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 7 THEN t2.IndName ELSE '' END) AS m07, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 8 THEN t2.IndName ELSE '' END) AS m08, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 9 THEN t2.IndName ELSE '' END) AS m09, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 10 THEN t2.IndName ELSE '' END) AS m10, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 11 THEN t2.IndName ELSE '' END) AS m11FROM @Tbl1 AS t1INNER JOIN @Tbl2 AS t2 ON t2.IndID = t1.IndIDGROUP BY t1.TickerORDER BY t1.Ticker E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-29 : 16:40:58
|
The stored procedure should look something like thisCREATE PROCEDURE dbo.uspShowTicker( @FirstDate DATETIME)ASSET NOCOUNT ONSELECT t1.Ticker, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 0 THEN t2.IndName ELSE '' END) AS m00, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 1 THEN t2.IndName ELSE '' END) AS m01, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 2 THEN t2.IndName ELSE '' END) AS m02, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 3 THEN t2.IndName ELSE '' END) AS m03, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 4 THEN t2.IndName ELSE '' END) AS m04, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 5 THEN t2.IndName ELSE '' END) AS m05, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 6 THEN t2.IndName ELSE '' END) AS m06, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 7 THEN t2.IndName ELSE '' END) AS m07, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 8 THEN t2.IndName ELSE '' END) AS m08, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 9 THEN t2.IndName ELSE '' END) AS m09, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 10 THEN t2.IndName ELSE '' END) AS m10, MAX(CASE WHEN DATEDIFF(MONTH, @FirstDate, t1.Date) = 11 THEN t2.IndName ELSE '' END) AS m11FROM @Tbl1 AS t1INNER JOIN @Tbl2 AS t2 ON t2.IndID = t1.IndIDGROUP BY t1.TickerORDER BY t1.Ticker Call the SP from your client.And since you know the value of the parameter passed to the SP, you can easily rename the columns in your application. E 12°55'05.25"N 56°04'39.16" |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2008-01-29 : 19:52:36
|
awesome, thanks |
 |
|
|
|
|
|
|