我有这个PostgreSQL
表和数据:
CREATE TABLE info (
brand VARCHAR(255),
segment VARCHAR(255),
name VARCHAR(255)
);
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'SUV', 'Highlander');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'SUV', 'Highlander');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'SUV', 'Highlander');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'SUV', '4Runner');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'SUV', 'RAV4');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'SUV', 'RAV4');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Sedan', 'Camry');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Sedan', 'Camry');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Sedan', 'Corolla');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Sedan', 'Corolla');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Sedan', 'Corolla');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Truck', 'Tacoma');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Truck', 'Tundra');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Truck', 'Tacoma');
INSERT INTO info (brand, segment, name) VALUES ('Toyota', 'Van', 'Sienna');
我执行此查询是为了显示每个分组集的计数,并按每个品牌、细分市场和名称的总计数对其进行排序:
SELECT
brand,
segment,
name,
count (1) as total
FROM
info
GROUP BY
GROUPING SETS (
(brand),
(brand, segment),
(brand, segment,name)
)
ORDER BY
max(count (1)) over (partition by brand) desc,
max(count (1)) over (partition by brand,segment) desc,
count (1) desc;
This fiddle显示了它的外观。
现在,我只想选择每个品牌的前2个细分市场,以及每个品牌/细分市场的前1个名称。因此,结果应该如下所示:
| 烙印|段|姓名|总计|
| - ------|- ------|- ------|- ------|
| 丰田|||十五|
| 丰田|越野车||六个|
| 丰田|越野车|汉兰达|三个|
| 丰田|轿车||五个|
| 丰田|轿车|花冠|三个|
我试过使用窗口函数,但结果不是我所期望的
1条答案
按热度按时间zphenhs41#
尝试按如下方式使用ROW_NUMBER函数:
See demo
另一种解决方案。您可以对每个品牌细分组使用按max(count)排序的dense_rank函数,如下所示:
WHERE segment_rank <= 3
这将检索每个品牌的两个段,加上一个,因为包括了基本品牌(其中段为空)。AND name_rank <= 2
这将为每个段检索一个名称,加上一个,因为包括了基本段(其中名称为空)。dense_rank函数的使用是为了在平局的情况下,即当存在具有相同max(count)的多个段/名称时,获得所有的(段,名称)。