2
Guys, how to add the query below as a subconsulta.
Consultation 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 place the query from above, because another column will be generated with the 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)
Demonstrate some example of the result you expect, only with what is exemplified by
query
I can’t understand what you want.– Sorack
@Renanbessa: In the first code, the junction between the tables
tarefa
andtipo
is of the type LEFT JOIN but the presence of the restrictionc.tipdescricao LIKE 'Habilita%'
turns it into INNER JOIN. This is just what you need?– José Diz