select a.采购单号,a.日期,b.物料编号,b.单价 from 采购单 a,采购单明细 b where a.采购单号=b.采购单号 and not exists ( select 1 from 采购单 a1,采购单明细 b1 where a1.采购单号=b1.采购单号 and b1.物料编号=b.物料编号 and a1.日期>a.日期 )
create table [采购单] ( [采购单号] char(32) ,[日期] datetime ) create table [采购单明细] ( [采购单号] char(32) ,[物料编号] char(32) ,[单价] numeric(10,2) )-- 每一种物料最近一次采购明细情况 select a.[采购单号],[日期],[物料编号],[单价] from [采购单] a,[采购单明细] b where a.[采购单号]=b.[采购单号] and exists (select 1 from ( select [物料编号], [日期] = max([日期]) from [采购单] a,[采购单明细] b where a.[采购单号]=b.[采购单号] ) as c where c.[物料编号]=b.[物料编号] and c.[日期]=a.[日期] )
select a.采购单号,a.日期,b.物料编号,b.单价
from 采购单 a,采购单明细 b
where a.采购单号=b.采购单号
and not exists (
select 1 from 采购单 a1,采购单明细 b1
where a1.采购单号=b1.采购单号
and b1.物料编号=b.物料编号
and a1.日期>a.日期
)
create table [采购单] (
[采购单号] char(32)
,[日期] datetime
)
create table [采购单明细] (
[采购单号] char(32)
,[物料编号] char(32)
,[单价] numeric(10,2)
)-- 每一种物料最近一次采购明细情况
select a.[采购单号],[日期],[物料编号],[单价]
from [采购单] a,[采购单明细] b
where a.[采购单号]=b.[采购单号]
and exists (select 1 from (
select [物料编号], [日期] = max([日期])
from [采购单] a,[采购单明细] b
where a.[采购单号]=b.[采购单号]
) as c
where c.[物料编号]=b.[物料编号] and c.[日期]=a.[日期]
)
运行正确
Time Space:
代码运行有误,不过还是谢谢
谢谢两位这么快答复!