如何结合并获得三个表的总和

时间:2016-08-10 06:56:49

标签: mysql sql oracle join union-all

我的查询

     ------------------------------------------
     Select cat_name,cat,total
       From
          (
            SELECT a.category_name as cat_name,
             Count (a.category_name) as cat,
                   Sum( b.position) as total
              FROM erom.category a, erom_kmch.nsdl b
             WHERE a.bene_type_nsdl = b.bene_type 
               AND a.bene_stype_nsdl= b.bene_stype
               And b.date ='2016-07-22' 
          Group By cat_name
         UNION All
            SELECT a.category_name as cat_name,
                   Count(a.category_name) as cat,
                     Sum( b.shares) as total
              FROM erom.category a, erom_kmch.cdsl b
             WHERE a.type_cdsl = b.type
               AND a.bo_substat_cdsl= b.bo_substat
               And b.date ='2016-07-22' group by cat_name
         UNION All
            SELECT a.category_name as cat_name,
                   Count(a.category_name) as cat,
                     Sum( b.shares) as total
             FROM erom.category a, erom_kmch.member_member_master b
            WHERE a.substatus_phy = b.substatus
              And b.date ='2016-07-22' 
              And shares > '0' 
         Group By cat_name 
        ) 
       c Group By cat_name 
      ------------------------------------

返回

     ----------------------------
       cat_name             cat       total
      Resident Individual   2705      2317048
     ---------------------------------------

如果我分别运行三个表,我将得到输出为

      ----------------------------------------
        cat_name             cat       total
      Resident Individual   2705      2317048
      Resident Individual    991      355218
      Resident Individual   3284      1219027
      ---------------------------------------

但我需要输出为

       --------------------------
        cat_name                 cat       total
      Resident Individual        6980     3891293
      -----------------------------------

我试图在表格中获得字段名称的总体计数,但我只得到第一张表的答案。帮我找到解决方案。

2 个答案:

答案 0 :(得分:2)

您的查询的第一行更改为

select cat_name, sum(cat) as cat, sum(total) as total

答案 1 :(得分:1)

请尝试这个:

select cat_name,sum(cat) as cat,sum(total) as total
     from
     (SELECT a.category_name as cat_name,
     count(a.category_name) as cat,
     sum( b.position) as total
     FROM erom.category a, erom_kmch.nsdl b
     WHERE a.bene_type_nsdl = b.bene_type 
     AND 
     a.bene_stype_nsdl= b.bene_stype
     And
     b.date ='2016-07-22' group by cat_name
     UNION all
     SELECT a.category_name as cat_name,
     count(a.category_name) as cat,
     sum( b.shares) as total
     FROM erom.category a, erom_kmch.cdsl b
     WHERE a.type_cdsl = b.type
     AND 
     a.bo_substat_cdsl= b.bo_substat
     And
     b.date ='2016-07-22' group by cat_name
     UNION all
     SELECT a.category_name as cat_name,
     count(a.category_name) as cat,
     sum( b.shares) as total
     FROM erom.category a, erom_kmch.member_member_master b
     WHERE a.substatus_phy = b.substatus
    And
    b.date ='2016-07-22' And shares > '0' group by cat_name ) 
   c group by cat_name 

您还需要在主查询中求和