基于两个字段对评论进行分组
Group Comments based on two fields
有人要求我根据结束日期和 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.
如果您想要最后一行,那么您不想聚合 数据。你只想select最后一个。
这是一种方法:
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
。
有人要求我根据结束日期和 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.
如果您想要最后一行,那么您不想聚合 数据。你只想select最后一个。
这是一种方法:
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
。