使用union计算3个表的总数

时间:2013-11-21 05:52:09

标签: sql

美好的一天,

我只是想查询我的查询,

我想得到每个表与单个国家的总和 这是我的表和所需的输出

这是我的查询

        select country,continent,(mycol2+mycol3+mycol4+mycol5+mycol6+mycol7+mycol8
   +mycol9+mycol10+mycol11+mycol12+mycol13+mycol14+mycol15+mycol16+mycol17
   +mycol18+mycol19+mycol20+mycol21+mycol22+mycol23+mycol14+mycol25+mycol26
   +mycol27+mycol27+mycol29+mycol30+mycol31) as total from tblWRSTopTargetVictim
        union 
        select country,continent,(mycol2+mycol3+mycol4+mycol5+mycol6+mycol7+mycol8
   +mycol9+mycol10+mycol11+mycol12+mycol13+mycol14+mycol15+mycol16+mycol17
   +mycol18+mycol19+mycol20+mycol21+mycol22+mycol23+mycol14+mycol25+mycol26
   +mycol27+mycol27+mycol29+mycol30+mycol31) as total from               tblFRSMalwareDetectionMap
        union 
        select country,continent,(mycol2+mycol3+mycol4+mycol5+mycol6+mycol7+mycol8
   +mycol9+mycol10+mycol11+mycol12+mycol13+mycol14+mycol15+mycol16+mycol17
   +mycol18+mycol19+mycol20+mycol21+mycol22+mycol23+mycol14+mycol25+mycol26
   +mycol27+mycol27+mycol29+mycol30+mycol31) as total from tblERSSpamVolumeMap

这里是根据上面的查询生成的结果,我想计算下面每个国家/地区的总结果是我要查看的结果

     country        continent       total
     AFGHANISTAN    Asia            6871
    AFGHANISTAN Asia            61479
    AFGHANISTAN Asia            257988
    ALAND ISLANDS   Europe          19
    ALAND ISLANDS   Europe          201
    ALBANIA         Europe          2340
    ALBANIA         Europe          9998
    ALBANIA         Europe          2612205
    ALGERIA         Africa          5675
    ALGERIA         Africa          84028
    ALGERIA         Africa          1435598
    AMERICAN        SAMOA   Oceania 8
    AMERICAN        SAMOA   Oceania 538
    AMERICAN        SAMOA   Oceania 43325
    ANDORRA         Europe          1341
    ANDORRA         Europe          3330
    ANDORRA         Europe          91466
    ANGOLA          Africa          8211
    ANGOLA          Africa          30009
    ANGOLA          Africa          846688
    ANGUILLA    North America   313
    ANGUILLA    North America   919

我希望输出

     country        continent          total
     AFGHANISTAN    Asia               326338
     ALAND ISLANDS  Europe             40
     ALBANIA        Europe             12338
     ALGERIA    Africa             1525301
     AMERICAN       SAMOA   Oceania    43871
     ANDORRA        EUROPE             96137
     ANGOLA         AFRICA             884908
     ........
     ........

1 个答案:

答案 0 :(得分:1)

是的,您必须使用分组。但是,我可以理解如何做到这一点并不是很明显:

SELECT country, continent, sum(total)
 FROM (
select country,continent,(mycol2+mycol3+mycol4+mycol5+mycol6+mycol7+mycol8   +mycol9+mycol10+mycol11+mycol12+mycol13+mycol14+mycol15+mycol16+mycol17   +mycol18+mycol19+mycol20+mycol21+mycol22+mycol23+mycol14+mycol25+mycol26   +mycol27+mycol27+mycol29+mycol30+mycol31) as total 
 from tblWRSTopTargetVictim
    union all
select country,continent,(mycol2+mycol3+mycol4+mycol5+mycol6+mycol7+mycol8   +mycol9+mycol10+mycol11+mycol12+mycol13+mycol14+mycol15+mycol16+mycol17   +mycol18+mycol19+mycol20+mycol21+mycol22+mycol23+mycol14+mycol25+mycol26   +mycol27+mycol27+mycol29+mycol30+mycol31) as total 
 from tblFRSMalwareDetectionMap
    union all
select country,continent,(mycol2+mycol3+mycol4+mycol5+mycol6+mycol7+mycol8   +mycol9+mycol10+mycol11+mycol12+mycol13+mycol14+mycol15+mycol16+mycol17   +mycol18+mycol19+mycol20+mycol21+mycol22+mycol23+mycol14+mycol25+mycol26   +mycol27+mycol27+mycol29+mycol30+mycol31) as total 
 from tblERSSpamVolumeMap
     ) as A
 GROUP BY country, continent