Answer the question
In order to leave comments, you need to log in
How to solve a problem in SQL?
Good afternoon!
Please tell me how to solve this problem. I didn't work with SQL, and I couldn't google the solution :(
Given:
There is a table in which there are three columns: id, value, date. It is
necessary to display unique rows for the id column with the maximum date. If the dates are the same, display the maximum id for this value.Tell
me how to do this.I'm trying to come up with a construction with GROUP BY and HAVING, so far without success :(
UPD .: I wrote a query that finds the maximum dates for each id:
SELECT id, max(date) as max_date FROM Income GROUP BY id
And now how to set the condition that if the id is more than one and at the same time each id has the same dates - you need to display the maximum value for this id ???
Answer the question
In order to leave comments, you need to log in
SELECT Income.id, max_date, MAX(value)
FROM Income
INNER JOIN (SELECT id, max(date) as max_date FROM Income GROUP BY id) IM
ON Income.id = IM.id AND Income.date = max_date
GROUP BY Income.id, max_date
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question