我有一个存储过程,它使用LEFT join从不同的表中获取数据。我想选择不同的记录(仅限第一个)。
存储过程如下:
ALTER PROCEDURE [dbo].[hr_ActionLog_GetList]
@Action INT = NULL,
@DateFrom DATETIME = NULL,
@DateTo DATETIME = NULL,
@CompanyID INT = NULL,
@RegistrantID INT = NULL,
@VacancyID INT = NULL,
@Language INT = 1
AS
BEGIN
SELECT AL.[RegistrantID]
,[EmployeeID]
,AL.[UserID]
,[CompanyID]
,[VacancyID]
,[Action])
,[ActionDate],
RV.Forename,
RV.Surname,
RV.Username AS RegistrantUsername,
E.Forename AS EmployeeForename,
E.Surname AS EmployeeSurname,
U.Username,
CASE
WHEN @Language = 2 THEN V.JobTitleLang2
ELSE V.JobTitleLang1
END AS JobTitle
FROM dbo.hr_ActionLog AL LEFT OUTER JOIN dbo.RegistrantsListView RV ON AL.RegistrantID = RV.RegistrantID
LEFT OUTER JOIN dbo.hr_Employees E ON AL.EmployeeID = E.EmployeeID
LEFT OUTER JOIN dbo.hr_Users U ON AL.UserID = U.UserID
LEFT OUTER JOIN dbo.hr_Companies C ON AL.CompanyID = C.CompanyID
LEFT OUTER JOIN dbo.hr_Vacancies V ON AL.VacancyID = V.VacancyID
LEFT OUTER JOIN dbo.hr_Companies VC ON V.CompanyID = VC.CompanyID
WHERE (@Action IS NULL OR AL.Action = @Action)
AND (@DateFrom IS NULL OR dbo.DateOnly(AL.ActionDate) >= dbo.DateOnly(@DateFrom))
AND (@DateTo IS NULL OR dbo.DateOnly(AL.ActionDate) <= dbo.DateOnly(@DateTo))
AND (@CompanyID IS NULL OR AL.CompanyID = @CompanyID)
AND (@RegistrantID IS NULL OR AL.RegistrantID = @RegistrantID)
AND (@VacancyID IS NULL OR AL.VacancyID = @VacancyID)
ORDER BY AL.ActionDate DESC
END
示例数据
1786 16294 15 16321 3 NULL 4 2013-08-03 12:18:08.130 cv 3 cv3@cc.com asif hameed asif@bb.com my company1aa NULL NULL
1785 16294 15 16321 3 NULL 4 2013-08-03 12:17:57.797 cv 3 cv3@cc.com asif hameed asif@bb.com my company1aa NULL NULL
1784 16293 15 16321 3 NULL 4 2013-08-03 12:17:47.243 cv 2 cv2@cc.com asif hameed asif@cc.com my company1aa NULL NULL
1783 16295 15 16321 3 NULL 4 2013-08-03 12:17:40.967 cv 4 cv4@cc.com asif hameed asif@cc.com my company1aa NULL NULL
1782 16292 15 16321 3 NULL 4 2013-08-03 12:17:31.953 cv 1 CV1@cc.com asif hameed asif@bb.com my company1aa NULL NULL
我想从动作日志表中获取第一条记录,这是不同的。
答案 0 :(得分:0)
我不确定我是否理解你的问题是正确的;当你说“我想要获得第一个独特的记录”时。 但是这样的事情可能有所帮助。 我仍然不确定哪些列具有重复值,但您可以尝试这一点。
Select Distinct TOP 1 * from
( SELECT
AL.[RegistrantID]
,[EmployeeID]
,AL.[UserID]
,[CompanyID]
,[VacancyID]
,[Action])
,[ActionDate],
RV.Forename,
RV.Surname,
RV.Username AS RegistrantUsername,
E.Forename AS EmployeeForename,
E.Surname AS EmployeeSurname,
U.Username,
CASE
WHEN @Language = 2 THEN V.JobTitleLang2
ELSE V.JobTitleLang1
END AS JobTitle
FROM dbo.hr_ActionLog AL LEFT OUTER JOIN dbo.RegistrantsListView RV ON AL.RegistrantID = RV.RegistrantID
LEFT OUTER JOIN dbo.hr_Employees E ON AL.EmployeeID = E.EmployeeID
LEFT OUTER JOIN dbo.hr_Users U ON AL.UserID = U.UserID
LEFT OUTER JOIN dbo.hr_Companies C ON AL.CompanyID = C.CompanyID
LEFT OUTER JOIN dbo.hr_Vacancies V ON AL.VacancyID = V.VacancyID
LEFT OUTER JOIN dbo.hr_Companies VC ON V.CompanyID = VC.CompanyID
WHERE (@Action IS NULL OR AL.Action = @Action)
AND (@DateFrom IS NULL OR dbo.DateOnly(AL.ActionDate) >= dbo.DateOnly(@DateFrom))
AND (@DateTo IS NULL OR dbo.DateOnly(AL.ActionDate) <= dbo.DateOnly(@DateTo))
AND (@CompanyID IS NULL OR AL.CompanyID = @CompanyID)
AND (@RegistrantID IS NULL OR AL.RegistrantID = @RegistrantID)
AND (@VacancyID IS NULL OR AL.VacancyID = @VacancyID)
ORDER BY AL.ActionDate DESC ) subquery
您甚至可以在子查询中添加Distinct,只需在主选择
中选择TOP 1 *答案 1 :(得分:0)
As @@ BogdanSahlean建议你可以试试
Select * from
( SELECT
AL.[RegistrantID]
,[EmployeeID]
,AL.[UserID]
,[CompanyID]
,[VacancyID]
,[Action])
,[ActionDate],
RV.Forename,
RV.Surname,
RV.Username AS RegistrantUsername,
E.Forename AS EmployeeForename,
E.Surname AS EmployeeSurname,
U.Username,
CASE
WHEN @Language = 2 THEN V.JobTitleLang2
ELSE V.JobTitleLang1
END AS JobTitle,
ROW_NUMBER() OVER(PARTITION BY DATEPART(yy,YOURDATEHERE) ORDER BY YOURDATEHERE DESC) ROW
FROM dbo.hr_ActionLog AL LEFT OUTER JOIN dbo.RegistrantsListView RV ON AL.RegistrantID = RV.RegistrantID
LEFT OUTER JOIN dbo.hr_Employees E ON AL.EmployeeID = E.EmployeeID
LEFT OUTER JOIN dbo.hr_Users U ON AL.UserID = U.UserID
LEFT OUTER JOIN dbo.hr_Companies C ON AL.CompanyID = C.CompanyID
LEFT OUTER JOIN dbo.hr_Vacancies V ON AL.VacancyID = V.VacancyID
LEFT OUTER JOIN dbo.hr_Companies VC ON V.CompanyID = VC.CompanyID
WHERE (@Action IS NULL OR AL.Action = @Action)
AND (@DateFrom IS NULL OR dbo.DateOnly(AL.ActionDate) >= dbo.DateOnly(@DateFrom))
AND (@DateTo IS NULL OR dbo.DateOnly(AL.ActionDate) <= dbo.DateOnly(@DateTo))
AND (@CompanyID IS NULL OR AL.CompanyID = @CompanyID)
AND (@RegistrantID IS NULL OR AL.RegistrantID = @RegistrantID)
AND (@VacancyID IS NULL OR AL.VacancyID = @VacancyID)
ORDER BY AL.ActionDate DESC ) subquery
where subquery.Row = 1