求计数的总值

时间:2018-05-09 14:46:59

标签: mysql sql count sum multiple-columns

我知道如何count或采取sum,但我的问题不同。

enter image description here

以上是我的点数清单。我想将我的所有3列计数加起来,例如7+68+13....= sum

SELECT COUNT(DISTINCT od.`meter_serial`) AS 'OGP Created', 
COUNT(DISTINCT mp.`meter_id`) AS 'Installed & Un-Verified Meters', 
COUNT(DISTINCT ins.`meter_msn`) AS 'Installed & Verified', 
sd.`sub_div_code` AS 'SD Code',sd.`name` AS 'SD-Name'
FROM `ogp_detail` od
INNER JOIN `survey_hesco_subdivision` sd ON od.`sub_div` = sd.`sub_div_code`
LEFT JOIN `meter_ping` mp ON od.`meter_id` = mp.`meter_id`
LEFT JOIN `installations` ins ON od.`meter_serial` = ins.`meter_msn`
WHERE od.`meter_type` = '3-Phase'
GROUP BY sd.`name`

我希望计数总和显示在每个3列的下方,同时显示计数。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

只需添加表达式

即可
COUNT(DISTINCT od.meter_serial) + 
COUNT(DISTINCT mp.meter_id) + 
COUNT(DISTINCT ins.meter_msn) AS total

或者使您的查询成为子查询:

select
  "OGP Created", 
  "Installed & Un-Verified Meters",
  "Installed & Verified",
  "OGP Created" + "Installed & Un-Verified Meters" + "Installed & Verified" as total
from ( your query here ) q;