0

LEFT 結合を使用して異なるテーブルからデータを取得するストアド プロシージャがあります。個別のレコードを選択したい (最初のレコードのみ)。

ストアド プロシージャは次のようになります。

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 に答える