根据日期(访问/ SQL)选择每个组的前5条记录

时间:2017-06-29 19:05:12

标签: sql ms-access

我需要根据最新或未来的CURRTARGETCOMPDATE从每个启动系统([SU SYS-TO DATE] .STARTUP_SYS)中选择5条记录。 我相信我需要在我的SQL查询中添加一个子查询,但我很难搞清楚如何:

SELECT Friday_Meeting.ID, [SU SYS-TO DATE].STARTUP_SYS, [SU SYS-TO DATE].[FORECAST DATE], TEAMWORKS_24590_TOPUNCHITEM.PUNCHITEM_KEY, TEAMWORKS_24590_TOPUNCHITEM.ORIGINATOR_NAME, TEAMWORKS_24590_TOPUNCHITEM.TAG_NO, TEAMWORKS_24590_TOPUNCHITEM.ENTRYDATE, TEAMWORKS_24590_TOPUNCHITEM.FIELD_ENGINEER, [ITEM_DESCRIPTION1] & " " & [ITEM_DESCRIPTION2] & " " & [TAG_NO] & " " & " " & [CRECORD] AS Description, TEAMWORKS_24590_TOPUNCHITEM.PRIORITY_CODE, [REMARKS1] & " " & [REMARKS2] AS Remarks, TEAMWORKS_24590_TOPUNCHITEM.RESTRAINTS, TEAMWORKS_24590_TOPUNCHITEM.SUPERINTENDENT, TEAMWORKS_24590_TOPUNCHITEM.PIDISCIPLINE, TEAMWORKS_24590_TOPUNCHITEM.CURRTARGETCOMPDATE, TEAMWORKS_24590_TOPUNCHITEM.ORIGTARGETCOMPDATE, TEAMWORKS_24590_TOPUNCHITEM.CRECORD
FROM Friday_Meeting LEFT JOIN ([SU SYS-TO DATE] LEFT JOIN TEAMWORKS_24590_TOPUNCHITEM ON [SU SYS-TO DATE].STARTUP_SYS = TEAMWORKS_24590_TOPUNCHITEM.STARTUP_SYS) ON Friday_Meeting.[SU SYS] = [SU SYS-TO DATE].STARTUP_SYS
WHERE ((([SU SYS-TO DATE].STARTUP_SYS)="MVE-L-01" Or ([SU SYS-TO DATE].STARTUP_SYS)="LVE-A-01" Or ([SU SYS-TO DATE].STARTUP_SYS)="GRE-A-01or LVE-B-03" Or ([SU SYS-TO DATE].STARTUP_SYS)="LVE-B-07" Or ([SU SYS-TO DATE].STARTUP_SYS)="CHW-B-01") AND ((TEAMWORKS_24590_TOPUNCHITEM.PRIORITY_CODE)="A") AND ((TEAMWORKS_24590_TOPUNCHITEM.CURRTARGETCOMPDATE)>=[FORECAST DATE]-14) AND ((TEAMWORKS_24590_TOPUNCHITEM.RECORD_CLOSURE) Is Null) AND ((TEAMWORKS_24590_TOPUNCHITEM.DELETED)="N"))
ORDER BY Friday_Meeting.ID; 

我非常感谢任何帮助和指导!

0 个答案:

没有答案