我如何在历史表中获得正确的行

时间:2017-06-03 21:08:19

标签: sql sql-server tsql

我有价格历史表:

| StrartDat | ProductID | Price | EndDate|

产品表:

| ProductID | Name | quantity|...|

订单表:

| OrderID |ProductID | date|...|

现在我正在尝试获取订单列表,但我得到一个产品的两行,其旧价格和新价格(其中enddate为空)

我如何能够逃避这种情况并以订购日期产品的价格获得订单。

我试过这个约束,但它并没有接受与enddate为null的价格相关联的订单

  Where EndDate > OrderCustm.Date AND StartDate <= OrderCustm.Date

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需添加明确的NULL条件:

Where StartDate <= OrderCustm.Date AND
      (EndDate > OrderCustm.Date OR EndDate IS NULL)