Friday, February 2, 2024

SQL finding the max date record per user - - Foreign key and Primary key join with max date per user in foreign key - remove duplicate rows from table

 SELECT * from courtapplicationdetails ua left join ( SELECT 

    u1.*

FROM

    courtapplicationproceedings AS u1

        LEFT JOIN

    courtapplicationproceedings AS u2 ON u1.ApplicationId = u2.ApplicationId

        AND u1.NextHearingDate < u2.NextHearingDate

WHERE

    u2.NextHearingDate IS NULL

        AND u1.NextHearingDate IS NOT NULL) e on e.ApplicationId = ua.ApplicationId;


or


SELECT 

    u1.*

FROM

    user_details u1

WHERE

    u1.login_time = (SELECT 

            MAX(u2.login_time)

        FROM

            user_details u2

        WHERE

            u2.user_name = u1.user_name);



or


SELECT cad.ApplicationNo,output.DtOfInstitution,output.NextHearingDate

FROM CourtApplicationDetails cad

LEFT JOIN (

    SELECT cap.ApplicationId, cap.DtOfInstitution, cap.NextHearingDate

    FROM CourtApplicationProceedings cap,

(SELECT Applicationid, MAX(NextHearingDate) as NextHearingDate   FROM CourtApplicationProceedings  GROUP BY ApplicationId) temp

WHERE cap.ApplicationId = temp.ApplicationId and cap.NextHearingDate = temp.NextHearingDate

) output ON output.ApplicationId = cad.ApplicationId



reference:- https://thispointer.com/mysql-select-row-with-max-date-per-user/

No comments:

Post a Comment