Filter query result by most recent date

后端 未结 1 1516
感动是毒
感动是毒 2021-01-25 17:29

This code:

SELECT  
    O896IA_VEMPPRSA.REG_NR & O896IA_VEMPPRSA.DIS_NR AS RRDD,  
    Mgmt_Lvl.MGMT_LVL,  
    Count(O896IA_VEMPPRSA.SYS_EMP_ID_NR) AS Total         


        
相关标签:
1条回答
  • 2021-01-25 18:09

    Say you have a table named [Survey] with the following data

    RespondentID    SurveyDate  Answer1                         Answer2
    ------------    ----------  ----------------------------    ----------------------------
    1               2013-01-01  answer 1, respondent 1, 2013    answer 2, respondent 1, 2013
    1               2012-01-01  answer 1, respondent 1, 2012    answer 2, respondent 1, 2012
    2               2012-01-01  answer 1, respondent 2, 2012    answer 2, respondent 2, 2012
    

    You can use a query of the form...

    SELECT Survey.*
    FROM
        Survey
        INNER JOIN
        (
            SELECT RespondentID, MAX(SurveyDate) AS MaxOfSurveyDate
            FROM Survey
            GROUP BY RespondentID
        ) AS MaxDate
            ON Survey.RespondentID = MaxDate.RespondentID
                AND Survey.SurveyDate = MaxDate.MaxOfSurveyDate
    

    ...to return the most recent row for each respondent, which in this case would be:

    RespondentID    SurveyDate  Answer1                         Answer2
    ------------    ----------  ----------------------------    ----------------------------
    1               2013-01-01  answer 1, respondent 1, 2013    answer 2, respondent 1, 2013
    2               2012-01-01  answer 1, respondent 2, 2012    answer 2, respondent 2, 2012
    
    0 讨论(0)
提交回复
热议问题