【问题标题】:Why the grouped query is sorting A to Z by default?为什么分组查询默认按 A 到 Z 排序?
【发布时间】:2019-12-18 08:36:03
【问题描述】:

我创建了一个查询,通过该查询从表的列中获取不为空的值:

Select * from(
SELECT       OEID, Chest_Pain as Head, Chest_PainComment as Detail
FROM            tblComplaints
union

SELECT       OEID, SOB as Head, SOBComment as Detail
FROM            tblComplaints
union

SELECT        OEID, PND as Head, Cyanosis as Detail FROM tblComplaints
union

SELECT        OEID, Odema_Feet as Head, Vertigo + as Detail  From tblComplaints
union

SELECT       OEID, DM as Head, DMComment as Detail
FROM            tblComplaints
union

SELECT       OEID, RS as Head, RSComment as Detail
FROM            tblComplaints

) as t
where (Head is not null and ltrim(rtrim(Head)) <> '')
and OEID = 6012

数据很好,但问题是这个查询会自动在输出结果中进行 A 到 Z 排序。我需要做的是按照我输入每一行的方式得到结果。

例如:目前我得到这个查询的输出如下:

Head                            Detail
Chest_Pain                      Chest_PainComment
DM                              DmComment
Odema_Feet                      Vertigo
PND                             Cyanosis
RS                              RSComment

我希望它是这样的:

Head                            Detail
Chest_Pain                      Chest_PainComment
RS                              RSComment
PND                             Cyanosis
DM                              DMComment

底线不应该是 A 到 Z 排序,这发生在我的查询中。我不知道为什么在查询中会发生这种 A 到 Z 排序,而我没有在任何地方对其进行排序。

我将不胜感激。

【问题讨论】:

    标签: sql sql-server visual-studio sorting union


    【解决方案1】:

    SQL 表和结果集代表无序 集(嗯,技术上是多集)。除非您指定 order by 子句,否则没有排序。所以添加一个:

    select OEID, Head, Detail
    from ((select OEID, Chest_Pain as Head, Chest_PainComment as Detail, 1 as ord
           from tblComplaints
          ) union all
          (select OEID, SOB as Head, SOBComment as Detail, 2
           from tblComplaints
          ) union all
          (select OEID, PND as Head, Cyanosis as Detail, 3
           from tblComplaints
          ) union all
          (select OEID, Odema_Feet as Head, Vertigo as Detail, 4
           from tblComplaints
          ) union all
          (select OEID, DM as Head, DMComment as Detail, 5
           from tblComplaints
          ) union all
          (select OEID, RS as Head, RSComment as Detail, 6
           from tblComplaints
          )
         ) as t
    where Head is not null and 
          ltrim(rtrim(Head)) <> '' and
          OEID = 6012
    order by ord;
    

    条件head is not null 是多余的。 &lt;&gt; 负责这个。

    您可以将查询简化为:

    select v.*
    from tblComplaints t cross apply
         (values (Chest_Pain, Chest_PainComment, 1),
                 (SOB, SOBComment, 2),
                 . . .  -- continue with the other values
         ) v(Head, Detail, ord)
    where ltrim(rtrim(Head)) <> '' and
          OEID = 6012
    order by ord;
    

    如果您的数据不是很小,您应该会发现它也具有更好的性能。

    最后,为了回答您的问题,union 正在删除重复项。在这种情况下,它似乎是通过对数据进行排序来实现的。 你不能依赖这种排序——例如,还有其他方法可以删除重复项。

    同样,使用union all 不会对数据进行排序,但这并不意味着结果集将按照您想要的顺序除非您有明确的order by

    【讨论】:

    • 很棒的人。这解决了我的问题。我试图从过去 3 小时开始解决这个问题。你只是在一分钟内解决了它。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2021-01-03
    • 2014-02-09
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多