在具有条件的多个列上计数distinct

oprakyz7  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(342)

我需要做一个数对不同的情况下,我已经知道这里给出的答案,如果我只有一列多列。但如果我有多个列,如下所示:

SELECT COUNT(DISTINCT (CASE WHEN CustomerId > 10 THEN CITY,COUNTRY END))
    FROM Customers;

我得到的错误是

Error 1: could not prepare statement (1 near ",": syntax error)
n3ipq98p

n3ipq98p1#

我不知道你的专栏是什么,但你可以总是分组。

SELECT 
  COUNT(1), 
  concat(CITY, ",", COUNTRY) as location 
FROM Customers where CustomerId > 10 
GROUP BY location

基本上,这将计算每个城市和国家的独特组合的1的数量。
另外,如果要计算不同的位置,只需在case then子句中concat

SELECT COUNT(
  DISTINCT (CASE WHEN CustomerId > 10 
    THEN CONCAT(CITY,",",COUNTRY) END)
  ) 
FROM Customers;

出现“error 1:could not prepare statement(1 near”,“:syntax error)”是因为then子句需要1个不带逗号的arg

相关问题