我在SQL服务器中有一个视图,用于通过连接将数据下载到Access中。目的是对公司数据库和我自己的所有记录进行排序,找到最近输入的记录,这是下载员工的记录。如果工作正常,它应该只下载大约65000条记录,但无论何时发生,它都会下载每个员工记录。 MEETING END DATE字段是时间戳字段,指示输入记录的时间。有人建议我在其中构建一个子选择,但子查询不是我的强项,我很难找到正确的方法。代码在
之下SELECT
AC.REG_NR, AC.DIS_NR, AC.GEMSID, AC.TMS_ID, AC.EMP_NA, AC.EMP_SEX_TYP_CD,
AC.EMP_EOC_GRP_TYP_CD, AC.DIV_NR, AC.CTR_NR,
AC.JOB_CLS_CD_DSC_TE, AC.JOB_GRP_CD, AC.Job_Function, AC.Job_Group,
AC.Meeting_Readiness_Rating, AC.Manager_Readiness_Rating, CD.Employee_ID,
CD.Meeting_Readiness_Rating AS Expr1, CD.Manager_Readiness_Rating AS Expr2,
CD.Meeting_End_Date, CD.EmployeeFeedback,
CD.DevelopmentForEmployee1, CD.DevelopmentForEmployee2,
CD.DevelopmentForEmployee3, CD.DevelopmentForEmployee4,
CD.DevelopmentForEmployee5,
CD.Justification, CD.Changed, CD.Notes
FROM
dbo.AC_Source AS AC
LEFT OUTER JOIN
(SELECT
EmployeeID AS Employee_ID,
MeetingReadinessLevel AS Meeting_Readiness_Rating,
ManagerReadinessLevel AS Manager_Readiness_Rating,
logdate AS Meeting_End_Date, EmployeeFeedback,
DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3,
DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes
FROM
dbo.AC_CDData_1
UNION ALL
SELECT
Employee_ID, Meeting_Readiness_Rating, Manager_Readiness_Rating,
Meeting_End_Date, '' AS EmployeeFeedback, '' AS DevelopmentForEmployee1,
'' AS DevelopmentForEmployee2, '' AS DevelopmentForEmployee3,
'' AS DevelopmentForEmployee4, '' AS DevelopmentForEmployee5,
'' AS Justification, '' AS Changed, '' AS Notes
FROM
dbo.TMS_Data_Latest_Career_Meeting_Rating
WHERE
(Plan_Year = '2013')) AS CD ON AC.TMS_ID = CD.Employee_ID
它基本上需要搜索员工ID并找到每个员工的最大会议结束日期
答案 0 :(得分:1)
好的,
如果我已正确理解了这个问题,您应该能够通过使用RANK窗口功能来完成您尝试做的事情。我已经重写了相当多的查询以消除了很多选择标准,这些标准主要是关于你试图达到的核心逻辑的噪音:
SELECT Blah1
FROM dbo.AC_Source AS AC
INNER JOIN
(
SELECT Blah2 FROM
(
SELECT Blah2, ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY Meeting_End_Date DESC) AS RowNum
FROM
(
SELECT Blah2
FROM dbo.AC_CDData_1
UNION ALL
SELECT Blah2
FROM dbo.TMS_Data_Latest_Career_Meeting_Rating
WHERE (Plan_Year = '2013')
) AS InnerSelect
) AS CD
WHERE RowNum = 1
)
ON AC.TMS_ID = CD.Employee_ID
所以这里发生的是,InnerSelect中的员工记录按EmployeeID分组,然后按分区中的Meeting_End_Date顺序排列。 " WHERE RowNum = 1"内部选择之外的子句表示您只会在该分区的排名员工中获取排名第一的记录。
我可能没有完全正确的语法,因为在没有你的桌子的情况下进行测试有点困难(我现在没有时间制作它们),但这个想法应该适用于你想要完成的事情。
答案 1 :(得分:0)
虽然它相当难看,但我担心表现会低于预期,但没有任何东西可以测试,这将是我最好的拍摄:
;WITH CD
AS (SELECT EmployeeID AS Employee_ID, MeetingReadinessLevel AS Meeting_Readiness_Rating, ManagerReadinessLevel AS Manager_Readiness_Rating,
logdate AS Meeting_End_Date, EmployeeFeedback, DevelopmentForEmployee1, DevelopmentForEmployee2, DevelopmentForEmployee3,
DevelopmentForEmployee4, DevelopmentForEmployee5, Justification, Changed, Notes
FROM dbo.AC_CDData_1
UNION ALL
SELECT Employee_ID, Meeting_Readiness_Rating, Manager_Readiness_Rating, Meeting_End_Date, '' AS EmployeeFeedback, '' AS DevelopmentForEmployee1,
'' AS DevelopmentForEmployee2, '' AS DevelopmentForEmployee3, '' AS DevelopmentForEmployee4, '' AS DevelopmentForEmployee5, '' AS Justification,
'' AS Changed, '' AS Notes
FROM dbo.TMS_Data_Latest_Career_Meeting_Rating
WHERE (Plan_Year = '2013')
),
MAXCD
AS (SELECT Employee_ID, Last_Meeting_End_Date = MAX(Meeting_End_Date)
FROM CD
GROUP BY Employee_ID)
SELECT AC.REG_NR, AC.DIS_NR, AC.GEMSID, AC.TMS_ID, AC.EMP_NA, AC.EMP_SEX_TYP_CD, AC.EMP_EOC_GRP_TYP_CD, AC.DIV_NR, AC.CTR_NR,
AC.JOB_CLS_CD_DSC_TE, AC.JOB_GRP_CD, AC.Job_Function, AC.Job_Group, AC.Meeting_Readiness_Rating, AC.Manager_Readiness_Rating, CD.Employee_ID,
CD.Meeting_Readiness_Rating AS Expr1, CD.Manager_Readiness_Rating AS Expr2, CD.Meeting_End_Date, CD.EmployeeFeedback,
CD.DevelopmentForEmployee1, CD.DevelopmentForEmployee2, CD.DevelopmentForEmployee3, CD.DevelopmentForEmployee4, CD.DevelopmentForEmployee5,
CD.Justification, CD.Changed, CD.Notes
FROM dbo.AC_Source AS AC
LEFT OUTER JOIN ( SELECT CD.*
FROM CD
JOIN MAXCD
ON MAXCD.Employee_ID = CD.Employee_ID
AND MAXCD.Last_Meeting_End_Date = CD.Meeting_End_Date ) FLTRD
ON AC.TMS_ID = FLTRD.Employee_ID