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 |
|
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 23 13 author 34 13 another author 3Table article:[id] [short_title] [issue_date] [ce_link]11 title 1 March 2009 link 112 title 2 February 2009 link 213 title 3 January 2009 link 3I made a proc to get all the articles and authors:select article.id, short_title, issue_date, ce_link,authors.authorfrom article as articleINNER JOIN article_author authors ON authors.article_id = article.idwhere isnull(short_title, '') <> ''order by article.id descThe problem is I'm getting back:[id] [short_title] [issue_date] [ce_link] [author]13 title 3 January 2009 link author 313 title 3 January 2009 link another author 312 title 2 February 2009 link author 211 title 1 March 2009 link author 1Is 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_authorwhere article_id = @article_idselect [author] = @strreturns:author 3 and another author 3I 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 23 13 author 34 13 another author 3Table article:[id] [short_title] [issue_date] [ce_link]11 title 1 March 2009 link 112 title 2 February 2009 link 213 title 3 January 2009 link 3I made a proc to get all the articles and authors:select article.id, short_title, issue_date, ce_link,authors.authorfrom article as articleINNER JOIN article_author authors ON authors.article_id = article.idwhere isnull(short_title, '') <> ''order by article.id descThe problem is I'm getting back:[id] [short_title] [issue_date] [ce_link] [author]13 title 3 January 2009 link author 313 title 3 January 2009 link another author 312 title 2 February 2009 link author 211 title 1 March 2009 link author 1Is 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_authorwhere article_id = @article_idselect [author] = @strreturns:author 3 and another author 3I hope I'm explaining my dilemma correctly...Thanks,Chris
|
 |
|
|
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) XXXON(authors.article_id = XXX.id and authors.ID = XXX.maxID)Hope I understand your question correctly... |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-04-21 : 00:12:18
|
| if ur using 2005 try this one toodeclare @tab table (id int, article_id int,author varchar(32))insert into @tab select 1, 11, 'author 1' union all select2, 12, 'author 2' union all select3, 13, 'author 3' union all select4, 13, 'another author 3'declare @tab1 table (id int, short_title varchar(32), issue_date datetime, ce_link varchar(12))insert into @tab1 select11, 'title 1', 'March 2009', 'link 1' union all select12, 'title 2', 'February 2009', 'link 2' union all select13, '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 tinner join @tab1 t1 on t1.id = t.article_id |
 |
|
|
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)asbeginDECLARE @str VARCHAR(300)SELECT @str = COALESCE(@str + ' and ', '') + [author]FROM article_authorwhere article_id = @article_idreturn @strendThen 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_hourfrom articlewhere continue_edu = 'true'and isnull(short_title, '') <> ''order by article_id desc |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-04-23 : 05:29:59
|
| Hi, benedettou can use the function or if ur using 2005 and above version u can use xml path too........... |
 |
|
|
|
|
|
|
|