1

我有以下 SQL:

;WITH CTE 
     AS (SELECT AL.*, 
                RV.FORENAME, 
                RV.SURNAME, 
                RV.USERNAME                  AS RegistrantUsername, 
                E.FORENAME                   AS EmployeeForename, 
                E.SURNAME                    AS EmployeeSurname, 
                U.USERNAME, 
                CASE 
                  WHEN @Language = 2 THEN C.NAMELANG2 
                  ELSE C.NAMELANG1 
                END                          AS CompanyName, 
                CASE 
                  WHEN VC.COMPANYID IS NULL THEN 
                    CASE 
                      WHEN @Language = 2 THEN V.COMPANYNAMELANG2 
                      ELSE V.COMPANYNAMELANG1 
                    END 
                  ELSE 
                    CASE 
                      WHEN @Language = 2 THEN VC.NAMELANG2 
                      ELSE VC.NAMELANG1 
                    END 
                END                          AS VacancyCompanyName, 
                CASE 
                  WHEN @Language = 2 THEN V.JOBTITLELANG2 
                  ELSE V.JOBTITLELANG1 
                END                          AS JobTitle, 
                ROW_NUMBER() 
                  OVER( 
                    PARTITION BY AL.REGISTRANTID, AL.COMPANYID 
                    ORDER BY ACTIONDATE ASC) AS RN 
         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 
        ) 
SELECT * 
FROM   CTE 
WHERE  RN = 1; 

它根据 actiondate 从组中返回第一个元素,这很好,但返回的结果未按日期排序意味着返回每个组的第一条记录,但此第一条记录的集合未按操作日期排序。我在 CTE 中尝试了 ORDER BY AL.ActionDate DESC 但它给出了错误:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
4

5 回答 5

2

在 cte 之外移动order by子句:

WITH CTE AS 
(
    ...
)
SELECT *
FROM CTE
WHERE RN = 1;
ORDER BY ActionDate DESC
于 2013-08-20T06:23:33.163 回答
2

你在外面点菜CTE

WITH CTE
AS
{
 ....
}
SELECT *
FROM CTE
WHERE RN = 1
ORDER BY ActionDate DESC
于 2013-08-20T06:23:39.577 回答
2

试试这个——

;WITH CTE AS
(
    SELECT  ...
    FROM dbo.hr_ActionLog AL
    LEFT JOIN ...
    WHERE AL.[Action] = ISNULL(@Action, AL.[Action])
        AND dbo.DateOnly(AL.ActionDate) BETWEEN 
            dbo.DateOnly(ISNULL(@DateFrom, AL.ActionDate))
            AND
            dbo.DateOnly(ISNULL(@DateTo, '30000101'))
        AND AL.CompanyID = ISNULL(@CompanyID, AL.CompanyID)
        AND AL.RegistrantID = ISNULL(@RegistrantID, AL.RegistrantID)
        AND AL.VacancyID = ISNULL(@VacancyID, AL.VacancyID)
)
SELECT *
FROM CTE
WHERE RN = 1
ORDER BY AL.ActionDate DESC;
于 2013-08-20T06:24:30.587 回答
2

是的,唯一ORDER BY影响返回结果的顺序是放在最外面的一个SELECT

SELECT *
FROM CTE
WHERE RN = 1
ORDER BY ActionDate DESC
于 2013-08-20T06:24:33.720 回答
0

ROW_NUMBER在 CTE 中有一个函数,并且查询仅使用RN=1.
如果我正确理解了问题,只需更改函数ORDER BY中的子句,ROW_NUMBER您应该会得到您正在寻找的结果。

于 2013-08-20T06:25:53.620 回答