0

I hv a table like this

Model     Qty        Date
ABC        1         20110102
ABC       -1         20110105
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

2
  • 2
    Please explain the logic that takes you from the original table to the result. We are not mind readers. Commented Jan 22, 2011 at 9:30
  • which database are you using? Commented Jan 22, 2011 at 9:37

2 Answers 2

4

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?

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

this selects the record with the newest date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.