SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concatenating rows from aliased tables on MSSQL 2005 server


Concatenating rows from aliased tables on MSSQL 2005 server

Author
Message
domingo.sqlservercentral
domingo.sqlservercentral
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 35
Hi Everyone, I'm newbie here and in TSQL too, so please be lenient and patient for me Wink

I have some trouble with row concatenation running MSSQL server 2005.

I wrote this query below and it works well when for some row from s_element, all of four tables (aliased here as: authors, composers, performers, arrangeurs) have the one only rows matching (or have none).
But when for some row from s_element are more matching persons in these 4 tables, my query multiply results for this one row from s_element (with this persons in other result rows).
My intention is, that query returns not multiplicated rows from s_element with concatenated matched rows from these four tables (and separated with some special character).

The code is:

SELECT
s_element.sendeplatz,
RTRIM (ISNULL(jingle.SzTitle, '')),
ISNULL (auths.szname, ''),
ISNULL (comps.szname, ''),
ISNULL (performs.szname, ''),
ISNULL (arrangs.szname, ''),
ISNULL (publisher.szname, ''),
RTRIM (ISNULL(jingle.lid, '')),
RTRIM (ISNULL(jingle.szshortinfo, ''))

FROM s_planhdh
LEFT OUTER JOIN s_element ON s_element.planheaderid = s_planhdh.planheaderid
LEFT OUTER JOIN programmitem ON programmitem.lid = s_element.lprogrammitemid
LEFT OUTER JOIN jingle ON jingle.lid = s_element.lprogrammitemid
LEFT OUTER JOIN publisher ON publisher.lid = jingle.lpublisherid
LEFT OUTER JOIN jinglepersonrolle AS authors ON authors.ljingleid = jingle.lid AND authors.larolleid = (SELECT lid FROM arolle WHERE arolle.szname = 'Author')
LEFT OUTER JOIN jinglepersonrolle AS composers ON composers.ljingleid = jingle.lid AND composers.larolleid = (SELECT lid FROM arolle WHERE arolle.szname = 'Composer')
LEFT OUTER JOIN jinglepersonrolle AS arrangeurs ON arrangeurs.ljingleid = jingle.lid AND arrangeurs.larolleid = (SELECT lid FROM arolle WHERE arolle.szname = 'Arrangeur')
LEFT OUTER JOIN jinglepersonrolle AS performers ON performers.ljingleid = jingle.lid AND performers.larolleid = (SELECT lid FROM arolle WHERE arolle.szname = 'Performer')
LEFT OUTER JOIN aperson AS comps ON comps.lid = composers.lapersonid
LEFT OUTER JOIN aperson AS auths ON auths.lid = authors.lapersonid
LEFT OUTER JOIN aperson AS arrangs ON arrangs.lid = arrangeurs.lapersonid
LEFT OUTER JOIN aperson AS performs ON performs.lid = performers.lapersonid

WHERE
lprogrammitemtypid=30
AND filetype='I'

ORDER BY s_element.sendeplatz



My knownedge of TSQL is not enough, so please help me.
Kind regards, Rob.
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34768 Visits: 9518
domingo.sqlservercentral (3/4/2014)

My intention is, that query returns not multiplicated rows from s_element with concatenated matched rows from these four tables (and separated with some special character).


OK, then what do you want it to do when there are multiple matches for one of the rows?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
domingo.sqlservercentral
domingo.sqlservercentral
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 35
RBarryYoung (3/4/2014)
domingo.sqlservercentral (3/4/2014)

My intention is, that query returns not multiplicated rows from s_element with concatenated matched rows from these four tables (and separated with some special character).


OK, then what do you want it to do when there are multiple matches for one of the rows?



Oh, I'm so sorry for my mistake in that post Wink
My intention was, that query returns not multiplicated rows from s_element, BUT with concatenated matched rows from these four tables (separated with some special character). And number of returned rows equal to the situation without many matched persons from any of these four person aliased tables.
domingo.sqlservercentral
domingo.sqlservercentral
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 35
Hi, could anybody help me? Please.
Or please tell me, where (on which forum) can I find an aid.
J Livingston SQL
J Livingston SQL
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12107 Visits: 37528
sample data would help us help you....do you know how to provide this?

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

drew.king1
drew.king1
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 61
Do you mean like this:

SELECT
s_element.sendeplatz,
RTRIM (ISNULL(jingle.SzTitle, '')) + ' ' +
ISNULL (auths.szname, '') + ' ' +
ISNULL (comps.szname, '') + ' ' +
ISNULL (performs.szname, '') + ' ' +
ISNULL (arrangs.szname, '') + ' ' +
ISNULL (publisher.szname, '') + ' ' +
RTRIM (ISNULL(jingle.lid, '')) + ' ' +
RTRIM (ISNULL(jingle.szshortinfo, '')) AS CatColumn
domingo.sqlservercentral
domingo.sqlservercentral
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 35
J Livingston SQL (3/17/2014)
sample data would help us help you....do you know how to provide this?


No, I don't know. Do you mean about upload of database backup file? Or that I write you a script for generating sample base properly for the query I asked?


drew.king1 (3/18/2014)
Do you mean like this:
SELECT
s_element.sendeplatz,
RTRIM (ISNULL(jingle.SzTitle, '')) + ' ' +
ISNULL (auths.szname, '') + ' ' +
ISNULL (comps.szname, '') + ' ' +
ISNULL (performs.szname, '') + ' ' +
ISNULL (arrangs.szname, '') + ' ' +
ISNULL (publisher.szname, '') + ' ' +
RTRIM (ISNULL(jingle.lid, '')) + ' ' +
RTRIM (ISNULL(jingle.szshortinfo, '')) AS CatColumn


No, I don't. This concatinate *constant number* of different columns, and no multiple (indeterminate number) of the same parameter rows.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62233 Visits: 17954
domingo.sqlservercentral (3/18/2014)
J Livingston SQL (3/17/2014)
sample data would help us help you....do you know how to provide this?


No, I don't know. Do you mean about upload of database backup file? Or that I write you a script for generating sample base properly for the query I asked?


What he was eluding to is that in order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
CKX
CKX
SSChasing Mays
SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)SSChasing Mays (618 reputation)

Group: General Forum Members
Points: 618 Visits: 1033
domingo.sqlservercentral (3/18/2014)
[quote]J Livingston SQL (3/17/2014)
sample data would help us help you....do you know how to provide this?



No, I don't know. Do you mean about upload of database backup file? Or that I write you a script for generating sample base properly for the query I asked?


This just means that in order to help you it is best to provide some code and data that can help them recreate the problem and craft an answer.

You might do something like this:

<YOUR PROBLEM DEFINITION>
Blah blah

<SAMPLE DDL AND DATA>
CREATE TABLE #tmp(myfirstcol SMALLINT, mysecondcol SMALLINT);

INSERT #tmp(myfirstcol,
mysecondcol)
SELECT 1,2




<CURRENT RESULTS>
SELECT * FROM #tmp;

myfirstcol mysecondcol
1 2

<EXPECTED RESULTS>
mycol
1
2

Obviously, in your case you will need to provide ddl and data that accurately reflects your problem.
domingo.sqlservercentral
domingo.sqlservercentral
SSC-Enthusiastic
SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)SSC-Enthusiastic (115 reputation)

Group: General Forum Members
Points: 115 Visits: 35
Ok, I'll try to prepare simple data as you mentioned and put it here.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search