开发者

how to filter duplicate rows with different value

I hv a table like this

Model     Qty        Date
ABC        1         20110102
ABC       -1         2开发者_开发技巧0110105
QWE        1         20110103
ZXC        1         20110103
ABC        1         20110110
QWE       -1         20110110

I wish to hv the final output like this:-

Model     Qty        Date
ZXC        1         20110103
ABC        1         20110110

How can I do this via SQL ?

Thanks


You want the sum of the Qty and the latest Date for each Model:

SELECT Model, SUM(Qty), MAX(Date)
FROM YourTable
GROUP BY Model
HAVING SUM(Qty) <> 0

QWE has a sum of Qty = 0 and therefore should not be returned, right?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜