我的选择查询中有以下子查询,它根据上次购买日期获取产品的最后购买日期、购买日期、订单号和供应商。但是,我需要对其进行修改,以便如果最后一次购买少于 10 美元,它将进入下一个最后一次购买日期,依此类推,并相应地填充购买日期、订单号和供应商。
有什么建议我会怎么做?
我的选择查询中有以下子查询,它根据上次购买日期获取产品的最后购买日期、购买日期、订单号和供应商。但是,我需要对其进行修改,以便如果最后一次购买少于 10 美元,它将进入下一个最后一次购买日期,依此类推,并相应地填充购买日期、订单号和供应商。
有什么建议我会怎么做?
添加purchamt >= 10
条件应该会有所帮助
,LastPurchase=(SELECT TOP (1) purchamt AS LastPurchase
FROM purchase AS p
WHERE (masterid = m.masterid)
AND (purchvendorid <> '')
AND purchamt >= 10
ORDER BY purchasedate DESC)
,PurchaseDate=(SELECT TOP (1) purchasedate
FROM purchase AS p
WHERE (masterid = m.masterid)
AND (purchvendorid <> '')
AND purchamt >= 10
ORDER BY purchasedate DESC)
,PONo=(SELECT TOP 1 do.orderno
FROM [purchase] p INNER JOIN dealorder do on do.orderid = p.purchasepoid
WHERE (masterid = m.masterid)
AND (purchvendorid <> '')
AND purchamt >= 10
ORDER BY purchasedate DESC)
,vendor=(SELECT TOP 1 v.vendor
FROM [purchase] p INNER JOIN vendor v on v.vendorid = p.purchvendorid
WHERE (masterid = m.masterid)
AND (purchvendorid <> '')
AND purchamt >= 10
ORDER BY purchasedate DESC)
我不知道我是否正确理解了您的问题,但您想要的只是购买金额应大于 10,只需在您的子查询中添加即可。它将忽略金额 < 10 的所有购买日期。
希望这可以帮助。
SELECT TOP (1) purchamt AS LastPurchase
FROM purchase AS p
WHERE (masterid = m.masterid)
AND (purchvendorid <> '')
and purchamt > 10
ORDER BY purchasedate DESC