我有一个存储过程,它使用左连接从不同的表中获取数据。我想选择不同的记录(仅限第一个)。
存储过程是这样的:
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
我想从不同的操作日志表中获取第一条记录。