我有两个CTE,一个是当前时期,另一个是前一个时期。我想创建一个IF语句,将当前的查找ID与之前的查找ID进行比较,如果它们不匹配,我希望它属于"转出" 。但是,我在创建if语句时出错。任何建议将不胜感激。
;with transfer_current_mc as
(
--Current
select snapshot_dt, finding_id, mc_clean_name, mc_1_down_clean_name, 'current' as current_col from audit.archer_findings_snapshot
where snapshot_dt = @current_snapshot and snapshot_time_granularity= @Snapshot
),
transfer_previous_mc as
(
--Previous
select snapshot_dt, finding_id, mc_clean_name, mc_1_down_clean_name,'previous' as previous_col from audit.archer_findings_snapshot
where snapshot_dt = @previous_snapshot and snapshot_time_granularity = @Snapshot
)
select if a.finding_id <> b.finding_id else 'Transfer Out' end
from transfer_current_mc a inner join transfer_previous_mc b on a.finding_id = b.finding_id
答案 0 :(得分:1)
您需要CASE
语句,如下所示
select case when a.finding_id <> b.finding_id then some_column
else 'Transfer Out' end as Calculated_column
from transfer_current_mc a
inner join transfer_previous_mc b on a.finding_id = b.finding_id