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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Joining duplicate data as a single row

Author  Topic 

CDeBenedetto
Starting Member

2 Posts

Posted - 2009-04-19 : 19:06:24
Where to start...

Table article_author:
[id] [article_id] [author]
1 11 author 1
2 12 author 2
3 13 author 3
4 13 another author 3

Table article:
[id] [short_title] [issue_date] [ce_link]
11 title 1 March 2009 link 1
12 title 2 February 2009 link 2
13 title 3 January 2009 link 3

I made a proc to get all the articles and authors:

select article.id, short_title, issue_date, ce_link,
authors.author
from article as article
INNER JOIN article_author authors ON authors.article_id = article.id
where isnull(short_title, '') <> ''
order by article.id desc

The problem is I'm getting back:
[id] [short_title] [issue_date] [ce_link] [author]
13 title 3 January 2009 link author 3
13 title 3 January 2009 link another author 3
12 title 2 February 2009 link author 2
11 title 1 March 2009 link author 1

Is there a way to put both authors with the same id into one row? I created a proc to do this, but I don't know if there is a way I can join it:

DECLARE @str VARCHAR(max)
SELECT @str = COALESCE(@str + ' and ', '') + [author]
FROM article_author
where article_id = @article_id
select [author] = @str

returns:
author 3 and another author 3

I hope I'm explaining my dilemma correctly...

Thanks,
Chris


chrianth
Yak Posting Veteran

50 Posts

Posted - 2009-04-19 : 22:03:31
quote:
Originally posted by CDeBenedetto

Where to start...

Table article_author:
[id] [article_id] [author]
1 11 author 1
2 12 author 2
3 13 author 3
4 13 another author 3

Table article:
[id] [short_title] [issue_date] [ce_link]
11 title 1 March 2009 link 1
12 title 2 February 2009 link 2
13 title 3 January 2009 link 3

I made a proc to get all the articles and authors:

select article.id, short_title, issue_date, ce_link,
authors.author
from article as article
INNER JOIN article_author authors ON authors.article_id = article.id
where isnull(short_title, '') <> ''
order by article.id desc

The problem is I'm getting back:
[id] [short_title] [issue_date] [ce_link] [author]
13 title 3 January 2009 link author 3
13 title 3 January 2009 link another author 3
12 title 2 February 2009 link author 2
11 title 1 March 2009 link author 1

Is there a way to put both authors with the same id into one row? I created a proc to do this, but I don't know if there is a way I can join it:

DECLARE @str VARCHAR(max)
SELECT @str = COALESCE(@str + ' and ', '') + [author]
FROM article_author
where article_id = @article_id
select [author] = @str

returns:
author 3 and another author 3

I hope I'm explaining my dilemma correctly...

Thanks,
Chris




Go to Top of Page

chrianth
Yak Posting Veteran

50 Posts

Posted - 2009-04-19 : 22:36:21
ooops, my post did not show...sorry.

basically you can't show a single data because you have two records of article_author.author on same article_author.article_id that is why it is showing both records...what I suggest for you to do is to eliminate one record in order for you to have a single output per article_author.article_id by getting the min/max article_author.ID for each unique article_author.article_id... add the following join on your query to do it...


INNER JOIN (SELECT max(ID) as maxID, article_id
FROM article_author
WHERE isnull(short_title, '') <> ''
GROUP BY article_id) XXX
ON(authors.article_id = XXX.id and authors.ID = XXX.maxID)

Hope I understand your question correctly...
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-04-21 : 00:12:18
if ur using 2005 try this one too
declare @tab table (id int, article_id int,author varchar(32))
insert into @tab select
1, 11, 'author 1' union all select
2, 12, 'author 2' union all select
3, 13, 'author 3' union all select
4, 13, 'another author 3'

declare @tab1 table (id int, short_title varchar(32), issue_date datetime, ce_link varchar(12))
insert into @tab1 select
11, 'title 1', 'March 2009', 'link 1' union all select
12, 'title 2', 'February 2009', 'link 2' union all select
13, 'title 3', 'January 2009', 'link 3'

select distinct t1.id, [short_title], [issue_date] ,[ce_link], stuff((select ' and '+ [author] from @tab where t.article_id = article_id for xml path('')),1,5,'')
from @tab t
inner join @tab1 t1 on t1.id = t.article_id

Go to Top of Page

CDeBenedetto
Starting Member

2 Posts

Posted - 2009-04-21 : 08:46:55
Thanks for your replies. I figured out I can just make it a function:


create function [dbo].[ce_author_join](@article_id int)
returns varchar(300)

as

begin

DECLARE @str VARCHAR(300)
SELECT @str = COALESCE(@str + ' and ', '') + [author]
FROM article_author
where article_id = @article_id

return @str

end

Then I called it in my proc:

select id as article_id, issue_folder_id, folder_id, short_title, issue_date, ce_link,
dbo.ce_author_join(id) as author, dbo.ce_hour_join(id) as ce_hour

from article
where continue_edu = 'true'
and isnull(short_title, '') <> ''
order by article_id desc
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-21 : 08:49:27
If you use SQL Server 2005, it is easier
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

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

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-04-23 : 05:29:59
Hi, benedetto
u can use the function or if ur using 2005 and above version u can use xml path too...........
Go to Top of Page
   

- Advertisement -