People, like adding the query below as a subquery.
Query with WITH
:
WITH tabela (projeto, operadora) AS (
SELECT p.projdesc,
tt.tartitulo
FROM tarefa tt
INNER JOIN projetos p ON p.projid = tt.projid
LEFT JOIN tipo c ON c.tipid = tt.tartipid
WHERE tt.modid = 181
AND tt.tarstatus <> 9
AND (tt.tartipid BETWEEN 867 AND 934
OR tt.tartipid IN (1004, 1034))
AND c.tipdescricao LIKE 'Habilita%')
SELECT r.projeto,
STUFF((SELECT ', ' + t.operadora
FROM tabela t
WHERE t.projeto = r.projeto
FOR XML PATH(''), TYPE).value('(./text())[1]', 'VARCHAR(MAX)'), 1, 2, '') AS operadoras
FROM tabela r
GROUP BY projeto;
Another query, where this is to put the query from above, because another column will be generated with query
above.
SELECT DISTINCT(pp.projdesc),
c.compdesc qtdecnpjs,
u1.usuuf
FROM projetos pp
INNER JOIN tarefa t ON pp.projid = t.projid
INNER JOIN usuario u ON t.usuidresponsavel = u.usuid
INNER JOIN usuario u1 ON t.usuidcliente = u1.usuid
LEFT JOIN statusaux s ON t.tarstatus = s.statusid
LEFT JOIN complemento c ON c.usuid = t.usuidcliente
AND c.compid = 1
WHERE t.macprocid = 33
AND t.tartipid IN (866)