W
W
whoareyoutofuckinglecture2019-12-19 16:44:32
SQL
whoareyoutofuckinglecture, 2019-12-19 16:44:32

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

2 answer(s)
K
Konstantin Tsvetkov, 2019-12-19
@whoareyoutofuckinglecture

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

S
SagePtr, 2019-12-19
@SagePtr

Maybe one of these will help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question