当前位置:网站首页>Real MySQL interview question (18) -- practical operation analysis
Real MySQL interview question (18) -- practical operation analysis
2022-06-22 07:12:00 【Socialphobia_ FOGO】
There is... In a company's database 3 A watch , Sales order form 、 Product list 、 List of sales outlets .
” Sales order form ” Recorded sales , Each piece of data indicates which customer 、 On which day 、 Which outlet bought what products , What is the number of purchases , And the retail price of the corresponding product .
“ Product list ” Records the details of the company's products .
“ List of sales outlets ” Recorded the company's sales outlets .
The sales order table and product detail table are passed through “ product ” Field Association , The sales order form and sales outlets are passed through “ Trading outlets ” relation .
#1. Analysis in 2020 Number of buyers in the first quarter of the year , Sales amount , Customer unit price , A single guest , Purchase frequency per capita
SELECT
COUNT(DISTINCT customer ID) AS Number of buyers ,
SUM( sales volumes * Retail price ) AS Sales amount ,
SUM( sales volumes * Retail price ) / COUNT(DISTINCT customer ID) AS Customer unit price ,
SUM( sales volumes ) / COUNT(DISTINCT customer ID) AS A single guest ,
COUNT(DISTINCT The order number ) / COUNT(DISTINCT customer ID) AS Purchase frequency per capita
FROM
Sales order form
WHERE Transaction date BETWEEN '2020-01-01'
AND '2020-03-31'

#2. Analyze the brand in 2019.5-2020.4 Repurchase rate during the period ( Repeat purchase rate : Place orders during this period >1 The number of people / Total number of buyers )
SELECT
SUM(
CASE
WHEN Order each customer > 1
THEN 1
ELSE 0
END
) / COUNT(*) AS Repeat purchase rate
FROM
(SELECT
customer ID,
COUNT(DISTINCT The order number ) AS Order each customer
FROM
Sales order form
WHERE Transaction date BETWEEN '2019-05-01'
AND '2020-04-30'
GROUP BY customer ID) a

#3. Find both purchased ProductA Bought again ProductB, But didn't buy ProductC Number of customers , And calculate the average customer unit price
SELECT
COUNT(DISTINCT customer ID) AS Number of customers ,
SUM( sales volumes * Retail price ) / COUNT(DISTINCT The order number ) AS Average customer price
FROM
Sales order form
WHERE customer ID IN
(SELECT
customer ID
FROM
(SELECT
customer ID,
GROUP_CONCAT(DISTINCT product ) AS Product portfolio
FROM
Sales order form
GROUP BY customer ID) a
WHERE Product portfolio LIKE '%ProductA%'
AND Product portfolio LIKE '%ProductB%'
AND Product portfolio NOT LIKE '%ProductC%')
GROUP BY customer ID
Image Name

#4. Find the second largest customer in each city , List the cities they bought 、 customer ID、 Purchase amount
SELECT
*
FROM
(SELECT
*,
dense_rank () over (
PARTITION BY City
ORDER BY Purchase amount DESC
) AS ranking
FROM
(SELECT
b. City ,
a. customer ID,
SUM( sales volumes * Retail price ) AS Purchase amount
FROM
Sales order form a
JOIN List of sales outlets b
ON a. Trading outlets = b. Trading outlets
GROUP BY b. City ,
a. customer ID) a) b
WHERE ranking = 2

#5. Calculate the number of stores in each city and the business summary of each city , The output contains cities without purchase records
SELECT
City ,
COUNT(DISTINCT Trading outlets ) AS Number of stores in the city ,
SUM( sales volumes * Retail price ) AS Business summary
FROM
List of sales outlets
LEFT JOIN Sales order form USING ( Trading outlets )
GROUP BY City

边栏推荐
- [Gan] Introduction to Gan basics and dcgan
- How can we effectively alleviate anxiety? See what ape tutor says
- JS中对数组进行去重的几种方法
- OpenGL - Draw Triangle
- Canoe learning notes (3) graphic window introduction diagram
- Map of STL knowledge summary
- JDBC查询结果集,结果集转化成表
- Résumé semestriel du programmeur de 33 ans
- [out of distribution detection] deep analog detection with outlier exposure ICLR '19
- Yolov1 (prediction process)
猜你喜欢

MySQL面试真题(二十)——视频数据分析实战

The mountains and rivers are heavy, and there is no doubt that there is no way out. There is another village where the willows are dark and the flowers are bright.

OpenGL - Textures

JS implementation of random generation of 16 bit key -- Basic accumulation

【GAN】SAGAN ICML‘19

Choco usage notes -- how to set the default package installation location of choco

校招路上的坑
![[Gan] Introduction to Gan basics and dcgan](/img/93/f0287f93283707b7082630cb89daf1.jpg)
[Gan] Introduction to Gan basics and dcgan

5g NR PWS system

golang調用sdl2,播放pcm音頻,報錯signal arrived during external code execution。
随机推荐
Antd - a-upload-dragger drag upload component - Basic accumulation
圣杯布局和双飞翼布局的区别
Buuctf part Title WP
Canoe learning notes (6) diagram of logging block data recording module
自定义实现JS中的bind方法
Rebuild binary tree
Jupyter notebook file storage location
[out of distribution detection] deep analog detection with outlier exposure ICLR '19
Vue failed to connect to MySQL database
Several methods of array de duplication in JS
CONDA installation method of deepmd kit and the solution of slow speed
On vector norm
The solution of word document being locked and unable to edit
Network layer: IP protocol
ROS QT environment construction
OpenGL - Draw Triangle
5G NR PWS系统
Theory and application of naturallanguageprocessing
校招路上的坑
JS中如何阻止事件的传播