我有一个子查询,它从一个表中获取项目编号,并在where子句中使用它来从主表中获取结果。但是,子查询不会返回正确的结果。这是查询和示例:
,(
select TOP 1[UNITPRCE]FROM [NIMET].[dbo].[SOP10200]
where [NIMET].[dbo].[SOP10200].ITEMNMBR=tblJobHold.ITEMNMBR
order by [NIMET].[dbo].[SOP10200].ReqShipDate desc
)
ITEMNMBR Price
07-0008 0.48000
20232001 0.00000
22241457 0.00000
67181001 0.00000
LR56230-095 2.36000
S-AF509 0.59000
S24A7869A1 0.00000
这会返回一些结果,但是,当我将其中一个结果显示为零并对表运行时,我得到一个结果。
SELECT TOP 1 [UNITPRCE]FROM [NIMET].[dbo].[SOP10200]
where ITEMNMBR='20232001'
order by ReqShipDate desc
UNITPRCE
2.10000
为什么子查询不返回此结果?
这是整个查询。很短。
declare @start datetime,
@end datetime
set @start =convert(datetime,'10/13/2014')
set @end =convert(datetime,'10/14/2014')
SELECT
tblJobHold.[MANUFACTUREORDER_I]
,[HoldQty]
,tblJobHold.[ITEMNMBR]
,[ITEMDESC]
,[OnHoldByName]
,[OnHoldDate]
,[Problem]
,[Cause]
,[Resolution]
,[Notes]
,[ReleasedByName]
,[ReleasedDate]
,(SELECT TOP 1 [UNITPRCE]FROM [NIMET].[dbo].[SOP10200]
where ITEMNMBR=tblJobHold.ITEMNMBR
order by ReqShipDate desc)
FROM [NIMETData].[dbo].[tblJobHold]
WHERE [ReleasedDate] >= @start AND [ReleasedDate] < @end
and Resolution in ('NOT FOUND', 'SCRAP-NIMET','SCRAP-PIONEEER','SCRAP-PIONEER','SCRAP-CUSTOMER','REWORK')
SOP10200根据上次发货日期保留每件商品的价格。我想使用tblJobHold表中的部件号从最新日期获得相应的价格。两者都有匹配的部件号。
我在该表中添加了第二个子查询,它在每个实例上返回值。似乎价格已经破裂
,(SELECT TOP 1 [shipToName]
FROM [NIMET].[dbo].[SOP10200]
where ITEMNMBR=tblJobHold.ITEMNMBR
order by ReqShipDate desc) as 'Ship To'