Click here to Skip to main content
15,890,579 members
Home / Discussions / Database
   

Database

 
GeneralRe: Equivalent in MS Sql Pin
jschell16-Dec-11 8:34
jschell16-Dec-11 8:34 
JokeRe: Equivalent in MS Sql Pin
SilimSayo16-Dec-11 14:31
SilimSayo16-Dec-11 14:31 
GeneralRe: Equivalent in MS Sql Pin
Shameel20-Dec-11 2:51
professionalShameel20-Dec-11 2:51 
QuestionGROUP BY and listing rows in an aggrigate Pin
nallelcm13-Dec-11 5:44
nallelcm13-Dec-11 5:44 
AnswerRe: GROUP BY and listing rows in an aggrigate Pin
Eddy Vluggen13-Dec-11 7:07
professionalEddy Vluggen13-Dec-11 7:07 
GeneralRe: GROUP BY and listing rows in an aggrigate Pin
nallelcm13-Dec-11 7:26
nallelcm13-Dec-11 7:26 
AnswerRe: GROUP BY and listing rows in an aggrigate Pin
Eddy Vluggen13-Dec-11 7:44
professionalEddy Vluggen13-Dec-11 7:44 
AnswerRe: GROUP BY and listing rows in an aggrigate Pin
Eddy Vluggen13-Dec-11 8:34
professionalEddy Vluggen13-Dec-11 8:34 
The same trick, basically;
SQL
BEGIN TRANSACTION
CREATE TABLE #EventType
(
 ID     INT
,[Name] VARCHAR(50)
)
INSERT INTO #EventType
      SELECT 1        ,'Party'
UNION SELECT 2        ,'Meeting'
UNION SELECT 3        ,'Something else that wasn''t mentioned'
 
CREATE TABLE #Person
(
 ID     INT
,[Name] VARCHAR(20)
)
INSERT INTO #Person
      SELECT 1      ,'Bob'
UNION SELECT 2      ,'Joe'
UNION SELECT 3      ,'Suzie'

CREATE TABLE [#Event]
(
 UID        INT
,EventID    INT
,EventType  INT
,Person     INT
)
INSERT INTO [#Event]
      SELECT 1,     1,           1,             1
UNION SELECT 2,     1,           1,             2
UNION SELECT 3,     1,           1,             3
UNION SELECT 4,     2,           1,             1
UNION SELECT 5,     2,           1,             3
UNION SELECT 6,     3,           2,             1
UNION SELECT 7,     3,           2,             2

SELECT DISTINCT EventId
           INTO #SomeTable
           FROM [#Event]

SELECT EventId
      ,[Name]
      ,SUBSTRING(CompoundColumn, 0, LEN(CompoundColumn) - LEN(', '))
      AS People
   FROM (
          SELECT ST.EventId
                ,ET.[Name]
                ,REPLACE(REPLACE(
                 (SELECT P.[Name]
                    FROM #Event E
                    JOIN #Person P ON E.Person = P.ID
                   WHERE E.EventID = ST.EventID
                    FOR XML AUTO), 
                 '<P Name="', ''), '"/>', ', '
                 ) AS CompoundColumn
            FROM #SomeTable ST
            LEFT JOIN #EventType ET ON ST.EventId = ET.ID
        ) AS tmp_cte

ROLLBACK

Desired result
EventID     EventTypeName      People
-------------------------------------------------------
1           Party              Bob, Joe, Suzie
2           Party              Bob, Suzie
3           Meeting            Bob, Joe

Result on my machine
EventId  Name                                       People
-------  ---------------------------------------    -----------------
1        Party                                      Bob, Joe, Suzie
2        Meeting                                    Bob, Suzie
3        Something else that wasn't mentioned       Bob, Joe

Are you sure that your sample output is correct? Smile | :)
Bastard Programmer from Hell Suspicious | :suss:

GeneralRe: GROUP BY and listing rows in an aggrigate Pin
nallelcm13-Dec-11 8:51
nallelcm13-Dec-11 8:51 
GeneralRe: GROUP BY and listing rows in an aggrigate Pin
Eddy Vluggen13-Dec-11 9:34
professionalEddy Vluggen13-Dec-11 9:34 
Questioncolums to single row Pin
Ramkumar_S12-Dec-11 18:29
Ramkumar_S12-Dec-11 18:29 
AnswerRe: colums to single row Pin
_Damian S_12-Dec-11 19:43
professional_Damian S_12-Dec-11 19:43 
AnswerRe: colums to single row Pin
SilimSayo13-Dec-11 3:13
SilimSayo13-Dec-11 3:13 
AnswerRe: colums to single row Pin
gvprabu22-Dec-11 1:45
gvprabu22-Dec-11 1:45 
Questionerror has occurred while establishing a connection Pin
Member 808991411-Dec-11 23:54
Member 808991411-Dec-11 23:54 
AnswerRe: error has occurred while establishing a connection Pin
Satheesh154612-Dec-11 1:20
Satheesh154612-Dec-11 1:20 
AnswerRe: error has occurred while establishing a connection Pin
thatraja12-Dec-11 1:58
professionalthatraja12-Dec-11 1:58 
QuestionSQL Server Management Studio R2 (varchar(MAX)) Pin
Framework .l.11-Dec-11 18:13
Framework .l.11-Dec-11 18:13 
AnswerRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Mycroft Holmes11-Dec-11 19:04
professionalMycroft Holmes11-Dec-11 19:04 
GeneralRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Framework .l.11-Dec-11 19:26
Framework .l.11-Dec-11 19:26 
GeneralRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Mycroft Holmes11-Dec-11 19:40
professionalMycroft Holmes11-Dec-11 19:40 
GeneralRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Framework .l.11-Dec-11 20:47
Framework .l.11-Dec-11 20:47 
GeneralRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Mycroft Holmes11-Dec-11 22:13
professionalMycroft Holmes11-Dec-11 22:13 
GeneralRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Framework .l.11-Dec-11 22:34
Framework .l.11-Dec-11 22:34 
AnswerRe: SQL Server Management Studio R2 (varchar(MAX)) Pin
Eddy Vluggen12-Dec-11 8:37
professionalEddy Vluggen12-Dec-11 8:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.