select id, shelfno, sectionno,
iif(tableA.shelfno = tableB.promono, itemdesc + ' Best Sale Right Now!', itemdesc),
salesprice
from tableA
left join tableB on tableA.shelfno = tableB.promono
查询按原样运行,但是,我想知道是否有更好的方法来编写/编写它。我正在考虑使用外部应用,但它看起来不会起作用。
编辑:为了更清楚,我正试图从SELECT语句中删除IIF(与CASE相同)。
答案 0 :(得分:2)
没有CASE,没有IIF,按要求......
select id, shelfno, sectionno,
COALESCE(promodesc, itemdesc),
salesprice
from tableA A
OUTER APPLY (
SELECT
itemdesc + ' Best Sale Right Now!' AS promodesc
FROM tableA
INNER JOIN tableB on tableA.shelfno = tableB.promono
WHERE id = A.id
) B
答案 1 :(得分:1)
你可以使用CASE WHEN。
SELECT id
,shelfno
,sectionno
,itemdesc + CASE WHEN tableA.shelfno = tableB.promono
THEN ' Best Sale Right Now!'
ELSE '' END
,salesprice
FROM tableA
LEFT JOIN tableB
ON tableA.shelfno = tableB.promono
答案 2 :(得分:0)
这是你要找的吗?
case
tableB.promono is null then itemdesc
else itemdesc + ' Best Sale Right Now!'
end
答案 3 :(得分:0)
这假设左连接仅在if
中使用,并且tableB
中没有使用其他列(如果您将它们全部别名则会更清楚): -
SELECT id, shelfno, sectionno, itemdesc + CASE WHEN exists(
select *
from tableB
where tableB.promono = tableA.shelfno) THEN ' Best Sale Right Now!'
ELSE '' END as itemdesc,
salesprice
FROM tableA
或在Sql Server 2012中: -
SELECT id, shelfno, sectionno, itemdesc + iif(exists(
select *
from tableB
where tableB.promono = tableA.shelfno),' Best Sale Right Now!','') as itemdesc,
salesprice
FROM tableA