我是SQL的新手,我遇到了这个。 Here's a screenshot
我的问题是,如果NULL
上的所有primary_payer_id
只有具有相同的ClientID和ActionCode IS NOT NULL
,才能获得上述值。仅供参考CustomColumn只是primary_payer_id的副本,我在下面附上了我的代码。
这是我的代码:
SELECT ci.client_id AS ClientID,
ci.primary_payer_id,
ci.effective_date AS EffectiveDate,
ci.action_code_id AS ActionCode,
cc.item_description AS ItemDesc,
ap.description AS IDescription,
ci.deleted
FROM census_item ci
LEFT JOIN common_code cc ON ci.adt_tofrom_id = cc.item_id
LEFT JOIN ar_lib_payers ap ON ci.primary_payer_id = ap.payer_id
WHERE ci.deleted = 'N'
答案 0 :(得分:1)
可能有一种更有效的方法,但这可行:
with t as (
<your query here>
)
select t.*,
(case when t.actioncode is not null and t.clientid is null
then tprev.clientid
else t.clientid
end) as new_clientid
from t outer apply
(select top 1 tprev.*
from t tprev
where tprev.clientid = t.clientid and
tprev.effectivedate < t.effectivedate
order by tprev.effecctivedate desc
) tprev;