| Author |
Topic |
|
conedm
Starting Member
33 Posts |
Posted - 2010-09-02 : 10:34:58
|
| I am trying to take multiple rows and comma seperate one column with the data. It is not doing it. Similar statements work but not this one. Can some one help? Below is the code...SELECT dbo.WEBVENDOR.VENDORID, STUFF ((SELECT ',' + CONVERT(VARCHAR(6), CSCONTRACTS.dbo.CONTRPROPOSALS.SCALENUM)FROM CSCONTRACTS.dbo.CONTRPROPOSALSWHERE WEBVENDOR.SCALENUM = CSCONTRACTS.dbo.CONTRPROPOSALS.SCALENUM AND CSCONTRACTS.dbo.CONTRPROPOSALS.SEQUENCE = 2 FOR XML PATH('')), 1, 1, '') AS [SCALES]FROM dbo.WEBVENDORWHERE (WEBVENDOR.SEQUENCE = 1 OR WEBVENDOR.SEQUENCE = 11)ORDER BY WEBVENDOR.VENDORID |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-02 : 12:27:26
|
| Did you get any error or unexpected result?MadhivananFailing to plan is Planning to fail |
 |
|
|
conedm
Starting Member
33 Posts |
Posted - 2010-09-02 : 12:36:52
|
| I always get this error:Error in WHERE clause near '('. Unable to parse query text.But I get that even with the similar statements that work.the result set shows everything as a seperate recordexamplevendorid scales408 1543408 1480408 4081I want:vendorid scales408 1543,1480,4081 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-02 : 12:42:12
|
| Are you using SQL Server version higher than 2000?MadhivananFailing to plan is Planning to fail |
 |
|
|
conedm
Starting Member
33 Posts |
Posted - 2010-09-02 : 12:44:16
|
| Sorry. I forgot to mention. I am using SQL Server 2005 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-02 : 12:59:09
|
| [code]declare @t table (vendorid int,scales int)insert into @t select 408 ,1543 union allselect 408, 1480 union allselect 408, 4081select * from @tSELECT DISTINCT s1.vendorid, STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + cast(s2.scales as varchar(10)) FROM @t AS s2 WHERE s2.vendorid = s1.vendorid ORDER BY ',' + cast(s2.scales as varchar(10)) FOR XML PATH('')), 1, 1, '') AS scalesFROM @t AS s1ORDER BY s1.vendorid[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-09-02 : 13:05:41
|
| Please, provide sample data and expected output |
 |
|
|
conedm
Starting Member
33 Posts |
Posted - 2010-09-02 : 15:40:41
|
| I used what you had and had to alter it a bit to work. But now it is perfect. Thank you!!!Here is what it ended up beingDECLARE @t TABLE (vendorid int, scales int) INSERT INTO @tSELECT WEBVENDOR.VENDORID, WEBVENDOR.SCALENUMFROM WEBVENDOR INNER JOINCSCONTRACTS.dbo.CONTRPROPOSALS ON WEBVENDOR.SCALENUM = CSCONTRACTS.dbo.CONTRPROPOSALS.SCALENUMWHERE (WEBVENDOR.SEQUENCE = 11 ORWEBVENDOR.SEQUENCE = 1) AND (CSCONTRACTS.dbo.CONTRPROPOSALS.SEQUENCE = 2)SELECT DISTINCT s1.vendorid, STUFF((SELECT DISTINCT ',' + CONVERT(varchar(10), s2.scales)FROM @t AS s2WHERE s2.vendorid = s1.vendoridORDER BY ',' + CONVERT(varchar(10), s2.scales) FOR XML PATH('')), 1, 1, '') AS scalesFROM @t AS s1ORDER BY s1.vendorid |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-03 : 04:15:53
|
quote: Originally posted by conedm I used what you had and had to alter it a bit to work. But now it is perfect. Thank you!!!Here is what it ended up beingDECLARE @t TABLE (vendorid int, scales int) INSERT INTO @tSELECT WEBVENDOR.VENDORID, WEBVENDOR.SCALENUMFROM WEBVENDOR INNER JOINCSCONTRACTS.dbo.CONTRPROPOSALS ON WEBVENDOR.SCALENUM = CSCONTRACTS.dbo.CONTRPROPOSALS.SCALENUMWHERE (WEBVENDOR.SEQUENCE = 11 ORWEBVENDOR.SEQUENCE = 1) AND (CSCONTRACTS.dbo.CONTRPROPOSALS.SEQUENCE = 2)SELECT DISTINCT s1.vendorid, STUFF((SELECT DISTINCT ',' + CONVERT(varchar(10), s2.scales)FROM @t AS s2WHERE s2.vendorid = s1.vendoridORDER BY ',' + CONVERT(varchar(10), s2.scales) FOR XML PATH('')), 1, 1, '') AS scalesFROM @t AS s1ORDER BY s1.vendorid
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
|