简体   繁体   English

如何编写SQL查询以对数据集进行排序或分组

[英]How to write a sql query to Sort or Group the dataset

I have data in a table A as shown below. 我在表A中有数据,如下所示。 I want to group memberID as one set and sort the Date_Time in each set as shown in snapshot below 我想将memberID分组为一组,并对每组中的Date_Time进行排序,如下面的快照所示

Date_Time        PersonID     Status
4/2/14 10:15 AM    ghi        Closed
4/1/14 9:15 AM     ghi        Cancelled
4/1/14 11:00 AM    abc        Cancelled
4/2/14 8:12 AM     def        Closed
4/1/14 9:17 AM     def        Hold
4/3/14 2:17 PM     abc        Hold
4/2/14 8:30 AM     abc        Open
4/3/14 8:16 AM     jkl        Closed
4/1/14 12:10 PM    jkl        Open
4/1/14 11:30 AM    abc        Hold

The final example-snapshot attached below you will see memberID: ghi first row date time '4/1/2014 9:15:00 AM' is greater than memberID: def 1st row date_Time '4/1/2014 9:17:00 AM' highlighted in yellow. 下面附带的最后一个示例快照将显示memberID: ghi第一行日期时间'4/1/2014 9:15:00 AM'大于memberID: def 1st row date_Time'4 / 1/2014 9:17:00 AM'以黄色突出显示。 And that is the main reason memberID ghi set tweaks as first set and then follows by memberID def set and then meberID abc , jkl etc... 这就是memberID ghi将调整设置为第一组然后跟随memberID def set然后meberID abcjkl ...等的主要原因 ...

在此输入图像描述

Could some one please help me how to write MS-SQL query to achieve the final result. 有人可以帮我如何编写MS-SQL查询来实现最终结果。

Thank you so much for your help. 非常感谢你的帮助。

If I'm understanding your question correctly, you need to join the table back to itself using the min aggregate to establish a sort order: 如果我正确理解你的问题,你需要join使用的表背到自身min骨料建立排序顺序:

select t.*
from yourtable t
  join (
    select personid, 
      min(date_time) min_date_time
    from yourtable
    group by personid
    ) t2 on t.personid = t2.personid
order by t2.min_date_time, t.date_time

Here's another way : 这是另一种方式

SELECT
  Date_Time,
  PersonID,
  Status
FROM
  dbo.atable
ORDER BY
  MIN(Date_Time) OVER (PARTITION BY PersonID),
  PersonID,
  Date_Time
;

The approach, though, is same as in sgeddes's answer , it just has different syntax. 不过,这种方法与sgeddes的答案相同,它只是有不同的语法。 It calculates minimum Date_Time values using MIN(...) OVER (...) . 它使用MIN(...) OVER (...)计算最小Date_Time值。 That makes a join to a derived table completely unnecessary. 这使得对派生表的连接完全没有必要。

One other little difference is that I have added PersonID to the ORDER BY clause to make sure that people with identical minimum Date_Time values do not have their rows mixed up. 另一个小区别是我已经将PersonID添加到ORDER BY子句中,以确保具有相同最小Date_Time值的人不会混淆他们的行。

Have you tried to add Order By PersonID, Date_Time in the end of your select statement ? 您是否尝试在select语句的末尾添加Order By PersonID, Date_Time

Please note that sorting the Date_Time column will not work for you as expected if it is a string and not a Date field. 请注意,如果Date_Time列是字符串而不是Date字段,则对Date_Time列进行排序将无法正常工作。

Provide a code sample of what you did so far so that we will be able to understand your problem more clearly so that we cab help you more efficiently. 提供您目前所做工作的代码示例,以便我们能够更清楚地了解您的问题,以便我们更有效地帮助您。

    SELECT Date_Time,PersonID,[Status] 
    FROM TABLE1
    GROUP BY Date_Time,PersonID,[Status] 
    ORDER BY PersonID,Date_Time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM