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)
 Problem with concats

Author  Topic 

adalgeir
Starting Member

4 Posts

Posted - 2009-12-23 : 04:54:06
Hi everyone

I need you help for something that I thought would be quite simple, but didn’t turn out to be (for me at least).

To simplify things I’ll create an imaginary example:

Table: Person
| PersonId | Name |
| 1 | John |
| 2 | Lisa |
| 3 | Jack |

Table: Quote
| PersonId | Quote |
| 1 | "Not sure how I’m.." |
| 1 | "I’ve done this before." |
| 2 | "Don’t you dare!" |
| 2 | "It’s easy." |
| 2 | "Ggrrr.." |
| 3 | "It wasn’t me!" |

OK, so what I want to do is to create a query that will join the two tables and concat all the quotes from each person into a single column, something like this:

| PersonId | Name | ConcatQuotes |
| 1 | John | "Not sure how I’m.." - "I’ve done this before." |
| 2 | Lisa | "Don’t you dare!" - "It’s easy" - "Ggrrr.." |
| 3 | Jack | "It wasn’t me!" |

Any ideas?

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-23 : 04:59:25
there have been tons of posts on this. The fastest way currently is to exploit the FOR XML behaviour of a sub-select.

If you search this site you'll get a good few methods.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-23 : 05:07:54
Something like:

/*
Table: Person
| PersonId | Name |
| 1 | John |
| 2 | Lisa |
| 3 | Jack |

Table: Quote
| PersonId | Quote |
| 1 | "Not sure how I’m.." |
| 1 | "I’ve done this before." |
| 2 | "Don’t you dare!" |
| 2 | "It’s easy." |
| 2 | "Ggrrr.." |
| 3 | "It wasn’t me!" |

OK, so what I want to do is to create a query that will join the two tables and concat all the quotes from each person into a single column, something like this:

| PersonId | Name | ConcatQuotes |
| 1 | John | "Not sure how I’m.." - "I’ve done this before." |
| 2 | Lisa | "Don’t you dare!" - "It’s easy" - "Ggrrr.." |
| 3 | Jack | "It wasn’t me!" |
*/

DECLARE @person TABLE ([personID] INT, [name] CHAR(4))
INSERT @person ([personId], [name])
SELECT 1, 'John'
UNION SELECT 2, 'Lisa'
UNION SELECT 3, 'Jack'

DECLARE @quote TABLE ([personId] INT, [quote] VARCHAR(255))
INSERT @quote ([personId], [quote])
SELECT 1, '"Not sure how I''m.."'
UNION SELECT 1, '"I''ve done this before."'
UNION SELECT 2, '"Don''t you dare!"'
UNION SELECT 2, '"It''s easy."'
UNION SELECT 2, '"Grrrr.."'
UNION SELECT 3, '"It wasn''t me!"'

SELECT * FROM @quote

SELECT
[personID]
, [name]
, LEFT([quotes], LEN([quotes]) -2) AS [ConcatQuotes]
FROM
(
SELECT
p.[personID]
, p.[name]
, (SELECT [quote] + ' - ' FROM @quote q WHERE q.[personID] = p.[personID] ORDER BY [quote] FOR XML PATH('')) AS [quotes]
FROM
@person p
)
q



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-23 : 05:10:28
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

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

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-23 : 05:14:06
good link.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-23 : 05:18:12
Also you can use Quirky update technique
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspx

Madhivanan

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

adalgeir
Starting Member

4 Posts

Posted - 2009-12-23 : 05:23:26
Thanks for the help guys - really appreciate it! When I was about to test this I realised that the tables are stored in an Oracle DB. :( This particular system stores data within couple of databases and I was so sure that these set of tables were within the SQL Server - which they weren't. Sigh..
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-23 : 05:28:53
LOL!

Got no clue which methods are best for oracle.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -