我有这个SQL查询:
SELECT date,
CASE WHEN h_score > v_score
THEN
ho_t_id
ELSE
v_t_id
END
FROM Games
我想在WHERE子句中使用所选的值(CASE WHEN语句)。 我尝试做类似的事情:
SELECT date,
CASE WHEN h_score > v_score
THEN
ho_t_id
ELSE
v_t_id
END AS T
FROM Games
WHERE T = '1234'
这(当然)不起作用。
我该怎么做?
谢谢!
答案 0 :(得分:1)
您可以在CTE中查询并将where子句放在一边。
with cte
as
(
SELECT date,
CASE
WHEN h_score > v_score
THEN ho_t_id
ELSE v_t_id
END AS T
FROM Games
)
select * from cte where T = '1234
答案 1 :(得分:1)
SELECT date, T FROM (
SELECT date,
CASE WHEN h_score > v_score
THEN
ho_t_id
ELSE
v_t_id
END AS T
FROM Games
) g
WHERE g.T = '1234'
答案 2 :(得分:0)
只需在CASE
子句中重复WHERE
:
SELECT date,
CASE WHEN h_score > v_score
THEN
ho_t_id
ELSE
v_t_id
END AS T
FROM Games
WHERE (CASE WHEN h_score > v_score
THEN
ho_t_id
ELSE
v_t_id
END) = '1234'