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 |
|
dpais
Yak Posting Veteran
60 Posts |
Posted - 2010-04-06 : 09:22:55
|
I'm trying to get the following output to work with no luck - please helpinput data is - DDL is below - Category CType LocaionMats 2 ZoneAMats 3 ZoneCPulleys 1 ZoneAPulleys 2 ZoneAWidgets 1 ZoneX please help with the needed output below - ';' could be commas or pipes ('|') ... Widgets CType LoctionMats 2;3 ZoneA;ZoneCPulleys 1;2 ZoneAWidgets 1 ZoneXDDL is - declare @TableTest table ( Category varchar(20) NOT NULL, CType nvarchar(20) NOT NULL, Location varchar(20) NOT NULL )Insert Into @TableTest (Category,CType,Location)select 'Widgets',1,'ZoneX'unionselect 'Pulleys',2,'ZoneA'unionselect 'Pulleys',1,'ZoneA'unionselect 'Mats',3,'ZoneC'unionselect 'Mats',2,'ZoneA'select * from @TableTest |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-06 : 09:43:26
|
| [code]declare @TableTest table ( Category varchar(20) NOT NULL, CType nvarchar(20) NOT NULL,Location varchar(20) NOT NULL )Insert Into @TableTest (Category,CType,Location)select 'Widgets',1,'ZoneX'unionselect 'Pulleys',2,'ZoneA'unionselect 'Pulleys',1,'ZoneA'unionselect 'Mats',3,'ZoneC'unionselect 'Mats',2,'ZoneA'--select * from @TableTest;with cte as(SELECT Category, CType, Location,ROW_NUMBER() OVER(Partition by Category order by Category) seqFROM @TableTest a)SELECT Category, STUFF((SELECT ':' + Ctype FROM cte WHERE Category = c.Category FOR XML PATH('')), 1,1,''), STUFF((SELECT ',' + Location FROM cte WHERE Category = c.Category FOR XML PATH('')), 1,1,'')FROM cte cWHERE seq = 1[/code]Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-04-06 : 11:24:15
|
| Bad SQL Programmers always ask how to destroy 1NF. An expensive recursive CTE is the best of the bad answers. The right answer is to do display formatting in the front end and not in the database. Do you have a report writer?--CELKO--Joe Celko, SQL Guru |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-06 : 11:42:45
|
quote: Originally posted by jcelko Bad SQL Programmers always ask how to destroy 1NF. An expensive recursive CTE is the best of the bad answers. The right answer is to do display formatting in the front end and not in the database. Do you have a report writer?--CELKO--Joe Celko, SQL Guru
that depends on where you're showing the data. Sometime you do have to retrieve data as in format shown from table when you dont have any easier way to achieve this at your front end.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dpais
Yak Posting Veteran
60 Posts |
Posted - 2010-04-06 : 12:01:15
|
| no jcelko I don' have a report writer - and thank you Vaibhav |
 |
|
|
dpais
Yak Posting Veteran
60 Posts |
Posted - 2010-04-06 : 17:04:47
|
| Sorry Vaibhav your output for Pulley looks like Pulleys 1:2 ZoneA,ZoneAi need it to readPulleys 1:2 ZoneAthanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-07 : 00:14:02
|
quote: Originally posted by dpais Sorry Vaibhav your output for Pulley looks like Pulleys 1:2 ZoneA,ZoneAi need it to readPulleys 1:2 ZoneAthanks
make a small tweak as follows and try;with cte as(SELECT Category, CType, Location,ROW_NUMBER() OVER(Partition by Category order by Category) seqFROM @TableTest a)SELECT Category, STUFF((SELECT ':' + Ctype FROM cte WHERE Category = c.Category FOR XML PATH('')), 1,1,''), STUFF((SELECT DISTINCT ',' + Location FROM cte WHERE Category = c.Category FOR XML PATH('')), 1,1,'')FROM cte cWHERE seq = 1------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-07 : 02:35:34
|
Hi visakh I have seen that output of my query is not giving required output.but i know there i need to put distinct somewhere but i was nt able to find the place Thats why i posted the query as it is to get the advice from the experts like you... Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-07 : 04:02:33
|
quote: Originally posted by vaibhavktiwari83 Hi visakh I have seen that output of my query is not giving required output.but i know there i need to put distinct somewhere but i was nt able to find the place Thats why i posted the query as it is to get the advice from the experts like you... Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER
No worriesGlad that I could sort it out for you ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-07 : 09:13:19
|
There is no need to use any recursive CTE but not sure of the performance though.SELECT Category, STUFF((SELECT ':' + Ctype FROM @TableTest WHERE Category = c.Category FOR XML PATH('')), 1,1,''), STUFF((SELECT distinct ',' + Location FROM @TableTest WHERE Category = c.Category FOR XML PATH('')), 1,1,'')FROM @TableTest cgroup by CategoryPBUH |
 |
|
|
dpais
Yak Posting Veteran
60 Posts |
Posted - 2010-04-11 : 08:25:23
|
| thanks guys - the grouping query was a success. appreciate the help. |
 |
|
|
|
|
|
|
|