Titan97 Titan97 - 1 year ago 57
SQL Question

What Query To be Used For getting the output has given below on the table given?

The database is

enter image description here

I want the output has

| x | y | z | d |
| 1 | ac | you |2016-07-12|
| 2 | foo | game|2016-07-21|
| 3 | gaa | bha |2016-07-05|

What is the query for this I am newbie in mysql. Please help me out with it. Thanks in advance

Answer Source

It looks like you want to select the record for each value of x which has the latest date in column d. If so, then the following query should work:

SELECT t1.x, t1.y, t1.z, t1.d
FROM yourTable t1
    SELECT x, MAX(d) AS d
    FROM yourTable
    GROUP BY x
) t2
    ON t1.x = t2.x AND t1.d = t2.d
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download