我有分组选择的例子:
SELECT cod, value, type
FROM table
GROUP BY cod, value, type
我得到了结果:
cod | value | type
1 | 1000 | A
1 | 570 | B
2 | 2000 | A
3 | 1500 | A
3 | 200 | B
但只有cod = 1
才能从type = A
返回值。期望的结果:
cod | value | type
1 | 1000 | A
2 | 2000 | A
3 | 1500 | A
3 | 200 | B
我认为CASE
中的WHERE
声明但有限制但无效。我怎么能这样做?
WHERE CASE WHEN cod = 1 THEN type = 'A' END
答案 0 :(得分:2)
你在这里:
SELECT cod, value, type
FROM table
WHERE cod != 1 OR type='A'
GROUP BY cod, value, type
答案 1 :(得分:1)
WHERE Type = CASE WHEN cod = 1 THEN 'A' else Type END
答案 2 :(得分:0)
另一个不错的选择 - 不是这里最好的方式,而是一个有用的选择。
SELECT cod, value, type
FROM table
GROUP BY cod, value, type
EXCEPT
SELECT cod, value, type
FROM table
WHERE cod = 1 AND type <> 'A'
GROUP BY cod, value, type