簡體   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