我有这个问题:
SELECT i.d, COUNT(id) AS dr, COUNT(id2) AS dn, SUM(eq) AS eq_sum, COUNT(thx) AS thx_count
FROM dsd
INNER JOIN
(
SELECT COUNT(id) AS d FROM ds
) i
现在我想只将WHERE用于WHERE thx =“y”之类的thx
列,这样它只会计算所有带“y”的值。
但是如果我只是在查询结尾处添加WHERE,它将影响其他列,我不想这样做。
怎么做?
答案 0 :(得分:0)
然后使用COUNT(thx) AS thx_count
表达式将您的CASE
更改为
COUNT(CASE WHEN thx = 'y' THEN 1 ELSE 0 END) AS thx_count
(OR)
SUM(CASE WHEN thx = 'y' THEN 1 ELSE 0 END) AS thx_count