开发者

how to filter duplicate rows with different value

开发者 https://www.devze.com 2023-02-06 06:58 出处:网络
I hv a table like this ModelQtyDate ABC120110102 ABC-12开发者_开发技巧0110105 QWE120110103 ZXC120110103

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

精彩评论

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