Author |
Topic |
jrl55
Starting Member
4 Posts |
Posted - 2010-04-28 : 08:00:54
|
This is a variation on a recent post by scabral7, but I just can't figure it out. I have this in the table:Jan aaaJan bbbFeb aaaFeb cccFeb dddMar bbbMar eeeI need this in the query result:Jan Feb Maraaa aaa bbbbbb ccc eee dddAny help would be greatly welcome!Many thanksJohn |
|
jrl55
Starting Member
4 Posts |
Posted - 2010-04-28 : 08:42:59
|
Sorry, the 'ddd' in the query result should be in the Feb column of course. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 09:34:23
|
quote: Originally posted by madhivanan See if this helpshttp://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx
I don't think that's going to be much help, as there are no aggregates in the OP. Here's something that may help:Suppose the data is stored in a table called "Test", with columns "Mon" and "Data", then:CREATE TABLE #tmpJan ( ID INT IDENTITY (1, 1), Data CHAR(3))CREATE TABLE #tmpFeb ( ID INT IDENTITY (1, 1), Data CHAR(3))CREATE TABLE #tmpMar ( ID INT IDENTITY (1, 1), Data CHAR(3))INSERT INTO #tmpJan (Data)SELECT Data FROM TestWHERE Mon = 'Jan'ORDER BY DataINSERT INTO #tmpFeb (Data)SELECT Data FROM TestWHERE Mon = 'Feb'ORDER BY DataINSERT INTO #tmpMar (Data)SELECT Data FROM TestWHERE Mon = 'Mar'ORDER BY DataSELECT Jan.Data AS Jan, Feb.Data AS Feb, Mar.Data AS MarFROM #tmpJan JanFULL OUTER JOIN #tmpFeb Feb ON Jan.ID = Feb.IDFULL OUTER JOIN #tmpMar Mar ON Jan.ID = Mar.IDDROP TABLE #tmpJan DROP TABLE #tmpFeb DROP TABLE #tmpMar It's a little messy, and would be much simpler on a 2005 server, with the use of ROW_NUMBER. Basically, it uses the IDENTITY column of each temp table to generate the row number, then joins on that row number.Hope this helps.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 09:42:48
|
Actually, you'll need to be a little more clever with the joins. I think you'll need to use COALESCE, like this:SELECT Jan.Data AS Jan, Feb.Data AS Feb, Mar.Data AS Mar, Apr.Data AS Apr, May.Data as MayFROM #tmpJan JanFULL OUTER JOIN #tmpFeb Feb ON Jan.ID = Feb.IDFULL OUTER JOIN #tmpMar Mar ON COALESCE(Jan.ID, Feb.ID) = Mar.IDFULL OUTER JOIN #tmpApr Apr ON COALESCE(Jan.ID, Feb.ID, Mar.ID) = Apr.IDFULL OUTER JOIN #tmpMay May ON COALESCE(Jan.ID, Feb.ID, Mar.ID, Apr.ID) = May.ID ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
|
|
jrl55
Starting Member
4 Posts |
Posted - 2010-04-28 : 10:16:55
|
Thank you very much for your suggestions. I'll try these out. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 10:42:41
|
wont this be enough?SELECT [Jan],[Feb],[Mar]FROM(SELECT ROW_NUMBER() OVER (PARTITION BY Mon ORDER BY Data ASC) AS Seq, Mon,DataFROM Table)tPIVOT (MAX(Data) FOR Mon IN ([Jan],[Feb],[Mar]))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 11:16:28
|
quote: Originally posted by visakh16 wont this be enough?SELECT [Jan],[Feb],[Mar]FROM(SELECT ROW_NUMBER() OVER (PARTITION BY Mon ORDER BY Data ASC) AS Seq, Mon,DataFROM Table)tPIVOT (MAX(Data) FOR Mon IN ([Jan],[Feb],[Mar]))p
Not in a SQL 2000 Forum. ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 11:32:07
|
[code]SELECT MAX(CASE WHEN Mon='Jan' THEN Data ELSE NULL END) AS [Jan],MAX(CASE WHEN Mon='Feb' THEN Data ELSE NULL END) AS [Feb],MAX(CASE WHEN Mon='Mar' THEN Data ELSE NULL END) AS [Mar]FROM(SELECT (SELECT COUNT(*) FROM Table WHERE Mon=r.Mon AND Data < r.Data)+1 AS Seq, Mon,DataFROM Table r)tGROUP BY Seq[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
jrl55
Starting Member
4 Posts |
Posted - 2010-04-28 : 11:49:08
|
Thanks to everybody for your quick and helpful replies, but thanks particularly to 'DBA in the making' - you've solved a problem I've been grappling with for a couple of days. I'm new to this forum but I already think it's great! |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 12:09:50
|
quote: Originally posted by visakh16
SELECT MAX(CASE WHEN Mon='Jan' THEN Data ELSE NULL END) AS [Jan],MAX(CASE WHEN Mon='Feb' THEN Data ELSE NULL END) AS [Feb],MAX(CASE WHEN Mon='Mar' THEN Data ELSE NULL END) AS [Mar]FROM(SELECT (SELECT COUNT(*) FROM Table WHERE Mon=r.Mon AND Data < r.Data)+1 AS Seq, Mon,DataFROM Table r)tGROUP BY Seq
That will also work, but only if there are no duplicate values of Data for a given month.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 12:10:28
|
quote: Originally posted by jrl55 Thanks to everybody for your quick and helpful replies, but thanks particularly to 'DBA in the making' - you've solved a problem I've been grappling with for a couple of days. I'm new to this forum but I already think it's great!
No worries. :)------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
|
|
|