sql server - SQL - Select statement inside case -
i want select particular columns table when field in other table not null.
in other words, have 2 tables, have common fields when field named "salesordrkey" in table number 1 not null need common fields table number 2 otherwise common fields table number 1
here's trying do
select slsordr.salesordrkey, whissue.warehissuekey, issuppk.issueprodpackkey, (case when whissue.salesordrkey not null (select slsordr.busipartnerkey, slsordr.contractkey, slsordr.salesmankey, slsordr.customerkey) else (select whissue.busipartnerkey, whissue.contractkey, whissue.salesmankey, slsordr.customerkey) end) warehissues whissue inner join issueprodpacks issuppk on whissue.warehissuekey = issuppk.warehissuekey left join slssalesordrs slsordr on whissue.salesordrkey = slsordr.salesordrkey whissue.partitionkey = @prtnkey , issuppk.prodpackkey = @prodpackkey
but didn't work .. there other way achieve that?
i assuming logic backwards. instead of:
case when whissue.salesordrkey not null . . .
i think intend:
case when slsordr.salesordrkey not null
the first doesn't check see if left join
working.
so, can use coalesce()
:
select slsordr.salesordrkey, whissue.warehissuekey, issuppk.issueprodpackkey, coalesce(slsordr.busipartnerkey, whissue.busipartnerkey) busipartnerkey, coalesce(slsordr.contractkey, whissue.contractkey) contractkey, coalesce(slsordr.salesmankey, whissue.salesmankey), coalesce(slsordr.customerkey, whissue.customerkey) warehissues whissue inner join issueprodpacks issuppk on whissue.warehissuekey = issuppk.warehissuekey left join slssalesordrs slsordr on whissue.salesordrkey = slsordr.salesordrkey whissue.partitionkey = @prtnkey , issuppk.prodpackkey = @prodpackkey
i should note not 100% same logic. possible records match have null
values in slsordr
table. using coalesce()
bring in values other table, though there match. however, logic.
otherwise, need use multiple case
statements:
(case when slsorderkey not null slsordr.busipartnerkey else whissue.busipartnerkey end) busipartnerkey,
case
expression. returns 1 value, not multiple values.
Comments
Post a Comment