SQL 中 GROUP BY 的排序问题

发布于 2024-11-30 14:13:00 字数 1451 浏览 2 评论 0原文

SELECT TOP 10 tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
FROM         tb_OrderItem INNER JOIN
                        (SELECT * FROM tb_Show WHERE IsDeleted = 0 AND PrivateShow = 0 AND Archive = 0 ) tb_Show
                      ON tb_OrderItem.ShowId = tb_Show.showId             
GROUP BY tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
ORDER BY Quantity DESC 
  • 160 dsadsadsadsa dsadsadsadsa 3
  • 160 dsadsadsadsa dsadsadsadsa 2
  • 150 基于 css 的地图测试事件 css-based-map-test-event 1
  • 159 交叉空心测试交叉-hollows-test 1

但我不想重复ID ie 160。所以我将其设置为子查询,并且仅获得带有 DISTINCT 关键字的三列。问题是,当我将其移动到带有 DISTINCT 关键字的子查询时,按数量排序不起作用,而没有排序是可以的,但是不同的关键字删除排序...


SELECT DISTINCT TOP 10 showId, Title, PageName  from 
( SELECT TOP 10 tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
FROM         tb_OrderItem INNER JOIN
                        (SELECT * FROM tb_Show WHERE IsDeleted = 0 AND PrivateShow = 0 AND Archive = 0 ) tb_Show
                      ON tb_OrderItem.ShowId = tb_Show.showId             
GROUP BY tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
ORDER BY Quantity DESC ) tb_show
  • 150 基于 css 的地图测试事件 css-based-map-test-event
  • 159 交叉空心测试 cross-hollows-test
  • 160萨德萨德萨萨德萨德萨德萨
SELECT TOP 10 tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
FROM         tb_OrderItem INNER JOIN
                        (SELECT * FROM tb_Show WHERE IsDeleted = 0 AND PrivateShow = 0 AND Archive = 0 ) tb_Show
                      ON tb_OrderItem.ShowId = tb_Show.showId             
GROUP BY tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
ORDER BY Quantity DESC 
  • 160 dsadsadsadsa dsadsadsadsa 3
  • 160 dsadsadsadsa dsadsadsadsa 2
  • 150 css based map test event css-based-map-test-event 1
  • 159 cross hollows test cross-hollows-test 1

But I dont want to repeat ID i.e. 160. So I made it subquery and get only three coloumns with DISTINCT keywords .. Issue is that sorting by quantity is not working when I move it into subquery with DISTINCT keywords without it sorting is fine but distinct keywords remove sorting...


SELECT DISTINCT TOP 10 showId, Title, PageName  from 
( SELECT TOP 10 tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
FROM         tb_OrderItem INNER JOIN
                        (SELECT * FROM tb_Show WHERE IsDeleted = 0 AND PrivateShow = 0 AND Archive = 0 ) tb_Show
                      ON tb_OrderItem.ShowId = tb_Show.showId             
GROUP BY tb_show.showId, tb_Show.Title, tb_Show.PageName, Quantity
ORDER BY Quantity DESC ) tb_show
  • 150 css based map test event css-based-map-test-event
  • 159 cross hollows test cross-hollows-test
  • 160 dsadsadsadsa dsadsadsadsa

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

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

发布评论

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

评论(1

地狱即天堂 2024-12-07 14:13:00

这将为您提供第二个示例中的输出。不同子查询对您不起作用的原因是因为您还按数量进行分组,这导致输出单独的不同行。

SELECT TOP 10 tb_show.showId, tb_Show.Title, tb_Show.PageName, MAX(quantity) as Quantity
FROM tb_OrderItem 
    INNER JOIN tb_Show 
        ON tb_OrderItem.ShowId = tb_Show.showId 
WHERE tb_show.IsDeleted = 0 AND tb_show.PrivateShow = 0 AND tb_show.Archive = 0 
GROUP BY tb_show.showId, tb_Show.Title, tb_Show.PageName
ORDER BY MAX(quantity)

This will give you the output in your second example. The reason the distinct subquery wasn't working for you was because you were also grouping by quantity which caused separate distinct rows to be output.

SELECT TOP 10 tb_show.showId, tb_Show.Title, tb_Show.PageName, MAX(quantity) as Quantity
FROM tb_OrderItem 
    INNER JOIN tb_Show 
        ON tb_OrderItem.ShowId = tb_Show.showId 
WHERE tb_show.IsDeleted = 0 AND tb_show.PrivateShow = 0 AND tb_show.Archive = 0 
GROUP BY tb_show.showId, tb_Show.Title, tb_Show.PageName
ORDER BY MAX(quantity)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文