Select a Column in SQL not in Group By

前端 未结 7 1400
忘了有多久
忘了有多久 2020-11-30 02:25

I have been trying to find some info on how to select a non-aggregate column that is not contained in the Group By statement in SQL, but nothing I\'ve found so far seems to

7条回答
  •  日久生厌
    2020-11-30 02:47

    You can join the table on itself to get the PK:

    Select cpe1.PK, cpe2.MaxDate, cpe1.fmgcms_cpeclaimid 
    from Filteredfmgcms_claimpaymentestimate cpe1
    INNER JOIN
    (
        select MAX(createdon) As MaxDate, fmgcms_cpeclaimid 
        from Filteredfmgcms_claimpaymentestimate
        group by fmgcms_cpeclaimid
    ) cpe2
        on cpe1.fmgcms_cpeclaimid = cpe2.fmgcms_cpeclaimid
        and cpe1.createdon = cpe2.MaxDate
    where cpe1.createdon < 'reportstartdate'
    

提交回复
热议问题