SELECT 来自另一个 table 的 MAX(OrderDate) 上的单位成本

SELECT UnitCost on MAX(OrderDate) from another table

我们有一个带有采购订单 header table (POHeader) 和采购订单行 table (PODetail) 的 ERP 系统。有数十万个 POLines,其中许多零件在几年内被反复购买。我需要找到自上次购买以来购买的每个零件的单位成本(采购订单的最大(订单日期))。

我一直在玩 SQL MAX(DATE) Order Number,对此我有几乎相同的要求。如果我们查看该线程中的解决方案代码:

SELECT t.ClientId, t.MaxDate, o.OrderNumber
FROM (SELECT ClientId, MAX(Date) as MaxDate
          FROM dbo.tblOrders
          GROUP BY ClientId) t
    INNER JOIN dbo.tblOrders o
        ON t.ClientId = o.ClientId
            AND t.MaxDate = o.Date

我的需求是相同的,我的部件号是他的 ClientId,我的单位成本是他的订单号。快速替换会给我这样的东西:

SELECT t.PartNum, t.MaxDate, o.UnitCost
FROM (SELECT PartNum, MAX(OrderDate) as MaxDate
          FROM POdetail
          GROUP BY PartNum) t
    INNER JOIN POdetail o
        ON t.PartNum = o.PartNum
            AND t.MaxDate = o.Date

难点在于采购订单日期存储在 header 级别,详细数据中没有日期。我必须在 POHeader table 的 max(OrderDate) 中加入 LEFT JOIN,这就是我遇到问题的地方。

我试图从 POHeader table 中复合 OrderDate,但我的代码是错误的。这是我停下来的地方。 有人可以帮我解决这个问题吗?:

SELECT d.PartNum, h.MaxDate, d.UnitCost
    FROM 
    PODetail d
    Left Join POHeader h ON 
    (SELECT PONum, MAX(OrderDate) as MaxDate
              FROM POHeader
              GROUP BY PONum) t
        INNER JOIN POHeader o
            ON t.PONum = o.PONum
                AND t.MaxDate = o.OrderDate

这里有一些 fool-around 数据...

create table POHeader (POnum int, OrderDate date);
insert into POHeader (PONum, OrderDate) values (12508, '05/13/2010');
insert into POHeader (PONum, OrderDate) values (12690, '06/04/2010');
insert into POHeader (PONum, OrderDate) values (12847, '09/08/2010');
create table PODetail (PONum int, PartNum varchar(15), Descript varchar(60), UnitCost numeric(10,2));
insert into PODetail (PONum, PartNum, Descript, UnitCost) values (12508, 'X37-BC055', 'PIPE, Titanium (MIL-T-9046), 3"OD x 8FT x .0125"', 6647.18);
insert into PODetail (PONum, PartNum, Descript, UnitCost) values (12690, 'X37-BC055', 'PIPE, Titanium (MIL-T-9046), 3"OD x 8FT x .0125"', 6651.90);
insert into PODetail (PONum, PartNum, Descript, UnitCost) values (12847, 'X37-BC055', 'PIPE, Titanium (MIL-T-9046), 3"OD x 8FT x .0125"', 6713.65);
insert into PODetail (PONum, PartNum, Descript, UnitCost) values (12690, 'N76-18824-ABN', 'COLLAR, Titanium (AMS 4902), 3"ID Flange', 564.13);
insert into PODetail (PONum, PartNum, Descript, UnitCost) values (12847, 'N76-18824-ABN', 'COLLAR, Titanium (AMS 4902), 3"ID Flange', 571.26);

历史数据可以看到,我买了3次8尺管子,两次买了领子。我正在努力达到这个结果——我最后一次购买每个零件时:

PartNum        Max Date   Last Price
-------------  ---------- ----------
X37-BC055      9/08/2010     6713.65
N76-18824-ABN  6/04/2010      571.26

感谢您的帮助, JM

使用支持最大 dbms

row_number()
   select * from
    (select ph.*,PartNum,UnitCost as lstPrice,row_number() over(partition by PartNum order by OrderDate desc) rn
   from POHeader ph INNER JOIN POdetail po
    ON ph.PONum = po.PONum
    ) a where a.rn=1

获取所有最后日期,然后加入表格:

select d.partnum PartNum, c.orderdate MaxDate, d.unitcost LastPrice
from podetail d 
inner join poheader h on h.ponum = d.ponum
inner join (
  select d.partnum, max(h.orderdate) orderdate
  from podetail d inner join poheader h
  on h.ponum = d.ponum
  group by d.partnum
) c on c.partnum = d.partnum and c.orderdate = h.orderdate