我有一个像这样的表
Model Qty Date
ABC 1 20110102
ABC -1 20110105
QWE 1 20110103
ZXC 1 20110103
ABC 1 20110110
QWE -1 20110110
我希望得到这样的最终输出: -
Model Qty Date
ZXC 1 20110103
ABC 1 20110110
如何通过SQL执行此操作?
由于
答案 0 :(得分:4)
您想要每个模型的数量和最新日期的总和:
SELECT Model, SUM(Qty), MAX(Date)
FROM YourTable
GROUP BY Model
HAVING SUM(Qty) <> 0
QWE总和Qty = 0因此不应退回,对吧?
答案 1 :(得分:0)
select t1.Model, t1.Qty, t1.Date from thetable t1 where t1.Date = (select max(t2.Date) from thetable t2 where t2.Model=t1.Model)