Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi all,I have a crosstab query which has come from a table of hierarchical data e.g. Item----code----description----valueAB1.....12......Width..........100AB1.....13......Height.........200AB1.....14......Colour.........RedAB2.....12......Width..........120AB2.....13......Height.........210AB2.....14......Colour.........Bluewhich looks a bit like this :Item-----Width----Height----ColourAB1......100......null......nullAB1......null.....200.......nullAB1......null.....null......RedetcIs there any way that I can get :Item----Width----Height----ColourAB1.....100......200.......RedAB2.....120......210.......BlueThe reason is I need to get an SSRS report per line out for each item and am struggling because of the null values.If there's another way round it I'll gladly take that as well! I'm stuck!Thanks very much for your time - please feel free to get me to clarify anything I haven't explained properly. I'm using sql server 2005.RegardsSimon
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-08-19 : 12:34:30
[code]SELECT Item,MAX(CASE WHEN description ='Width' THEN value ELSE NULL END) AS Width,MAX(CASE WHEN description ='Height' THEN value ELSE NULL END) AS Height,MAX(CASE WHEN description ='Colour' THEN value ELSE NULL END) AS ColourFROM YourQueryGROUP BY Item[/code]
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-08-19 : 12:36:54
or use pivot
SELECT Item, [Width], [Height], [Colour]FROM (SELECT Item,code,description,value FROM Yourquery)mPIVOT (MAX(value) FOR description IN ([Width],[Height],[Colour]))p
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-08-19 : 12:41:13
And another way is to do this in your ssrs report with a matrix. just place give row grouping as item field and column grouping based on description field.
SimonMallinson
Starting Member
3 Posts
Posted - 2008-08-19 : 15:07:32
Thanks very much for the help - I will give those a go when I get to work tomorrow morning!RegardsSimon
SimonMallinson
Starting Member
3 Posts
Posted - 2008-08-20 : 07:39:58
Worked a treat - thanks.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-08-20 : 07:41:54
quote:Originally posted by SimonMallinson Worked a treat - thanks.