我被要求根据Enddate和CD_TYPE_COMMENTS字段对TX_Comments进行分组。输出应该是每CD_TYPE_COMMENTS和Enddate
的最新行ID_EMPLOYEE ID_HEALTH_ASSESSMENT CD_TYPE_COMMENT ENDDATE TX_COMMENT
0M00910044 37754 Validation 18.06.2013 Record validated.
0M00910044 37754 Validation 20.05.2013 Record validated.
0M00910044 37754 Result 26.07.2013 Created.
0M00910044 37754 Appointment 18.06.2013 Pls cancel due to work commitments.
0M00910044 37754 Appointment 25.06.2013 Confirmed (via Import)
0M00910044 37754 Ellipse Difference 23.07.2013 The following Employee Details have been updated:
0M00910044 37754 General 26.07.2013 Record complete
0M00910044 37754 Validation 20.05.2013 Record created.
0M00910044 37754 Appointment 09.07.2013 As advised by Mick Maskell. To be rescheduled to another date.
0M00910044 37754 Appointment 09.07.2013 Late cancellation charges will apply.
0M00910044 37754 Appointment 09.07.2013 Confirmed (via Import)
0M00910044 37754 Ellipse Difference 23.07.2013 The following Employee Details have been updated and should be checked for impact on this HA:
0M00910044 37754 Validation 20.05.2013 Manually created due to import exception error - Test Type could not be determined. Create HA manually. Has a valid RISI.
0M00910044 37754 Appointment 25.06.2013 Preferences provided.
0M00910044 37754 Ellipse Difference 04.07.2013 The following Employee Details have been updated and should be checked for impact on this HA:
0M00910044 37754 Validation 09.07.2013 Record validated.
0M00910044 37754 Appointment 18.06.2013 Cancelled (by Railcorp)
0M00910044 37754 Appointment 09.07.2013 Cancelled (by Railcorp)
0M00910044 37754 Appointment 24.05.2013 Preferences provided.
0M00910044 37754 Appointment 28.05.2013 Confirmed (via Import)
0M00910044 37754 Appointment 09.07.2013 Preferences provided.
答案 0 :(得分:1)
如果您想要最后一行,那么您不希望聚合数据。你只想选择最后一个。
这是一种方法:
select t.*
from t
where t.enddate = (select max(t2.enddate)
from t t2
where t2.CD_TYPE_COMMENT = t.CD_TYPE_COMMENT
);
如果您确实希望每位员工都获得此信息,请在相关条款中包含and t2.Employee_ID = t.Employee_ID
。