繁体   English   中英

SQL 表/MySQL 中不同列的 COUNT 总和

[英]SUM of COUNT on distinct columns in SQL Table / MySQL

我正在尝试基于 SQL 表上特定列的多个不同计数执行 SUM。 到目前为止的问题是我无法执行这个求和,而我认为合成器会没问题。 但似乎不是因为我经常从这个查询中得到一个问题。

 select SUM(`s1`.`t1`) from (
select
COUNT(DISTINCT s1_global) from NPS_deploiement_synthese as s1 where hubspot_company_id = 2436352252
union
select
COUNT(DISTINCT s2_global) from NPS_deploiement_synthese as s1 where hubspot_company_id = 2436352252
) as t1;

您根本不需要子查询:

select COUNT(DISTINCT s1_global) + COUNT(DISTINCT s2_global)
from NPS_deploiement_synthese s
where hubspot_company_id = 2436352252;

可以想象您的版本可以工作,但子查询需要列别名。

您可以添加 2 count():

select
  COUNT(DISTINCT s1_global) + COUNT(DISTINCT s2_global) 
from NPS_deploiement_synthese as s1 
where hubspot_company_id = 2436352252

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM