我有一个Select语句,其中包含超过23K的大量数据,它与5个以上的表连接在一起,当我查看该表时,其中之一导致重复,我发现我正在执行的连接具有2行匹配,< / p>
我尝试按分组方式进行操作,但没有成功,我还尝试了选择DISTINCT,但同样行不通,我将如何处理?
SELECT DISTINCT FirstName, LastName, F.Interview FROM tbData D
LEFT JOIN tbInterview F on D.UserID = F.UserID
where CreatedDate is between '' and ''
因为tbInterview有多个链接USerID的Interview,所以返回Duplicates,然后尝试了
SELECT DISTINCT FirstName, LastName, F.Interview FROM tbData D
LEFT JOIN (Select UserID from tbInterview GROUP BY UserID) as InterviewID ON D.UserID = InterviewID.UserID
LEFT JOIN tbInterview F on InterviewID.UserID = F.UserID
where CreatedDate is between '' and ''
这也不起作用。
这是表tbInterview中的数据示例
╔═════════════╤════════╤═════════════╤═════════════════════════════╗
║ InterViewID │ UserID │ DateCreated │ Interview ║
╠═════════════╪════════╪═════════════╪═════════════════════════════╣
║ 1 │ 120 │ 2015/05/10 │ Inter View Done ║
╟─────────────┼────────┼─────────────┼─────────────────────────────╢
║ 2 │ 120 │ 2015/05/15 │ 2nd Interview was requested ║
╚═════════════╧════════╧═════════════╧═════════════════════════════╝
现在,当我使用tbInterview的Join进行Select时,它显示的输出如下:
╔═══════════╤══════════╤═════════════════════════════╗
║ FirstName │ LastName │ Interview ║
╠═══════════╪══════════╪═════════════════════════════╣
║ James │ Smith │ Inter View Done ║
╟───────────┼──────────┼─────────────────────────────╢
║ James │ Smith │ 2nd Interview was requested ║
╚═══════════╧══════════╧═════════════════════════════╝
答案 0 :(得分:0)
假设您正在tbInterview中查找创建日期的最新行,那么您有几种可能的解决方案。这是根据您的第一个查询使用ROWNUMBER()函数的函数:
这假设您实际上只希望获得示例查询中指定的范围内的信息,该值的范围在CreatedDate值(假设在tbData表上,因为DateCreated在tbInterview上等效)。
SELECT
FirstName,
LastName,
F.Interview
FROM tbData D
LEFT JOIN (
SELECT
UserID,
CreatedDate,
ROW_NUMBER() OVER (Partition By UserId ORDER BY DateCreated Desc) as RowNumber
FROM tbInterview F
WHERE DateCreated > '' --- Earliest date created for range on tbData
) as F
on D.UserID = F.UserID
WHERE CreatedDate is between '' and ''
AND RowNumber = 1
这会将最新的采访行(基于DateCreated)与tbData用户相关联。