将多行折叠为单行

时间:2017-12-14 17:04:58

标签: sql sql-server dynamic-sql

Sql不是我的强项......请考虑以下五个表......

   CREATE TABLE SMTickets (
id INT PRIMARY KEY IDENTITY,
summary NVARCHAR(MAX)
)

CREATE TABLE CUSTOMCONTROL (
    [id] INT PRIMARY KEY IDENTITY,
    [label] NVARCHAR(MAX),
)

CREATE TABLE CUSTOMCONTROL_RESPONSE (
    [id] INT PRIMARY KEY IDENTITY,
    [customcontrolid] INT CONSTRAINT FK_CUSTOMCONTROL_RESPONSE_customcontrolid FOREIGN KEY (customcontrolid)     
    REFERENCES CUSTOMCONTROL(id)
)

CREATE TABLE CUSTOMCONTROL_RESPONSE_VALUES (
    [id] INT PRIMARY KEY IDENTITY,
    [responseid] INT CONSTRAINT FK_CUSTOMCONTROL_RESPONSE_VALUES_responseid FOREIGN KEY (responseid) REFERENCES CUSTOMCONTROL_RESPONSE(id),
    [value] NVARCHAR(MAX)
)

CREATE TABLE CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT (
    [id] INT PRIMARY KEY IDENTITY,
    [responseid] INT CONSTRAINT FK_CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT_responseid FOREIGN KEY (responseid)     
    REFERENCES CUSTOMCONTROL_RESPONSE(id),
    [ticketid] INT CONSTRAINT FK_CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT_ticketid FOREIGN KEY (ticketid) 
    REFERENCES SMTickets(id)
)

INSERT INTO SMTickets (Summary) VALUES ('summary description')
INSERT INTO CUSTOMCONTROL (label) VALUES ('SINGLE')
INSERT INTO CUSTOMCONTROL (label) VALUES ('MULTIPLE')
INSERT INTO CUSTOMCONTROL_RESPONSE (customcontrolid) VALUES (1)
INSERT INTO CUSTOMCONTROL_RESPONSE (customcontrolid) VALUES (2)
INSERT INTO CUSTOMCONTROL_RESPONSE_VALUES(responseid, value) VALUES (1, 'single option')
INSERT INTO CUSTOMCONTROL_RESPONSE_VALUES(responseid, value) VALUES (2, 'option 1')
INSERT INTO CUSTOMCONTROL_RESPONSE_VALUES(responseid, value) VALUES (2, 'option 2')
INSERT INTO CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT(responseid, ticketid) VALUES(1, 1)
INSERT INTO CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT(responseid, ticketid) VALUES(2, 1)
INSERT INTO CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT(responseid, ticketid) VALUES(2, 1)

响应可能有很多响应值...... 当我执行以下查询

declare @custom nvarchar(max)
declare @sql nvarchar(max)

set @custom = STUFF((
            SELECT ' , min(Case label WHEN ''' +  (label)  + ''' THEN value END) ' +  QUOTENAME(label) 
            FROM CUSTOMCONTROL
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @sql = '
select smtickets.id, summary
' + @custom + '
from smtickets
left join CUSTOMCONTROL_RESPONSE_TICKET_ASSIGNMENT ta on ta.ticketid = smtickets.id
left join CUSTOMCONTROL_RESPONSE r on r.id = ta.responseid
left join CUSTOMCONTROL_RESPONSE_VALUES rv on rv.responseid = r.id
left join CUSTOMCONTROL c on c.id = r.customcontrolid
group by smtickets.id, summary
order by smtickets.id '

exec(@sql)

我得到以下结果

ticket_id   summary                 Single          Multiple
3335     [Asset Monitoring Events]  Medium Impact   Option 1

我想得到以下结果

ticket_id   summary                 Single          Multiple
3335     [Asset Monitoring Events]  Medium Impact   Option 1, Option 2

我已尝试在值上添加联接,但这不起作用......

0 个答案:

没有答案