在单列中连接行

时间:2019-07-19 08:42:20

标签: sql sql-server join select

我正在建立一个查询,我需要将一列的值连接到一行中

我使用STRING_AGG函数对其进行了管理,但不幸的是,我要使用存储过程的数据库不是2017版,所以我正在尝试一些东西,但无法正常工作

select w1.idQuestionario, w1.nominativo, w1.media, w1.valutazione, count(w1.risposto) as Funzionari, (stuff((select a.peso for xml path('')), 1, 0, ',')) as peso, (stuff((select a.titolo for xml path('')), 1, 0, ',')) from (
    select w.nominativo, w.idQuestionario, w.risposto, sum(w.valore) / convert(float, count(w.Domande)) as media, w.valutazione
    from (
    select u.nominativo, q.idQuestionario, q.nome, d.idDomanda as Domande,  r.peso , ur.matricola as risposto, 1 * r.peso as valore, sum(us.valutazione) / convert(float, count(us.idSezione)) as valutazione
    from Questionario q 
    join Domanda d ON d.idQuestionario = q.idQuestionario
    join Risposta r ON r.idDomanda = d.idDomanda
    join rUtenteRisposta ur ON ur.idRisposta = r.idRisposta
    join Utente u ON u.matricola = ur.matricola
    left join rUtenteSezione us ON us.idQuestionario = q.idQuestionario AND us.matricola = u.matricola
    where q.cancellato = 0
    and q.idQuestionario = '111222'
    and q.anonimo = 0
    group by u.nominativo, q.idQuestionario, q.nome, d.idDomanda, r.peso, ur.matricola
    ) w
    group by w.idQuestionario,w.risposto,w.nominativo,w.valutazione ) w1
    join (select r.peso, d.titolo as titolo, u.nominativo, r.creazione from Domanda d
    join Questionario as q ON q.idQuestionario = d.idQuestionario
    join Risposta as r ON r.idDomanda = d.idDomanda
    join rUtenteRisposta as ur on ur.idRisposta = r.idRisposta
    join utente u ON u.matricola = ur.matricola
    where q.idQuestionario = '111222'
    and q.cancellato = 0
    and q.anonimo = 0) a ON a.nominativo = w1.nominativo
    group by w1.idQuestionario, w1.media, w1.nominativo, w1.valutazione, a.peso, a.titolo, a.creazione
    order by a.creazione

我在得到东西之前就得到了这个结果:

column1 column2 column3 column4 column5 column6
aaaaa   bbbbb   0,2     6       1       how are you?
aaaaa   bbbbb   0,2     6       2       did you eat?
ccccc   dddddd  0,5     7       1       how are you?
ccccc   dddddd  0,5     7       1       did you eat?

现在有了东西,我得到了:

column1 column2 column3 column4 column5 column6
aaaaa   bbbbb   0,2     6       ,<columns5>1,</columns5>       ,<columns6>how are you?,</columns6>
aaaaa   bbbbb   0,2     6       ,<columns5>1,</columns5>       ,<columns6>did you eat?,</columns6>
ccccc   dddddd  0,5     7       ,<columns5>1,</columns5>       ,<columns6>how are you?,</columns6>
ccccc   dddddd  0,5     7       ,<columns5>1,</columns5>       ,<columns6>did you eat?,</columns6>

但是我想达到这个结果:

column1 column2 column3 column4 column5 column6
aaaaa   bbbbb   0,2     6       1, 2    how are you?, did you eat?
ccccc   dddddd  0,5     7       1, 1    how are you?, did you eat?

数据库是SQL Server 2012和2014。

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

替换以下内容:

STUFF((SELECT a.peso FOR XML PATH('')), 1, 0, ',')

STUFF((SELECT ',' + a.peso FOR XML PATH('')), 1, 1, '')

由于您的表达式只是一列,因此将使用标签<peso>来包装XML中的值。另外,您还需要在XML开头添加逗号,并从字符串中删除第一个字符(而不仅仅是在开头添加一个逗号)。

最后,如果您的专栏a.peso可能包含特殊字符,那么我建议以下内容:

STUFF((SELECT ',' + a.peso FOR XML PATH(''),TYPE).value('.','nvarchar(MAX)'), 1, 1, '')