我有这个问题:
SELECT
CASE
WHEN COUNT(DISTINCT Q.FIELD_1) > 1 AND MAX(Q.FIELD_1) <> 'N/A' THEN 'VALUE_010' ELSE 'VALUE_011' END AS FIELD_1,
CASE
WHEN COUNT(DISTINCT Q.FIELD_2) = 10 AND MAX(Q.FIELD_2) <> 'N/A' THEN 'VALUE_020'
WHEN COUNT(DISTINCT Q.FIELD_2) = 100 AND MAX(Q.FIELD_2) <> 'N/A' THEN 'VALUE_021'
ELSE 'VALUE_022' END AS FIELD_2
FROM (
SELECT
A.FIELD_1,
B.FIELD_2
FROM TABLE_A a
JOIN TABLE_B b
ON a.FIELD_X = b.FIELD_Y
GROUP BY
A.FIELD_1,
B.FIELD_2
) Q
我想要的是在select的CASE / WHEN条件中使用COUNT或MAX等聚合函数。实际上我得到错误:“&gt;附近的语法不正确”。 是否有可能或我应该尝试不同的方法?
答案 0 :(得分:1)
我认为这是因为您的代码中的FIELD_2之后有一个逗号
ELSE 'VALUE_022' END AS FIELD_2,
答案 1 :(得分:0)
在您的查询中,style
否则尝试
ELSE 'VALUE_022' END AS FIELD_2,
答案 2 :(得分:0)
试试这个
SELECT
CASE WHEN COUNT(DISTINCT a.FIELD_1) > 1 THEN 'VALUE_010' ELSE 'VALUE_011' END AS FIELD_1
,CASE
WHEN COUNT(DISTINCT b.FIELD_2) = 10 THEN 'VALUE_020'
WHEN COUNT(DISTINCT b.FIELD_2) = 100 THEN 'VALUE_021'
ELSE 'VALUE_022' END AS FIELD_2
FROM TABLE_A a
JOIN TABLE_B b
ON a.FIELD_X = b.FIELD_Y
GROUP BY A.FIELD_1, B.FIELD_2