如果值小于10 SQL,则计算上一次购买价格

时间:2013-05-30 16:21:34

标签: sql sql-server sql-server-2008

我的select查询中有以下子查询,它根据上次购买日期获取产品的最后购买日期,购买日期,订单号和供应商。但是,我需要修改它,以便如果最后一次购买少于10美元,它将进入下一个最后购买日期,依此类推,并相应地填充购买日期,订单号和供应商。

有什么建议我会怎么做?

2 个答案:

答案 0 :(得分:0)

添加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)

答案 1 :(得分:0)

我不知道我是否正确理解了您的问题,但您想要的只是购买金额&gt; 10只需在子查询中添加它。它将忽略金额<1的所有购买日期。 10。

希望这有帮助。

SELECT TOP (1) purchamt AS LastPurchase 
FROM purchase AS p 

WHERE (masterid = m.masterid) 
    AND (purchvendorid <> '')
    and purchamt  > 10
ORDER BY purchasedate DESC