当前位置:网站首页>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

边栏推荐
- Set the way that liquid (Jekyll) is displayed in markdown but not parsed
- JS中如何阻止事件的传播
- Process engine solves complex business problems
- Correspondence between pytorch and torchvision
- PDF转图片实现方式
- PIP for source changing and accelerated downloading
- [fundamentals of machine learning 03] gbdt (gradient boost decision tree)
- [outside distribution detection] your classifier is secret an energy based model and you head treat it like one ICLR '20
- [fundamentals of machine learning 04] matrix factorization
- [fundamentals of machine learning 02] decision tree and random forest
猜你喜欢

Introduction to 51 Single Chip Microcomputer -- digital clock

Error: unable to find vcvarsall Solutions to bat errors

JDBC查询结果集,结果集转化成表

Canoe learning notes (1) illustration of new project and channel configuration steps

ROS Qt环境搭建

33岁程序员的年中总结

Mid year summary of 33 year old programmer

Résumé semestriel du programmeur de 33 ans

33歲程序員的年中總結
![[fundamentals of machine learning 04] matrix factorization](/img/f5/373bfe68f1a3422e907056c20a0db3.jpg)
[fundamentals of machine learning 04] matrix factorization
随机推荐
[anomaly detection] malware detection: mamadroid (dnss 2017)
ROS QT environment construction
【实习】跨域问题
Canoe learning notes (9) sending module can Ig diagram
Tikz learning notes (III) marking and intersection of graphics
antd——a-upload-dragger拖拽上传组件——基础积累
【GAN】《ENERGY-BASED GENERATIVE ADVERSARIAL NETWORKS》 ICLR‘17
Yolov1 (prediction process)
C语言——深入理解数组
Xh_ CMS penetration test documentation
Introduction to 51 Single Chip Microcomputer -- digital tube
Canoe learning notes (3) graphic window introduction diagram
编程题:移除数组中的元素(JS实现)
Error: unable to find vcvarsall Solutions to bat errors
The solution of word document being locked and unable to edit
Multi camera data collection based on Kinect azure (II)
[internship] cross domain problems
Rviz ROS wiki official website tutorial learning notes (1) - User Guide
CNN model collection | RESNET variants -wideresnet interpretation
Self attention (notes, for personal use)