需要两个相似表的合计结果而不会重复。
表具有相似的结构。 (日期,帐单编号,载体编号,持续时间...) 表格是2019-01-29和2019-01-30
我尝试了相同查询的UNION,但是得到了重复的条目。
select billed_number, sum(CASE WHEN [account_billed_duration] > 0 THEN 1 END) AS completed
from alex4..[2019-01-29]
where billed_number = '702640'
and carrier_id = 171
group by billed_number
union all
select billed_number, sum(CASE WHEN [account_billed_duration] > 0 THEN 1 END) AS completed
from alex4..[2019-01-30]
where billed_number = '702640'
and carrier_id = 171
group by billed_number
在此示例中,我仅使用表格([2019-01-29]和[2019-01-30]) 我还需要能够合并两个以上的表。
结果: billed_number,已完成 702640 3735 702640 3539
预期结果: billed_number,已完成 702640 7274
答案 0 :(得分:0)
使用联合工会不确实的所有与重复结合所有结果
答案 1 :(得分:0)
感谢您的答复。实际上,我同时尝试了UNION和UNION ALL,并获得了相同的结果。
我昨晚起床,直到我弄清楚了。 子查询可以解决问题...
SELECT distinct billed_number , sum(completed)as comp , FROM (
SELECT [billed_number],
sum(CASE WHEN [account_billed_duration] > 0 THEN 1 END) AS completed,
sum(CASE WHEN [account_billed_duration] = 0 THEN 1 END) AS failed
FROM alex4..[2019-01-29]
where billed_number = '702640'
and carrier_id = 171
group by billed_number --,account_billed_duration
UNION
SELECT [billed_number],
sum(CASE WHEN [account_billed_duration] > 0 THEN 1 END) AS completed,
sum(CASE WHEN [account_billed_duration] = 0 THEN 1 END) AS failed
FROM alex4..[2019-01-30]
where billed_number = '702640'
and carrier_id = 171
group by billed_number --, account_billed_duration
) Q
group by billed_number
再次感谢您的回复:-)