Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Concatenating rows from aliased tables on MSSQL 2005 server Expand / Collapse
Author
Message
Posted Tuesday, March 4, 2014 9:59 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 4:04 AM
Points: 11, Visits: 25
Hi Everyone, I'm newbie here and in TSQL too, so please be lenient and patient for me ;)

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.
Post #1547425
Posted Tuesday, March 4, 2014 2:18 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
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."
Post #1547560
Posted Tuesday, March 4, 2014 4:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 4:04 AM
Points: 11, Visits: 25
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 ;)
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.
Post #1547596
Posted Monday, March 17, 2014 8:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 4:04 AM
Points: 11, Visits: 25
Hi, could anybody help me? Please.
Or please tell me, where (on which forum) can I find an aid.
Post #1551777
Posted Monday, March 17, 2014 9:22 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:36 PM
Points: 1,886, Visits: 18,565
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 !
__________________________________________________________________
Post #1551795
Posted Tuesday, March 18, 2014 6:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, July 20, 2014 4:32 PM
Points: 14, Visits: 50
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

Post #1552124
Posted Tuesday, March 18, 2014 6:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 4:04 AM
Points: 11, Visits: 25
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.
Post #1552143
Posted Tuesday, March 18, 2014 7:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
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 Moden's 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)
Post #1552189
Posted Wednesday, March 19, 2014 7:49 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 69, Visits: 462
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.
Post #1552618
Posted Wednesday, March 19, 2014 9:45 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 4:04 AM
Points: 11, Visits: 25
Ok, I'll try to prepare simple data as you mentioned and put it here.
Post #1552683
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse