我有这个查询:
SELECT
c.br,
c.no_,
CONCAT(c.name, ' ', c.surname) AS Name,
t.type,
t.code,
t.sub,
GETDATE() AS date,
c2.name
FROM
c
INNER JOIN t on c.rowno = t.rowno
LEFT JOIN termjnt on t.rowno = termjnt.rowno
LEFT JOIN c c2 on termjnt.rowno_c2 = c2.rowno
WHERE
t.status = 'active'
这是一个输出示例
如何将每个no_的c2.name行合并? 例如,我希望单元格为“ Humphry,Heather”,而不是Heather和Humphry是不同的行
答案 0 :(得分:0)
您可以在此处使用自连接,但问题是确定显示名称的顺序。为此,我仅使用字母名称顺序来确定序列:
WITH TEST
AS
(
SELECT
NAME = C.NAME+' '+C.SURNAME,
NO_,
ROW_NUMBER() OVER (PARTITION BY NO_ ORDER BY C.NAME+' '+C.SURNAME DESC) SEQ
FROM C
)
SELECT TEST.NO_, NAMECONCAT = TEST.NAME+', '+TEST2.NAME
FROM TEST
LEFT JOIN TEST TEST2
ON TEST.NO_ = TEST2.NO_
AND TEST.SEQ = TEST2.SEQ-1