gpt4 book ai didi

sql - 按sql中的另一列对具有重复值的列进行排序

转载 作者:搜寻专家 更新时间:2023-10-30 20:03:44 24 4
gpt4 key购买 nike

我有下表:

+------+--------+
| Type |Location|
+------+--------+
| 0003 | US |
| 0010 | US |
| 0007 | CA |
| 0013 | MX |
| 0003 | EU |
| 0007 | MX |
| 0219 | CN |
+------+--------+

我需要按“位置”排序,除非在“类型”列中发现重复项。然后下一行应该是重复的(或更多重复的),然后再继续按“位置”排序。

预期结果:

+------+--------+
| Type |Location|
+------+--------+
| 0007 | CA |
| 0007 | MX | <---- Exception (duplicate of 0007)
| 0219 | CN |
| 0003 | EU |
| 0003 | US | <---- Exception (duplicate of 0003)
| 0013 | MX |
| 0010 | US |
+------+--------+

我试过使用 cte 和 row_number,但我无法接近。我不确定这是否是一个好方法,但我想不出任何其他解决方案。

最佳答案

你可以用order by中的窗口函数做你想做的事:

select t.*
from t
order by (min(location) over (partition by type)),
location

关于sql - 按sql中的另一列对具有重复值的列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56599387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com