比较diff表中的日期并获取最新日期

时间:2015-03-18 15:36:07

标签: db2

我有3张桌子。

SALES 交易号 salesDate 顾客号码 employerNumbet

的salesdetail transactionNumbet 产品代码 量

PRICEHISTORY effectivitydate 产品代码 单价

我想比较产品的销售日期和有效日期,以获得最新的单位价格。

1 个答案:

答案 0 :(得分:-1)

Select UnitPrice
From Sales s
Inner join SalesDetail sd on s.transactionNumber = sd.transactionNumber
Inner join PriceHistory ph on ph.ProductCode = sd.ProductCode
Where salesDate =
(
    Select Max(salesDate)
    From Sales s
    Inner join SalesDetail sd on s.transactionNumber = sd.transactionNumber
    Inner join PriceHistory ph on ph.ProductCode = sd.ProductCode
)