|
mchdba 发表于 2014-8-26 12:55 ![]()
如果要去重的功效,就直接用union 不要用union all就可以了。
我用的就是union啊,不知道为什么没去重呢,我写的是
select sum(t.counts),sum(t.txn_amount) from (select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_agent_1 ='08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_agent_2 ='08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_agent_3 = '08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and agent_cd_1 = '08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and agent_cd_2 = '08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and agent_cd_3='08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_ins='08A000S000') t;
说明一下啊,tm_agent_1,tm_agent_2,tm_agent_3,agent_cd_1,agent_cd_2,agent_cd_3,tm_ins这些字段不是只有一个有值,大部分时候有几个字段同时取一个值,少数情况下是只有一个字段取值
|
|