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 |
|
jelost
Starting Member
7 Posts |
Posted - 2005-09-07 : 09:33:22
|
| I am running a query on multiple tables and the data I get back consists of several repeated rows but with one column different. I want to take out those repeated rows and for the column that is different join that data and separate it by a comma. Can this be done?Ex.Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 717Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 610Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 310So i would like this data to come up as:Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 717,610,310 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2005-09-07 : 17:27:11
|
What is the unique identifier of this table? What is the meaning of the differing codes? Do you want to preserve the relationship between the two. Why the CSV? Why not break it out to 1:M table?You could acheive your select with something like below, but this assumes that you have < 3 possible distinct values of the 'code'. You can modify it to account for more, but I really think there is more to your problem. Can you post in more detail?Thanks!declare @test table (first_name varchar(10), last_name varchar(10), address varchar(100), code int)insert into @test select 'Cindy','Lair','111 Drury Circle Harrisburg Pennsylvania',717 union select 'Cindy','Lair','111 Drury Circle Harrisburg Pennsylvania',716 union select 'Cindy','Lair','111 Drury Circle Harrisburg Pennsylvania',715 select t1.first_name, t1.last_name, t1.address, isnull(cast(max(t1.code) as varchar),'') + isnull(',' + cast(max(t2.code) as varchar),'') + isnull(',' + cast(max(t3.code) as varchar),'') as 'code'from @test t1 left join @test t2 on t1.first_name = t2.first_name and t1.last_name = t2.last_name and t1.address = t2.address and t1.code > t2.codeleft join @test t3 on t1.first_name = t3.first_name and t1.last_name = t3.last_name and t1.address = t3.address and t2.code > t3.codegroup by t1.first_name, t1.last_name, t1.addressNathan Skerl |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2005-09-08 : 13:52:56
|
Here is a much cooler way to do this. This is from an article posted here at sqlteam, what Madhivanan is eluding to...  declare @results table (test_id int, first_name varchar(10), last_name varchar(10), address varchar(100), code varchar(5), code_csv varchar(8000))insert into @results (test_id, first_name, last_name, address, code) select 1,'Cindy','Lair','111 Drury Circle Harrisburg Pennsylvania',717 union select 2,'Cindy','Lair','111 Drury Circle Harrisburg Pennsylvania',716 union select 3,'Cindy','Lair','111 Drury Circle Harrisburg Pennsylvania',715 union select 4,'Nathan','Skerl','1234 Main',111 union select 5,'Nathan','Skerl','1234 Main',112select * from @results--------------------------------------------------------------------------- generate a common id for those with matching names, address, etc.-------------------------------------------------------------------------update rset test_id = d.test_idfrom @results r inner join (select min(test_id) test_id, first_name, last_name, address from @results group by first_name, last_name, address) d on r.first_name = d.first_name and r.last_name = d.last_name and r.address = d.address--------------------------------------------------------------------------- ** set based method from: [url]http://www.sqlteam.com/item.asp?ItemID=11021[/url]-------------------------------------------------------------------------declare @code varchar(8000), @last_id intselect @code = '', @last_id = -1update @resultsset @code = code_csv = case when @last_id <> test_id then code else @code + ', ' + code end, @last_id = test_id--------------------------------------------------------------------------- results-------------------------------------------------------------------------select first_name, last_name, address, convert(varchar(200),max(code_csv)) code_csvfrom @resultsgroup by first_name, last_name, address Nathan Skerl |
 |
|
|
jelost
Starting Member
7 Posts |
Posted - 2005-09-15 : 16:28:09
|
| Thanks very much for your responses.I am simply trying to get a report in SQL navigator by running a select statement. I can't create a table or a function I only have select access.I just want to be able to have the report show this: For example...There are 2 columns of data:UserID and ProjectIDThe User can work on many projects. So the same user id can have many different project id's. I just want to be able to have just one unique UserID with the different projectID's separated by commas or just listed next to each other. ex: UserID ProjectID1111 10, 12, 131123 12, 15, 16or UserID ProjectID 1125 10 12 15or 1124 10 121122 15 16 171111 10 11Total310121024Does this make any sense?? sorry...please ask me questions if you need clarification.Thanks |
 |
|
|
jelost
Starting Member
7 Posts |
Posted - 2005-09-15 : 16:35:39
|
| UserID ProjectID1111 10, 12, 131123 12, 15, 16or UserID ProjectID 1125...10..12..15or 1124....10............121122....15............16............171111....10............11Total3The ...are just spaces ignore them, sorry!Thanks |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-16 : 00:31:58
|
quote: 1124....10............121122....15............16............171111....10............11
If you want this format in your Report use Suppress Duplicate option for UserId columnMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|