这是我的查询,但它显示结果不是我预期的。
SELECT
a.PROJECT_NO,
CASE WHEN a.PROJECT_NO = b.PROJECT_NO THEN 'TRUE' ELSE 'FALSE' END AS CONDITION
FROM PROJECT a
LEFT OUTER JOIN WORKSHEET b ON (b.PROJECT_NO = a.PROJECT_NO)
Table a Table b
Project_No Project_No
1111 1111
2222 3333
3333
4444
正确的结果应该是
Project_No Condition
1111 True
2222 FALSE
3333 TRUE
4444 FALSE
我的查询结果,所有条件状态显示为真
Project_No Condition
1111 TRUE
2222 TRUE
3333 TRUE
4444 TRUE
任何人都可以帮我修理它。谢谢
答案 0 :(得分:2)
a.PROJECT_NO = b.PROJECT_NO
始终在null
时返回b.PROJECT_NO = null
,而WHEN
中的第一个CASE
选择返回TRUE
,否则,b.PROJECT_NO
时NULL
由于正确的相等结果,1}}不会返回TRUE
IS NULL
。因此,最简单的方法是将t2.Project
条件用于SELECT a.PROJECT_NO
, (CASE WHEN b.PROJECT_NO IS NULL THEN 'TRUE' ELSE 'FALSE' END) AS CONDITION
FROM PROJECT a LEFT OUTER JOIN WORKSHEET b ON b.PROJECT_NO = a.PROJECT_NO
列:
{{1}}
答案 1 :(得分:1)
用COALESCE
COALESCE(CASE WHEN a.PROJECT_NO = b.PROJECT_NO THEN 'TRUE' END, 'FALSE')
答案 2 :(得分:0)
您可以尝试 ISNULL
试试这个
SELECT
a.PROJECT_NO,
CASE WHEN (ISNULL(a.PROJECT_NO,0) = IsNULL(b.PROJECT_NO,1) THEN 'TRUE' ELSE 'FALSE' END AS CONDITION
FROM PROJECT a
LEFT OUTER JOIN WORKSHEET b ON (b.PROJECT_NO = a.PROJECT_NO)
答案 3 :(得分:0)
我认为这样做会让你不再需要加入
选择
a.PROJECT_NO,
当a.PROJECT_NO IN(从工作表中选择PROJECT_NO)那么' TRUE' ELSE' FALSE'结束条件
FROM PROJECT a
答案 4 :(得分:0)
另一种方式:)
SELECT a.PROJECT_NO,
COALESCE (CONDITION,'FALSE') AS CONDITION FROM
PROJECT a
OUTER APPLY
(
SELECT 'TRUE' AS CONDITION
FROM WORKSHEET
WHERE PROJECT_NO = a.PROJECT_NO
) AS Sub
谢谢, barua.me