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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 removing rows with repeated data

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 717
Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 610
Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 310

So 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.code
left 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.code
group by t1.first_name, t1.last_name, t1.address


Nathan Skerl
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-08 : 03:15:42
Refer this
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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',112

select * from @results

-------------------------------------------------------------------------
-- generate a common id for those with matching names, address, etc.
-------------------------------------------------------------------------
update r
set test_id = d.test_id
from @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 int

select @code = '',
@last_id = -1

update @results
set @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_csv
from @results
group by first_name, last_name, address


Nathan Skerl
Go to Top of Page

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 ProjectID
The 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 ProjectID
1111 10, 12, 13
1123 12, 15, 16

or
UserID ProjectID
1125 10 12 15

or
1124 10
12

1122 15
16
17

1111 10
11
Total
3
1012
1024

Does this make any sense?? sorry...please ask me questions if you need clarification.
Thanks
Go to Top of Page

jelost
Starting Member

7 Posts

Posted - 2005-09-15 : 16:35:39
UserID ProjectID
1111 10, 12, 13
1123 12, 15, 16

or
UserID ProjectID
1125...10..12..15

or
1124....10
............12

1122....15
............16
............17

1111....10
............11
Total
3


The ...are just spaces ignore them, sorry!
Thanks
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-09-15 : 23:55:58
Try posting like this:

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-16 : 00:31:58
quote:
1124....10
............12

1122....15
............16
............17

1111....10
............11


If you want this format in your Report use Suppress Duplicate option for UserId column

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -