0

我有一个存储过程,它使用左连接从不同的表中获取数据。我想选择不同的记录(仅限第一个)。

存储过程是这样的:

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

我想从不同的操作日志表中获取第一条记录。

4

2 回答 2

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 *

于 2013-08-03T08:44:50.153 回答
0

正如@@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
于 2013-08-03T11:50:37.173 回答