作为输出的数据计数

时间:2016-09-20 13:28:34

标签: php mysql sql codeigniter

我的查询是:

  SELECT cat_name AS category_name, SUM( cat ) AS catgory_count, SUM( total ) AS category_sum, (
  SUM( total ) *100 /  '10942263'
  ) AS Percentage
   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

  -----------------------

我得到的输出为

  ----------------
       category_name          catgory_count             category_sum            Percentage
     Bank-Nationalised          1                            100            0.0009138877396750563
     Clearing Member            13                          2459                0.022472499518609634
     Individual- Director       1                           100             0.0009138877396750563
 Individual- Directors Relative 7                         139969                1.2791595303457794
   Individual- Promoters        1                         30000             0.2741663219025169
   Individual-Minor             1                             1             0.000009138877396750563
      ----------------------

但需要输出作为计数(个人 - 董事,个人 - 董事相对,个人 - 推动者)作为一个个人和计数9

我需要输出

     category_name        catgory_count             category_sum    
         One-Individual       9                        170069

我得到第一个输出后需要输出,如果有人知道帮助我

1 个答案:

答案 0 :(得分:1)

添加外层包装器,不必仔细检查是否所有初始查询都是必要的:

Select 'One-Individual' as category_name,
sum(category_count) as 'category_count',
sum(category_sum) as 'category_sum'
FROM
(
  SELECT cat_name AS category_name, SUM( cat ) AS category_count, SUM( total ) AS category_sum, (
  SUM( total ) *100 /  '10942263'
  ) AS Percentage
   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
) d
WHERE category_name IN ('Individual- Director','Individual- Directors Relative','Individual- Promoters');