当前位置:网站首页>How to solve the problem of store ranking?
How to solve the problem of store ranking?
2022-07-29 04:06:00 【Monkey data analysis】
【 Interview questions 】
There are two tables ,“ Product list ” contain 2 A field : product 、 product line .
“ The order sheet ” contain 7 A field : Order id、 customer id、 Transaction date 、 product 、 The store 、 number 、 amount of money .
problem : Query product line (category_3) The store with the second largest sales volume .
【 The problem solving steps 】
1. Their thinking
The problem is to analyze the sales of a product line . Can calculate “ sales ” Field of “ number ” stay “ The order sheet ” in ,“ product line ” stay “ Product list ” in .
therefore , It involves two tables , Think of it 《 Monkeys learn from scratch SQL》 It's used in “ Multi-table query ”
In order to retain “ The order sheet ” All the data in , With “ The order sheet ” It is the left table ( Record as sub query a1), Make left connection .
“ Product list ” It's the right table ( Record as sub query a2).
The association condition is field “ product ”.
select Field
from
a1
left join
a2
on a1. product = a2. product
Add subquery a1、a2 Plug in :
select a1.*,a2. product line
from
The order sheet as a1
left join
Product list as a2
on a1. product = a2. product ;
Query results :
Will be more than SQL Record as sub query a3.
3. Summary analysis
The problem is to check the product line (category_3) The store with the second largest sales volume , therefore :
1) Just focus on the product line category_3 The order of ;
2) Summarize by store , To get the product line (category_3) Sales volume in each store ;
3) Sales of “ number ” Sum up , Because each order is not necessarily only 1 Products .
select a3. The store ,
sum( number ) as category_3 sales
from a3
where a3. product line = 'category_3'
group by a3. The store ;
Add subquery a3 Plug in :
select a3. The store ,
sum( number ) as category_3 sales
from (
select a1.*,a2. product line
from
The order sheet as a1
left join
Product list as a2
on a1. product = a2. product
) as a3
where a3. product line = 'category_3'
group by a3. The store ;
Will be more than SQL Record as sub query a4.
4. Window function
Query ranking generally uses window functions , There are three common window functions about ranking :
Prevent the situation of parallel ranking , We choose dense_rank().
select *,dense_rank() over(order by category_3 sales desc) as ranking
from a4;
Add subquery a4 Plug in :
select *,dense_rank() over(order by a4.category_3 sales desc) as ranking
from (
select a3. The store ,
sum( number ) as category_3 sales
from (
select a1.*,a2. product line
from
The order sheet as a1
left join
Product list as a2
on a1. product = a2. product
) as a3
where a3. product line = 'category_3'
group by a3. The store
) as a4;
Will be more than SQL Record as sub query a5.
Last , The selected ranking is 2 Our shop :
select The store
from a5
where ranking = 2;
Add subquery a5 Plug in :
select a5. The store
from (
select *,dense_rank() over(order by a4.category_3 sales desc) as ranking
from (
select a3. The store ,
sum( number ) as category_3 sales
from (
select a1.*,a2. product line
from
The order sheet as a1
left join
Product list as a2
on a1. product = a2. product
) as a3
where a3. product line = 'category_3'
group by a3. The store
) as a4
) as a5
where a5. ranking = 2;
【 The test point of this question 】
1) Check your understanding of group summary , And flexible use to solve business problems ;
2) Check your understanding of multi table connections , And flexible use to solve business problems ;
3) Check your understanding of window functions .
️ Click on 「 Read the original 」
Free registration Data analysis training camp
边栏推荐
- 数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
- [introduction to C language] zzulioj 1031-1035
- 有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
- UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
- Typescript from getting started to mastering (XXII) namespace namespace (I)
- Problems encountered in vscode connection SSH
- Asp. Net MVC, how can the controller in the folder jump to the controller in the root directory?
- Communication between parent-child components and parent-child components provide and inject
- 为什么opengauss启动的时候这么多的unknown?
- The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
猜你喜欢
【深度学习CPU(番外篇)——虚拟内存】
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
Press the missing number of interview question 17.04 | | 260. the number that appears only once (including bit operation knowledge points)
Deep understanding of browser caching mechanism (HTTP)
SSL==证书相关概念
Is the array name a pointer
初识C语言(3)
VScode连接ssh遇到的问题
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
随机推荐
Array as function parameter -- pointer constant / constant pointer
About the writing of ALV format control part
mmdetection初步使用
First knowledge of C language (3)
"Weilai Cup" 2022 Niuke summer multi school training camp 2H
C language: getchar () and cache
Basic configuration of BGP - establish peers and route announcements
OPENSQL快速学习
How to execute insert into select from job in SQL client
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
opengauss预检查安装
Let variable declaration feature of ES6 new feature and its case
Codeforces round 810 (Div. 2) d. rain (segment tree difference)
【深度学习CPU(番外篇)——虚拟内存】
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
Use case of arrow function of new features in ES6
LVS+KeepAlived高可用部署实战应用
Opensql quick learning
AssertionError(“Torch not compiled with CUDA enabled“)
Analysis of new retail o2o e-commerce model