Select
pnf_notas_processadas.pnf_notas_dest_cnpj_cpf,
Count(Distinct pnf_notas_processadas.pnf_notas_nnf) As destinadas,
Sum(0) As emitidas
From
pnf_notas_processadas
Group By
pnf_notas_processadas.pnf_notas_dest_cnpj_cpf
Having
pnf_notas_processadas.pnf_notas_dest_cnpj_cpf In ('03846642000102',
'03846642000285', '03846642000447', '03846642000528')
Union
Select
pnf_notas_processadas.pnf_notas_emit_cnpj_cpf,
Sum(0) As destinadas,
Count(Distinct pnf_notas_processadas.pnf_notas_nnf)
From
pnf_notas_processadas
Group By
pnf_notas_processadas.pnf_notas_emit_cnpj_cpf
Having
pnf_notas_processadas.pnf_notas_emit_cnpj_cpf In ('03846642000102',
'03846642000285', '03846642000447', '03846642000528')
结果:
我对查询的期望
答案 0 :(得分:0)
一种方法是一步完成聚合。但是,您需要以某种方式聚合两个字段。
这是一种方法。基本上,行是"加倍"然后聚合拉出正确的值:
select (case when which = 1 then np.pnf_notas_dest_cnpj_cpf else pnf_notas_emit_cnpj_cpf end) as cnpj_cpf,
count(distinct case when which = 1 then np.pnf_notas_nnf end) as destinadas,
count(distinct case when which = 2 then np.pnf_notas_nnf end) as emitidas
from pnf_notas_processadas np cross join
(values (1), (2)) as v(which)
where np.pnf_notas_dest_cnpj_cpf In ('03846642000102', '03846642000285', '03846642000447', '03846642000528')
Group By cnpj_cpf;
答案 1 :(得分:0)
我调整了查询
select (case when which = 1 then pnf_notas_processadas.pnf_notas_dest_cnpj_cpf else pnf_notas_processadas.pnf_notas_emit_cnpj_cpf end) as cnpj_cpf,
count(distinct case when which = 1 then pnf_notas_processadas.pnf_notas_nnf end) as destinadas,
count(distinct case when which = 2 then pnf_notas_processadas.pnf_notas_nnf end) as emitidas
from pnf_notas_processadas cross join
(values (1), (2)) as v(wich)
where pnf_notas_processadas.pnf_notas_dest_cnpj_cpf In ('03846642000102', '03846642000285', '03846642000447', '03846642000528')
Group By cnpj_cpf;
但是返回了此错误
选择句子使第5行,第16行的意外元素“2”无效