admin管理员组文章数量:1023744
Here is a sample of my data
select * from (
select 'A' as POD, 164 as result from dual union all
select 'A' as POD, 3 as result from dual union all
select 'A' as POD, 2 as result from dual union all
select 'B' as POD, 409 as result from dual union all
select 'B' as POD, 128 as result from dual union all
select 'B' as POD, 5 as result from dual union all
select 'C' as POD, 12391 as result from dual union all
select 'C' as POD, 624 as result from dual union all
select 'C' as POD, 405 as result from dual union all
select 'C' as POD, 26 as result from dual union all
select 'C' as POD, 3 as result from dual union all
select 'C' as POD, 2 as result from dual
)
POD | RESULT |
---|---|
A | 164 |
A | 3 |
A | 2 |
B | 409 |
B | 128 |
B | 5 |
C | 12391 |
C | 624 |
C | 405 |
C | 26 |
Here is a sample of my data
select * from (
select 'A' as POD, 164 as result from dual union all
select 'A' as POD, 3 as result from dual union all
select 'A' as POD, 2 as result from dual union all
select 'B' as POD, 409 as result from dual union all
select 'B' as POD, 128 as result from dual union all
select 'B' as POD, 5 as result from dual union all
select 'C' as POD, 12391 as result from dual union all
select 'C' as POD, 624 as result from dual union all
select 'C' as POD, 405 as result from dual union all
select 'C' as POD, 26 as result from dual union all
select 'C' as POD, 3 as result from dual union all
select 'C' as POD, 2 as result from dual
)
POD | RESULT |
---|---|
A | 164 |
A | 3 |
A | 2 |
B | 409 |
B | 128 |
B | 5 |
C | 12391 |
C | 624 |
C | 405 |
C | 26 |
And what I want is for them to be sorted by group with highest first count:
POD | RESULT |
---|---|
C | 12391 |
C | 624 |
C | 405 |
C | 26 |
B | 409 |
B | 128 |
B | 5 |
A | 164 |
A | 3 |
A | 2 |
I am not even sure how to express this as SQL
C
has the highest result
in the first row, then B
has the next highest then A
- Please explain precisely what leads to showing C before B before A. – Thorsten Kettner Commented Nov 19, 2024 at 8:30
- updated for clarity – Christian Bongiorno Commented Nov 20, 2024 at 1:31
2 Answers
Reset to default 3I understand the task as follows:
- Look at the pods' results. Show the pod with the highest result first, then the pod with the next highest, etc.
- Order the rows in each pod by result descending.
Now, what do we consider the highest result? Is this the maximum result per pod? That would be A = 164, B = 409, C = 12391. We would get this with
MAX(result) OVER (PARTITION BY pod)
Or is it the maximum result sum per pod? That would be A = 169, B = 542, C = 13446. And the expression we'd need is
SUM(result) OVER (PARTITION BY pod)
Use one or the other in your ORDER BY
clause. E.g.:
SELECT pod, result
FROM mytable
ORDER BY
MAX(result) OVER (PARTITION BY pod) DESC,
pod,
result DESC;
another way is by using the row_number in this way to sort your data desc:
select * from (
select 'A' as POD, 164 as result from dual union all
select 'A' as POD, 3 as result from dual union all
select 'A' as POD, 2 as result from dual union all
select 'B' as POD, 409 as result from dual union all
select 'B' as POD, 128 as result from dual union all
select 'B' as POD, 5 as result from dual union all
select 'C' as POD, 12391 as result from dual union all
select 'C' as POD, 624 as result from dual union all
select 'C' as POD, 405 as result from dual union all
select 'C' as POD, 26 as result from dual union all
select 'C' as POD, 3 as result from dual union all
select 'C' as POD, 2 as result from dual
)
order by last_value(result) OVER (PARTITION BY pod order by result rows between unbounded preceding and unbounded following) desc, result desc
Hope it helps.
Here is a sample of my data
select * from (
select 'A' as POD, 164 as result from dual union all
select 'A' as POD, 3 as result from dual union all
select 'A' as POD, 2 as result from dual union all
select 'B' as POD, 409 as result from dual union all
select 'B' as POD, 128 as result from dual union all
select 'B' as POD, 5 as result from dual union all
select 'C' as POD, 12391 as result from dual union all
select 'C' as POD, 624 as result from dual union all
select 'C' as POD, 405 as result from dual union all
select 'C' as POD, 26 as result from dual union all
select 'C' as POD, 3 as result from dual union all
select 'C' as POD, 2 as result from dual
)
POD | RESULT |
---|---|
A | 164 |
A | 3 |
A | 2 |
B | 409 |
B | 128 |
B | 5 |
C | 12391 |
C | 624 |
C | 405 |
C | 26 |
Here is a sample of my data
select * from (
select 'A' as POD, 164 as result from dual union all
select 'A' as POD, 3 as result from dual union all
select 'A' as POD, 2 as result from dual union all
select 'B' as POD, 409 as result from dual union all
select 'B' as POD, 128 as result from dual union all
select 'B' as POD, 5 as result from dual union all
select 'C' as POD, 12391 as result from dual union all
select 'C' as POD, 624 as result from dual union all
select 'C' as POD, 405 as result from dual union all
select 'C' as POD, 26 as result from dual union all
select 'C' as POD, 3 as result from dual union all
select 'C' as POD, 2 as result from dual
)
POD | RESULT |
---|---|
A | 164 |
A | 3 |
A | 2 |
B | 409 |
B | 128 |
B | 5 |
C | 12391 |
C | 624 |
C | 405 |
C | 26 |
And what I want is for them to be sorted by group with highest first count:
POD | RESULT |
---|---|
C | 12391 |
C | 624 |
C | 405 |
C | 26 |
B | 409 |
B | 128 |
B | 5 |
A | 164 |
A | 3 |
A | 2 |
I am not even sure how to express this as SQL
C
has the highest result
in the first row, then B
has the next highest then A
- Please explain precisely what leads to showing C before B before A. – Thorsten Kettner Commented Nov 19, 2024 at 8:30
- updated for clarity – Christian Bongiorno Commented Nov 20, 2024 at 1:31
2 Answers
Reset to default 3I understand the task as follows:
- Look at the pods' results. Show the pod with the highest result first, then the pod with the next highest, etc.
- Order the rows in each pod by result descending.
Now, what do we consider the highest result? Is this the maximum result per pod? That would be A = 164, B = 409, C = 12391. We would get this with
MAX(result) OVER (PARTITION BY pod)
Or is it the maximum result sum per pod? That would be A = 169, B = 542, C = 13446. And the expression we'd need is
SUM(result) OVER (PARTITION BY pod)
Use one or the other in your ORDER BY
clause. E.g.:
SELECT pod, result
FROM mytable
ORDER BY
MAX(result) OVER (PARTITION BY pod) DESC,
pod,
result DESC;
another way is by using the row_number in this way to sort your data desc:
select * from (
select 'A' as POD, 164 as result from dual union all
select 'A' as POD, 3 as result from dual union all
select 'A' as POD, 2 as result from dual union all
select 'B' as POD, 409 as result from dual union all
select 'B' as POD, 128 as result from dual union all
select 'B' as POD, 5 as result from dual union all
select 'C' as POD, 12391 as result from dual union all
select 'C' as POD, 624 as result from dual union all
select 'C' as POD, 405 as result from dual union all
select 'C' as POD, 26 as result from dual union all
select 'C' as POD, 3 as result from dual union all
select 'C' as POD, 2 as result from dual
)
order by last_value(result) OVER (PARTITION BY pod order by result rows between unbounded preceding and unbounded following) desc, result desc
Hope it helps.
本文标签: sqlIn Oracle How to sort by groupcluster by a column valueStack Overflow
版权声明:本文标题:sql - In Oracle: How to sort by groupcluster by a column value - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745601577a2158487.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论