我想根据一些简单的规则在 PostgreSQL 9.6 中为商家选择最好的报价:
- 无论优惠类型如何,如果优惠的折扣值(value)更大,则该优惠优于其他优惠
- 如果折扣值相等,则福利类型为 ALL 的折扣值高于 FOOD 类型
- 如果折扣和福利类型相同,则可以选择任何优惠,例如选择第一个
所以最好的不仅仅是 max() 调用,而是“条件” max() ,其中也应该检查另一列以确定它是哪一行。
你能帮忙吗?
架构:
create table offer (
id bigserial not null,
discount int4,
benefit_type varchar(25),
...
merchant_id int8 not null
);
查询(部分):
select merchant_id, max(discount) as max_discount
from offer
group by merchant_id;
数据库中的示例报价:
id discount benefit_type ... merchant_id
0 10 FOOD 0
1 20 FOOD 0
2 20 ALL 0
3 30 ALL 1
4 40 ALL 1
5 40 FOOD 1
6 40 ALL 2
7 50 FOOD 2
期望的结果集:
merchant_id max_discount benefit_type
0 20 ALL
1 40 ALL
2 50 FOOD
- 商家 0 的最佳报价是报价 2,因为 20 ALL > 20 FOOD。
- 商家 1 的最佳报价是报价 4,因为 40 ALL > 30 ALL。
- 商家 2 的最佳报价是报价 5,因为 50 FOOD > 40 ALL。
最佳答案
这可以使用 distinct on()
和 benefit_type 的自定义排序定义来实现:
select distinct on (merchant_id) *
from offer
order by merchant_id,
discount desc,
case when benefit_type = 'ALL' then 1 else 2 end;
这更喜欢更高的折扣。如果两个折扣相同,则使用 ALL
的 benefit_type 作为决胜局。
关于sql - PostgreSQL - 从 2 列中选择具有复合最大值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600211/