对count的总值求和

wljmcqd8  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(266)

我知道怎么做 count 或采取 sum 但我的问题不同。

上面是我的名单。我想把我所有的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` 列同时显示计数。
任何帮助都将不胜感激。
s5a0g9ez

s5a0g9ez1#

或者简单地添加表达式

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;

相关问题