一个奇怪的问题,有没有一种方法可以查找值列表,而对于不存在的值列表,而是使用别名并对此进行查找。

问题描述 投票:0回答:0

ITEMCODE

alternatecode

xxxx树树 2000home2100sled5000技术 75006350因此说这是下面的数据ITEMCODEPrice
abcd 1200
1800
贝尔

abcd9.75xxxx10.00树树 21.00210015.50635012.75如果我有一个查询,我只参考主列中的项目代码 我需要结果来寻找别名并以价格返回主要代码,结果低于ITEMCODE
11.05
sled 17.12
技术
SELECT itemcode, price FROM pricing WHERE itemcode in ('abcd','xxxx','bark','home','sled','tech','bell')
Price


abcd

9.75树树 11.05sled17.12技术 21.0015.5012.75有一种方法可以在顶部宣布匹配的方式? thanks!左将加入与别名表以获取丢失的项目代码;我们使用左加入,因为我们希望结果即使没有匹配,也可以包含每个行的定价。匹配项目代码与替代方案。 select * from pricing p left join alternate_codes ac on ac.alternate_code = p.itemcode 现在我们将每一行都与其替代品匹配。 coalesce
xxxx 10.00
home
贝尔
启动
现在选择正确的项目代码。如果定义了备用_codes.itemcode,我们应该使用它,否则我们使用pricing.itemcode。使用
选择第一个非零列。
SELECT coalesce(ac.itemcode, p.itemcode) as itemcode, price FROM pricing p LEFT JOIN alternate_codes ac ON ac.alternate_code = p.itemcode WHERE coalesce(ac.itemcode, p.itemcode) in ('abcd','xxxx','bark','home','sled','tech','bell')

postgresql alias
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.