我有一个SQL Select查询,用于返回从一个表到另一个表匹配的ID列以及一列不匹配的ID。查询中还有其他一些列,但它们在这里并不重要。 table2中有另一列(当前不在查询中),称为StatusId。如果StatusId = 3,我希望Ids只出现在匹配列中。否则,我希望它们出现在Unmatched Column中。听起来很简单,但我无法弄清楚如何使用WHERE
语句。
查询:
Select t2.Id as MatchedId,
case when t2.Id is null
then t1.Id end as UnmatchedId
, t2.Number,t1.Date
from Table1 t1
left join Table2 t2
on t2.Id = t1.Id;
答案 0 :(得分:2)
Select
case when t2.statusid = 3 then t2.Id end as MatchedId,
case when t2.statusid <> 3 then t1.Id end as UnmatchedId
,t2.Number,t1.Date
from Table1 t1
left join Table2 t2
on t2.Id = t1.Id;
这是你想要做的吗?