【问题标题】:How to order by certain columns first in a union select如何在联合选择中首先按某些列排序
【发布时间】:2018-05-15 13:51:28
【问题描述】:

我正在从各种表中执行SELECT 语句,但我想首先ORDER BY 一组标准。以下是SELECT 语句的示例:

SELECT
 a.ItemName,
 a.ItemDescription,
 a.ItemPrice,
 a.DateAdded,
 a.UserID,
 u.UserType
FROM
 table a
 inner join user u ON
 u.UserID = a.UserID
UNION
SELECT
 b.ItemName,
 b.ItemDescription,
 b.ItemPrice,
 b.DateAdded,
 b.UserID,
 u.UserType
FROM
 table b
 inner join user u ON
 u.UserID = b.UserID
UNION
SELECT
 c.ItemName,
 c.ItemDescription,
 c.ItemPrice,
 c.DateAdded,
 c.UserID,
 u.UserType
FROM
 table c
inner join user u ON
u.UserID = c.UserID

从上面的结果集中,我想先ORDER BY where u.UserType = 'Consultant' and DateAdded DESC, and then by u.UserType = 'Internal' and Price DESC and DateAdded DESC

我怎样才能以上述方式强制排序

【问题讨论】:

  • 附带说明:一个真正的数据库有多个项目表会很奇怪,但这只是我猜的一个例子?真的有重复要删除吗?否则使用UNION ALL 为 DBMS 节省不必要的工作。
  • 它们是不同的表,但都包含不同类型的项目。当我在做 UNION 时,我只是给所有列加上别名,以便它们匹配。例如,它实际上是SELECT c.MonroeProductCode AS ItemDescription 等等。

标签: sql sql-server tsql sql-server-2012 sql-server-2014


【解决方案1】:

您可以使用case 来做到这一点

Order by
       case when u.UserType = 'Consultant' then 1 else 0 end,
       DateAdded desc,
       case when u.UserType = 'Internal' then 1 else 0 end,
       Price desc,
       DateAdded desc

【讨论】:

    【解决方案2】:

    没有任何 DDL 和 Sample 数据进行测试,这有点猜测,但也许...

    ORDER BY CASE UserType WHEN 'Consultant' THEN 1 END DESC,
             DateAdded DESC,
             CASE UserType WHEN 'Internal' THEN 1 END DESC,
             Price DESC,
             DateAdded DESC;
    

    编辑,重读后,我不完全确定这是 OP 想要的。如果我和 Magnus 不正确,示例和预期输出会很有用。

    【讨论】:

      【解决方案3】:

      顾问的第一个订单与否。然后按价格订购(但不是针对顾问,所以对他们来说,按恒定的伪价格订购)。然后按添加日期排序。

      order by
        case when usertype = 'Consultant' then 1 else 2 end,
        case when usertype = 'Consultant' then 0 else price end desc,
        dateadded desc;
      

      首先是顾问(按添加日期排序),然后是所有其他顾问(按价格和添加日期排序)。

      如果存在的用户类型多于您提到的两种,并且您首先需要顾问,然后是内部人员,然后是其他人,您必须相应地更改第一行:

      order by
        case usertype when 'Consultant' then 1 when 'Internal' then 2 else 3 end,
        case when usertype = 'Consultant' then 0 else price end desc,
        dateadded desc;
      

      【讨论】:

      • 是的,不仅仅是顾问和内部人员。当usertype = 'Consultant' 不止一次时,您的案例陈述如何检查?这是否意味着在这两种情况下都是正确的?您能解释一下then 0then 1 相比有什么区别吗?我不完全了解编号系统。
      • 我们希望首先拥有所有顾问,然后才是所有内部人员。这就是ORDER BY 的第一行所做的;它检查该行是什么用户类型,并给它一个排序号 1 用于顾问,2 用于内部,3 用于其他。完成后,我们进入第二行。所以内部顾问和内部内部等我们进一步分类。但是,由于每个查询只有一个 ORDER BY 子句,因此我们必须找到适合所有用户类型的排序顺序...
      • ... 让我们看看顾问:如果我们检测到顾问行,我们给它一个恒定的排序值 0(这可以是任何恒定值)。所以没有一个顾问比另一个更好,没有排序。然而,对于其他用户类型,我们使用价格作为它们的排序值,因此内部块可以按价格进一步排序。然后我们按日期排序的最后一行添加:顾问块首先出现,但顾问本身是无序的,现在按日期排序......
      • 接下来是内部模块。内部零件已经按价格订购,现在价格相同的零件按日期订购。如果您仍然不明白:使用 Excel。列:sortkey1、sortkey2、sortkey3、用户类型、价格、添加日期。让一些顾问和内部人员具有不同且相同的价格和日期(例如,大约 20 行)。为顾问设置 sortkey1 = 1,为内部人员设置 2,否则为 3。为顾问设置 sortkey2 = 0,为其他设置价格。将 sortkey3 设为日期。单独按 sortkey1 排序。看看你得到了什么。按 sortkey1 和 sortkey2 排序。按 sortkey1 和 sortkey2 和 sortkey3 排序。
      猜你喜欢
      • 2012-11-14
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多