我需要对有条件的多个列进行计数 如果我只有1列,我已经知道给定here的答案。但是,如果我有多个列,如下所示:
SELECT COUNT(DISTINCT (CASE WHEN CustomerId > 10 THEN CITY,COUNTRY END))
FROM Customers;
我得到的错误是
Error 1: could not prepare statement (1 near ",": syntax error)
答案 0 :(得分:1)
我不确定您的专栏是什么,但您始终可以将其分组。
SELECT
COUNT(1),
concat(CITY, ",", COUNTRY) as location
FROM Customers where CustomerId > 10
GROUP BY location
基本上,这将对CITY和COUNTRY的每个唯一组合计算1的数量。
此外,如果您想计算不同的位置,只需在CASE THEN子句中合并即可
SELECT COUNT(
DISTINCT (CASE WHEN CustomerId > 10
THEN CONCAT(CITY,",",COUNTRY) END)
)
FROM Customers;
出现“您的错误1:无法准备语句(“,”附近的1:语法错误)“是因为THEN CLAUSE期望1个无逗号的参数