SELECT DISTINCT A.ID, A.MT_NO, A.OPERATION, A.UNIT, F.STD_TIMES, A.WORK_TIMES, A.STD_CODE, A.UPDATE_BY, A.UPDATE_TIME, F.OPERATION_DESC, F.OPERATION_ENG, F.OPERATION_ACTION, F.CYCLE, F.PART_NO FROM tpmdata.EMS_M_DOC_OPERATION A LEFT JOIN TPMDATA.EMS_M_DOC_HEAD B ON A.MT_NO = B.MT_NO LEFT JOIN tpmdata.EMS_M_SYS_SCHEDULE C ON B.schedule_id = C.ID LEFT JOIN tpmdata.EMS_M_SYS_PLAN D ON D.ID = C.plan_id LEFT JOIN tpmdata.EMS_M_SYS_HEAD E ON D.operation_id = E.ID LEFT JOIN TPMDATA.EMS_M_SYS_ITEM F ON F.OPERATION_GROUP_ID = E.ID where A.MT_NO = '23-2023maintenancePlan-DY-002'; SQL中DISTINCT失效,查询数据有重复,如何用字段中的UPDAT_TIME在查询出来的数据过滤出最新时间的数据,展示SQL
时间: 2024-04-08 19:34:56 浏览: 86
oncrpc.rar_RPC. VC++_oncrpc windows_windows RPC_ycnian的博客
5星 · 资源好评率100%
您可以使用子查询和MAX函数来过滤出最新时间的数据,并展示在SQL查询结果中。以下是修改后的SQL查询语句:
```sql
SELECT DISTINCT
A.ID,
A.MT_NO,
A.OPERATION,
A.UNIT,
F.STD_TIMES,
A.WORK_TIMES,
A.STD_CODE,
A.UPDATE_BY,
A.UPDATE_TIME,
F.OPERATION_DESC,
F.OPERATION_ENG,
F.OPERATION_ACTION,
F.CYCLE,
F.PART_NO
FROM
tpmdata.EMS_M_DOC_OPERATION A
LEFT JOIN TPMDATA.EMS_M_DOC_HEAD B ON A.MT_NO = B.MT_NO
LEFT JOIN tpmdata.EMS_M_SYS_SCHEDULE C ON B.schedule_id = C.ID
LEFT JOIN tpmdata.EMS_M_SYS_PLAN D ON D.ID = C.plan_id
LEFT JOIN tpmdata.EMS_M_SYS_HEAD E ON D.operation_id = E.ID
LEFT JOIN TPMDATA.EMS_M_SYS_ITEM F ON F.OPERATION_GROUP_ID = E.ID
WHERE
A.MT_NO = '23-2023maintenancePlan-DY-002'
AND
A.UPDATE_TIME = (
SELECT MAX(UPDATE_TIME)
FROM tpmdata.EMS_M_DOC_OPERATION
WHERE MT_NO = '23-2023maintenancePlan-DY-002'
);
```
这样修改后的查询语句将根据字段中的UPDATE_TIME过滤出最新时间的数据,并确保结果中没有重复的记录。请注意将'MT_NO'值替换为您要过滤的特定值。
阅读全文