如何获得特定字段的值的总和

时间:2016-06-22 05:24:08

标签: mysql

 -------------------------------------------------
SELECT trtm, 
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
(SELECT 
  (SUM( tshares ) AS total WHERE trtm =  'TM'),
  (SUM( tshares ) AS total1 WHERE trtm =  'TR'))
FROM transfer_file
where t_date between '10/1/1992' and '10/2/1992'
 -----------------------------------------------

如何获取特定字段的值的总和,我得到了值的计数但没有得到值的总和,该怎么办?如果有任何人知道,请帮助我

1 个答案:

答案 0 :(得分:2)

您也应该在SUM内使用条件聚合

SELECT trtm, 
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
SUM(CASE WHEN trtm = 'TM' THEN tshares END) AS Total_Transmission,
SUM(CASE WHEN trtm = 'TR' THEN tshares END) AS Total_Transfer
FROM transfer_file
WHERE t_date between '10/1/1992' and '10/2/1992'