如果由于case BY子句中的case语句而导致SELECT DISTINCT错误,则ORDER BY项目必须出现在选择列表中

时间:2019-01-25 05:45:30

标签: sql-server

我在select中按列添加了顺序,然后发生了错误

  

如果由于order by子句中的case语句而导致SELECT DISTINCT,则ORDER BY项目必须出现在选择列表中

这是用于SQL Server 2012

DECLARE @SortType VARCHAR(20) = 'lUf'

SELECT DISTINCT
    COUNT(*) OVER () AS Count,      
    nm.NoteID, nm.Title, 
    Description, CreatedDate, Createddatetime, 
    nm.BusinessID, CreatedUser, nm.LastUpdatedDatetime 
FROM
    PLO.NotesMaster nm with(nolock) 
LEFT JOIN
    PLO.NoteTags nt ON nm.NoteID = nt.NoteID
WHERE 
    (nm.Title LIKE '%'+@keyword+'%'  OR COALESCE(@keyword ,'')='')
    AND (nt.TagID = @TagID OR COALESCE(@TagID, '') = '')
    AND (nm.BusinessID = @BusinessID)
    AND (nm.CreatedUser = @UserID)
    AND (nm.Status != 2)
ORDER BY
    Pinned DESC,
    (CASE @SortType
        WHEN 'lUf' THEN nm.LastUpdatedDatetime
     END) DESC,
    (CASE @SortType 
        WHEN 'lul' THEN nm.LastUpdatedDatetime
     END) ASC,
    (CASE @SortType 
        WHEN 'az' THEN nm.Title  
     END) ASC,
    (CASE @SortType 
        WHEN 'za' THEN nm.Title 
     END) desc

2 个答案:

答案 0 :(得分:2)

由于您在Select DISTINCT语句中使用了Order byCase,因此需要将所有带有case语句的按列排序都包括在select语句中。

SELECT DISTINCT
    COUNT(*) OVER () AS Count,      
    nm.NoteID, nm.Title, 
    Description, CreatedDate, Createddatetime, 
    nm.BusinessID, CreatedUser, nm.LastUpdatedDatetime,
    Pinned DESC,
    (CASE @SortType
        WHEN 'lUf' THEN nm.LastUpdatedDatetime
     END) DESC,
    (CASE @SortType 
        WHEN 'lul' THEN nm.LastUpdatedDatetime
     END) ASC,
    (CASE @SortType 
        WHEN 'az' THEN nm.Title  
     END) ASC,
    (CASE @SortType 
        WHEN 'za' THEN nm.Title 
     END) desc
FROM
    PLO.NotesMaster nm with(nolock) 
LEFT JOIN
    PLO.NoteTags nt ON nm.NoteID = nt.NoteID
WHERE 
    (nm.Title LIKE '%'+@keyword+'%'  OR COALESCE(@keyword ,'')='')
    AND (nt.TagID = @TagID OR COALESCE(@TagID, '') = '')
    AND (nm.BusinessID = @BusinessID)
    AND (nm.CreatedUser = @UserID)
    AND (nm.Status != 2)
ORDER BY
    Pinned DESC,
    (CASE @SortType
        WHEN 'lUf' THEN nm.LastUpdatedDatetime
     END) DESC,
    (CASE @SortType 
        WHEN 'lul' THEN nm.LastUpdatedDatetime
     END) ASC,
    (CASE @SortType 
        WHEN 'az' THEN nm.Title  
     END) ASC,
    (CASE @SortType 
        WHEN 'za' THEN nm.Title 
     END) desc

SELECT * FROM
(
    SELECT DISTINCT
        COUNT(*) OVER () AS Count,      
        nm.NoteID, nm.Title, 
        Description, CreatedDate, Createddatetime, 
        nm.BusinessID, CreatedUser, nm.LastUpdatedDatetime 
    FROM
        PLO.NotesMaster nm with(nolock) 
    LEFT JOIN
        PLO.NoteTags nt ON nm.NoteID = nt.NoteID
    WHERE 
        (nm.Title LIKE '%'+@keyword+'%'  OR COALESCE(@keyword ,'')='')
        AND (nt.TagID = @TagID OR COALESCE(@TagID, '') = '')
        AND (nm.BusinessID = @BusinessID)
        AND (nm.CreatedUser = @UserID)
        AND (nm.Status != 2)
) t
ORDER BY
    Pinned DESC,
    (CASE @SortType
        WHEN 'lUf' THEN nm.LastUpdatedDatetime
     END) DESC,
    (CASE @SortType 
        WHEN 'lul' THEN nm.LastUpdatedDatetime
     END) ASC,
    (CASE @SortType 
        WHEN 'az' THEN nm.Title  
     END) ASC,
    (CASE @SortType 
        WHEN 'za' THEN nm.Title 
     END) desc

答案 1 :(得分:0)

不,您还没有:

  

我在select中按列添加了顺序,然后发生了错误

例如,您尚未添加以下列:

CreatedUser BusinessID Description NoteID Createddatetime CreatedDate

您可以开始注释SELECT中的列,以查看查询何时开始工作。