【发布时间】:2018-09-23 23:46:58
【问题描述】:
我在 SQL Server 中有一个 products 表,如下所示:
ID ItemCode ProductName
------ ------------ -----------------
1 8680972060039 test product
2 123457 coffee
3 123458 tea
我有一个price 表用于这些产品,如下所示:
PriceListLineID ItemCode Price PriceGroupCode LastUpdatedDate
------------------------------------ -------------- --------- -------------- ------------------------
D7B4F7A5-3CEF-4FFB-B90A-A81700D6CCE8 8680972060039 79,00 B2C 2017-10-25 15:44:30.640
3456748E-FA54-4683-83E7-A81700D6CC40 8680972060039 75,00 B2B 2017-10-25 15:44:30.117
CF03A676-4276-432B-BA7B-A76A0128D9B6 8680972060039 124,00 B2C 2017-05-05 20:47:05.197
5257C168-365A-475E-B2FD-A76A0128D8EF 8680972060039 118,00 B2B 2017-05-05 20:47:04.630
我想显示最近更新的 b2c 和 b2b 价格以及产品详细信息,例如
ItemCode ProductName B2BPrice B2CPrice
------------ ------------- --------- -----------
8680972060039 test product 79,00 75,00
我使用一个内部连接来获取价格,但我无法获得第二个价格,我该怎么做?
这是我使用的 SQL 代码:
select
Products.ItemCode, Products.ProductName, Prices.Price
from
Products WITH(NOLOCK)
inner join
Prices with(nolock) on Prices.ItemCode = Products.ItemCode
and Prices.PriceGroupCode = 'B2B'
and Prices.Price is not null
order by
Prices.LastUpdated desc
【问题讨论】:
标签: sql sql-server merge inner-join