好的,这是选择陈述
SELECT dbo.tbl_inspectors.user_firstname,
dbo.tbl_inspectors.user_lastname,
dbo.tbl_inspectors.user_loc,
dbo.tbl_appraisals.appPartA,
dbo.tbl_appraisals.appPartB,
dbo.tbl_appraisals.appAppraised,
dbo.tbl_appraisals.appFinalized,
dbo.tbl_appraisals.appDate,
dbo.tbl_inspectors.appProcStart,
dbo.tbl_inspectors.appDue,
dbo.tbl_inspectors.inspAdhocStaff,
dbo.tbl_inspectors.access_id,
dbo.tbl_appraisals.appraiseID, dbo.tbl_inspectors.user_id
FROM dbo.tbl_inspectors
LEFT OUTER JOIN
dbo.tbl_appraisals ON dbo.tbl_inspectors.user_id = bo.tbl_appraisals.inspectID
如果针对tbl_appraisals表中的tbl_inspectors.user_id有多个结果,那么我会为每个检查器获得多个结果,我只是希望它从tbl_inspectors中获取最新的匹配对tbl_inspectors user_id,我该如何实现这一点简单地?
PS tbl_appraisals.appDate是确定来自tbl_appraisals的最新结果的字段
答案 0 :(得分:1)
假设引擎不是MySQL ......
这是每组" TOP 1"问题
SELECT
*
FROM
(
SELECT I.user_firstname,
I.user_lastname,
I.user_loc,
A.appPartA,
A.appPartB,
A.appAppraised,
A.appFinalized,
A.appDate,
I.appProcStart,
I.appDue,
I.inspAdhocStaff,
I.access_id,
A.appraiseID,
I.USER_ID,
ROW_NUMBER() OVER (PARTITION BY I.user_id ORDER BY COALESCE(A.appDate, '19000101') DESC) AS rn
FROM dbo.tbl_inspectors I
LEFT OUTER JOIN
dbo.tbl_appraisals A ON I.user_id = A.inspectID
) X
WHERE
X.rn = 1