SQL 中 GROUP BY 的排序问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将为您提供第二个示例中的输出。不同子查询对您不起作用的原因是因为您还按数量进行分组,这导致输出单独的不同行。
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.