Postgresql 按多列分组并在每个组内排序
Postgresql group by multiple columns and sort within each group
我有一个 table 包含以下列:
order_id
amount
rating
percent
customer_id
ref_id
doc_id
1
1000
1
0.6
112
8
5
2
2000
2
0.1
111
8
8
2
3000
3
0.2
110
8
6
3
4000
5
0.1
100
7
7
3
4000
2
0.7
124
7
9
3
5000
4
0.6
143
5
10
4
2000
6
0.4
125
4
11
4
2500
1
0.55
185
4
12
4
1000
4
0.42
168
5
13
4
1200
8
0.8
118
1
14
对于每个 order_id 我想找到 doc_id 具有最高数量、最高评级、最高百分比、最低 customer_id。
对于单个订单 ID,我可以这样做:
select order_id, doc_id
from orders
where order_id = 1625
order by amount desc nulls last,
rating desc nulls last,
percent desc nulls last,
customer_id asc
limit 1;
但我没能完成所有订单。所以输出应该是这样的:
order_id
doc_id
1
5
2
6
3
10
4
12
我正在使用 Postgresql。
知道我应该怎么做吗?
您似乎需要在 window 中实现行编号,按您想要的标准排序:
with t as (
select *,
Row_Number()
over(partition by order_id
order by amount desc, rating desc, percent desc, customer_id
) seq
from Yourtable
)
select order_id, doc_id
from t
where seq=1
使用FIRST_VALUE()
window函数:
SELECT DISTINCT order_id,
FIRST_VALUE(doc_id) OVER (
PARTITION BY order_id
ORDER BY amount DESC NULLS LAST, rating DESC NULLS LAST, percent DESC NULLS LAST, customer_id
) doc_id
FROM orders;
参见demo。
我有一个 table 包含以下列:
order_id | amount | rating | percent | customer_id | ref_id | doc_id |
---|---|---|---|---|---|---|
1 | 1000 | 1 | 0.6 | 112 | 8 | 5 |
2 | 2000 | 2 | 0.1 | 111 | 8 | 8 |
2 | 3000 | 3 | 0.2 | 110 | 8 | 6 |
3 | 4000 | 5 | 0.1 | 100 | 7 | 7 |
3 | 4000 | 2 | 0.7 | 124 | 7 | 9 |
3 | 5000 | 4 | 0.6 | 143 | 5 | 10 |
4 | 2000 | 6 | 0.4 | 125 | 4 | 11 |
4 | 2500 | 1 | 0.55 | 185 | 4 | 12 |
4 | 1000 | 4 | 0.42 | 168 | 5 | 13 |
4 | 1200 | 8 | 0.8 | 118 | 1 | 14 |
对于每个 order_id 我想找到 doc_id 具有最高数量、最高评级、最高百分比、最低 customer_id。
对于单个订单 ID,我可以这样做:
select order_id, doc_id
from orders
where order_id = 1625
order by amount desc nulls last,
rating desc nulls last,
percent desc nulls last,
customer_id asc
limit 1;
但我没能完成所有订单。所以输出应该是这样的:
order_id | doc_id |
---|---|
1 | 5 |
2 | 6 |
3 | 10 |
4 | 12 |
我正在使用 Postgresql。 知道我应该怎么做吗?
您似乎需要在 window 中实现行编号,按您想要的标准排序:
with t as (
select *,
Row_Number()
over(partition by order_id
order by amount desc, rating desc, percent desc, customer_id
) seq
from Yourtable
)
select order_id, doc_id
from t
where seq=1
使用FIRST_VALUE()
window函数:
SELECT DISTINCT order_id,
FIRST_VALUE(doc_id) OVER (
PARTITION BY order_id
ORDER BY amount DESC NULLS LAST, rating DESC NULLS LAST, percent DESC NULLS LAST, customer_id
) doc_id
FROM orders;
参见demo。