如何在SQL Server中使用预定义的顺序获取行?

时间:2017-07-19 20:15:21

标签: sql sql-server

我有一个像这样的SQL Server表:

MenuID    MenuName           MenuColor
---------------------------------------
10         Daily Tickets     Gray
27         Annual Pass       sky blue
22         Frequent visitor  Musturd
20         Group Discount    Dark Ash
11         Discount ticket   Brown
24         free Ticket       Yellow
25         Kids Pass         Pink
15         Kids Ticket       Dark Pink
17         Referral Ticket   Beige

此表包含大量记录和更多列。

期望的结果 - 前四个菜单应该按照预先定义的顺序(我在试用查询中提到)订购,剩下的应该在MenuName列上订购ASC

期望的结果集:

 SELECT * 
 FROM tMenus m
 ORDER BY 
     (CASE m.MenuName
         WHEN 'Daily Tickets' THEN 1
         WHEN 'Annual Pass'  THEN 2
         WHEN 'Frequent visitor' THEN 3
         WHEN 'Group Discount' THEN 4  
      END), m.MenuName ASC;

这是我为此尝试的查询:

{{1}}

然而,这并没有返回我想要的结果。请纠正我错在哪里。

由于

2 个答案:

答案 0 :(得分:4)

也许你只需要一个else

 ORDER BY (CASE m.MenuName
             WHEN 'Daily Tickets' THEN 1
             WHEN 'Annual Pass'  THEN 2
             WHEN 'Frequent visitor'  THEN 3
             WHEN 'Group Discount' THEN 4  
             ELSE 5
          END) , m.MenuName ASC;

答案 1 :(得分:1)

将“DisplayOrder”添加到实际表格中......

IF OBJECT_ID('tempdb..#Menue', 'U') IS NOT NULL 
DROP TABLE #Menue;

CREATE TABLE #Menue (
    MenuID INT NOT NULL PRIMARY KEY,
    MenuName VARCHAR(30) NOT NULL,
    MenuColor VARCHAR(10) NOT NULL,
    DisplayOrder INT NOT NULL 
    );

INSERT #Menue(MenuID, MenuName, MenuColor, DisplayOrder) VALUES 
    (10,'Daily Tickets', 'Gray', 100),
    (15,'Kids Ticket', 'Dark Pink', 800),
    (20,'Group Discount', 'Dark Ash', 400),
    (11,'Discount ticket', 'Brown', 500),
    (17,'Referral Ticket', 'Beige', 900),
    (22,'Frequent visitor', 'Musturd', 300),
    (27,'Annual Pass', 'sky blue', 200),
    (25,'Kids Pass', 'Pink', 700),
    (24,'free Ticket', 'Yellow', 600);
    -- Note: I'm leaving gaps in the DisplayOrder values.
    --  This makes it easy to add new values and set their 
    --  values w/o having to adjust existing values.

SELECT 
    m.MenuID,
    m.MenuName,
    m.MenuColor
FROM 
    #Menue m
ORDER BY 
    m.DisplayOrder;

编辑回答......

IF OBJECT_ID('tempdb..#MenueDisplayOrder', 'U') IS NOT NULL 
DROP TABLE #MenueDisplayOrder;

CREATE TABLE #MenueDisplayOrder (
    MenueID INT NOT NULL, --add FK to Menues table
    DisplayTypeID INT NOT NULL, --add FK to available Types table
    DisplayOrder INT NOT NULL 
    PRIMARY KEY CLUSTERED (DisplayTypeID, MenueID)
    );

    INSERT #MenueDisplayOrder (MenueID, DisplayTypeID, DisplayOrder) VALUES 
    (10, 1, 100), (11, 1, 500), (15, 1, 800), (17, 1, 900), (20, 1, 400),
    (22, 1, 300), (24, 1, 600), (25, 1, 700), (27, 1, 200), 
    (27, 2, 100), (25, 2, 500), (24, 2, 800), (20, 2, 900), (17, 2, 400),
    (22, 2, 300), (15, 2, 600), (11, 2, 700), (10, 2, 200),
    (15, 3, 100), (11, 3, 500), (10, 3, 800), (22, 3, 900), (24, 3, 400),
    (17, 3, 300), (20, 3, 600), (27, 3, 700), (25, 3, 200);

SELECT 
    m.MenuID,
    m.MenuName,
    m.MenuColor
FROM 
    #Menue m
    JOIN #MenueDisplayOrder mdo
        ON m.MenuID = mdo.MenueID
WHERE 
    mdo.DisplayTypeID = 2 -- alter this value to change the display order.
ORDER BY 
    mdo.DisplayOrder;