Mr. Developer Mr. Developer - 1 year ago 85
SQL Question

how to return a single record with DateTime senior status - sql server

I would do a join with two tables and get me back only one record, the one with the closest date to Now. I am not very practical sql, so if anyone can help me to compose the query. Thank you.

below a picture of all the records, I should make it just go back one, that between the two columns has given the greater, the one closest to now as mentioned earlier.

enter image description here

Answer Source

You can follow below query,

FROM (SELECT RGS_Id, r.RGS_DateTime, datediff(ss,r.RGS_DateTime,GETDATE()) diff_value
    from RGS_Registrazione as r
    JOIN RGA_Allarmi as a on r.RGS_MCC_Numero_Serie_MS = a.RGS_MCC_Numero_Serie_MS
    GROUP BY RGS_Id, r.RGS_DateTime) T1
ORDER BY diff_value 

Hope this should help you out.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download