【问题标题】:ORDER BY items must appear in the select list if SELECT DISTINCT error due to case statement in order by clause如果由于 order by 子句中的 case 语句导致 SELECT DISTINCT 错误,则 ORDER BY 项目必须出现在选择列表中
【发布时间】:2019-01-25 05:45:30
【问题描述】:

我在select中添加了按列排序,然后出现错误

如果SELECT DISTINCT由于order by子句中的case语句,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

【问题讨论】:

    标签: sql-server


    【解决方案1】:

    由于您将Select DISTINCTOrder 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
    

    【讨论】:

      【解决方案2】:

      不,你没有:

      我在select中添加了按列排序,然后出现错误

      例如,您没有添加以下列:

      CreatedUser BusinessID Description NoteID Createddatetime CreatedDate
      

      您可以开始评论SELECT 中的列以查看查询何时开始工作。

      【讨论】:

      • 我只使用了 3 列按子句排序。即 pinned、lastupdateddatetime 和标题。我已经添加了这 3 列
      • @alenantony 您需要添加所有这些 - SELECT 语句中引用的所有列必须在 ORDER BY 子句中指定 - 只需将它们添加到当前列之后,它们不会造成任何伤害。跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      相关资源
      最近更新 更多