我知道如何构建一个SELECT语句,按照产品代码和子模型列表的数量给出排名前10位的条目,但是如何在排名上更深入一级?那就是列出前10名排名模型中排名前5位(按计数)的子模型?
例如:
CREATE TABLE RANKTEST (
"MODEL" VARCHAR2(5 BYTE),
"SUBMODEL" VARCHAR2(5 BYTE) ) ;
insert into ranktest values('A100','R1');
insert into ranktest values('A100','R1');
insert into ranktest values('A100','R1');
insert into ranktest values('A100','R1');
insert into ranktest values('A100','R2');
insert into ranktest values('A100','R2');
insert into ranktest values('A100','R3');
insert into ranktest values('A100','R4');
insert into ranktest values('A103','R5');
insert into ranktest values('A103','R5');
insert into ranktest values('A103','R5');
insert into ranktest values('A103','R5');
insert into ranktest values('A103','X1');
insert into ranktest values('A103','X1');
insert into ranktest values('A103','X1');
insert into ranktest values('A103','X2');
insert into ranktest values('A103','X2');
insert into ranktest values('A103','X3');
insert into ranktest values('A104','R1');
insert into ranktest values('A104','R2');
insert into ranktest values('A105','R2');
insert into ranktest values('A105','R3');
insert into ranktest values('A105','R4');
commit;
SELECT model,
cnt,
rank
FROM
(SELECT model,
COUNT(*) AS cnt,
DENSE_RANK() OVER(ORDER BY COUNT(model) DESC) AS rank
FROM
( SELECT * FROM ranktest )
GROUP BY model )
WHERE rank <=3;
This gives the result:
MODEL CNT RANK
A103 10 1
A100 8 2
A105 3 3
我正在寻找SELECT语法来显示排名前3位的模型排名前2位的子模型
MODEL SUBMODEL CNT RANK SUB-COUNT SUB-RANK
A103 R5 10 1 4 1
A103 X1 10 1 3 2
A100 R1 8 2 4 1
A100 R2 8 2 2 2
A105 R2 3 3 1 1
A105 R3 3 3 1 1
A105 R4 3 3 1 1
(模型A105及其所有子模型的子排名是使用DENSE_RANK()的结果,至少我认为它应该看起来。)
答案 0 :(得分:1)
我首先计算模型和子模型的数量。
查询1 :
select model_, submodel_,
count(*) submodel_count,
sum(count(*)) over (partition by model_) model_count
from ranktest
group by model_, submodel_
order by 1,2,3 desc
<强> Results 强>:
| MODEL_ | SUBMODEL_ | SUBMODEL_COUNT | MODEL_COUNT |
|--------|-----------|----------------|-------------|
| A100 | R1 | 4 | 8 |
| A100 | R2 | 2 | 8 |
| A100 | R3 | 1 | 8 |
| A100 | R4 | 1 | 8 |
| A103 | R5 | 4 | 10 |
| A103 | X1 | 3 | 10 |
| A103 | X2 | 2 | 10 |
| A103 | X3 | 1 | 10 |
| A104 | R1 | 1 | 2 |
| A104 | R2 | 1 | 2 |
| A105 | R2 | 1 | 3 |
| A105 | R3 | 1 | 3 |
| A105 | R4 | 1 | 3 |
然后使用dense_rank函数指定排名。
查询2 :
select model_, submodel_, model_count, submodel_count,
dense_rank() over (order by model_count desc) model_rank,
dense_rank() over (partition by model_ order by submodel_count desc) submodel_rank
from (
select model_, submodel_,
count(*) submodel_count,
sum(count(*)) over (partition by model_) model_count
from ranktest
group by model_, submodel_
)
order by 1,2,5,6
<强> Results 强>:
| MODEL_ | SUBMODEL_ | MODEL_COUNT | SUBMODEL_COUNT | MODEL_RANK | SUBMODEL_RANK |
|--------|-----------|-------------|----------------|------------|---------------|
| A100 | R1 | 8 | 4 | 2 | 1 |
| A100 | R2 | 8 | 2 | 2 | 2 |
| A100 | R3 | 8 | 1 | 2 | 3 |
| A100 | R4 | 8 | 1 | 2 | 3 |
| A103 | R5 | 10 | 4 | 1 | 1 |
| A103 | X1 | 10 | 3 | 1 | 2 |
| A103 | X2 | 10 | 2 | 1 | 3 |
| A103 | X3 | 10 | 1 | 1 | 4 |
| A104 | R1 | 2 | 1 | 4 | 1 |
| A104 | R2 | 2 | 1 | 4 | 1 |
| A105 | R2 | 3 | 1 | 3 | 1 |
| A105 | R3 | 3 | 1 | 3 | 1 |
| A105 | R4 | 3 | 1 | 3 | 1 |
最后,过滤掉顶级模型和子模型。
查询3 :
select model_, submodel_, model_count, submodel_count,
model_rank, submodel_rank
from (
select model_, submodel_, model_count, submodel_count,
dense_rank() over (order by model_count desc) model_rank,
dense_rank() over (partition by model_ order by submodel_count desc) submodel_rank
from (
select model_, submodel_,
count(*) submodel_count,
sum(count(*)) over (partition by model_) model_count
from ranktest
group by model_, submodel_
)
)
where model_rank <=3 and submodel_rank <=2
<强> Results 强>:
| MODEL_ | SUBMODEL_ | MODEL_COUNT | SUBMODEL_COUNT | MODEL_RANK | SUBMODEL_RANK |
|--------|-----------|-------------|----------------|------------|---------------|
| A103 | R5 | 10 | 4 | 1 | 1 |
| A103 | X1 | 10 | 3 | 1 | 2 |
| A100 | R1 | 8 | 4 | 2 | 1 |
| A100 | R2 | 8 | 2 | 2 | 2 |
| A105 | R2 | 3 | 1 | 3 | 1 |
| A105 | R4 | 3 | 1 | 3 | 1 |
| A105 | R3 | 3 | 1 | 3 | 1 |