SQL 帮助:值相关排序?

发布于 2024-11-07 11:47:22 字数 385 浏览 0 评论 0原文

我正在尝试编写一个 SQL 查询来执行一些复杂的排序。我有一个包含一些记录的表,每个记录都有三个重要字段:

  • Group_ID
  • Modified_Order
  • Created_Order

现在,我尝试编写一个查询,首先按组 ID 对这些记录进行排序。然后,如果组 ID 为 1,则按“修改顺序”对这些结果进行排序。如果组ID为2,则按“创建顺序”对它们进行排序。不会出现非 1 或 2 的组 ID。换句话说,我希望结果如下图所示。

在此处输入图像描述

如何编写查询的“ORDER BY”或“GROUP BY”部分来执行此操作?

I am trying to write a SQL query that does some complex sorting. I have a table with some records, each having three important fields:

  • Group_ID
  • Modified_Order
  • Created_Order

Now, I am trying to write a query that first sorts these by the group ID. Then if the group ID is 1, sort those results by their "modified order". If the group ID is 2, sort them by their "created order". Group ID's that are not 1 or 2 will not occur. In other words, I want the results to look like the picture below.

enter image description here

How can I write the "ORDER BY" or "GROUP BY" part of my query to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香橙ぽ 2024-11-14 11:47:22
Select RecordID, GroupID, Modified_Order, Created_Order
FROM TableName
ORDER BY GroupID, Case When GroupID = 1 Then Modified_Order Else Created_Order end
Select RecordID, GroupID, Modified_Order, Created_Order
FROM TableName
ORDER BY GroupID, Case When GroupID = 1 Then Modified_Order Else Created_Order end
烛影斜 2024-11-14 11:47:22
select * from jegad where gr_id in ('1','2') order by gr_id
select * from jegad where gr_id in ('1','2') order by gr_id
会发光的星星闪亮亮i 2024-11-14 11:47:22

怎么样:

 select * from table order by group_id,group_id=1 and modified_order,group_id=2 and created_order;

How about:

 select * from table order by group_id,group_id=1 and modified_order,group_id=2 and created_order;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文