当前位置:网站首页>This article shows you how to use SQL to process weekly report data
This article shows you how to use SQL to process weekly report data
2022-07-24 02:23:00 【Monkey data analysis】

【 subject 】
There are three tables in the database of a company , Weekly sales table 、 Store information table 、 Daily sales schedule .
Table 1 : The weekly sales table records the sales information of stores across the country in each natural week , The field contains weeks 、 Store code 、 Tag amount 、 Sales amount .

Table two : The store information table records the area where the store is located , The hierarchical relationship and business type corresponding to the store , The field contains the store code 、 Area 、 The abbreviation of superior customer 、 Business types .
Table 3 : The daily sales schedule records the amount of goods sold by stores in different regions of the country every day , Customer nature and product information , Field contains area 、 The abbreviation of superior customer 、 Superior customer code 、 Customer nature 、 Store code 、 Sales date 、 Sales amount 、 Commodity code .

Business needs :
1. For different areas 、 Tag amount of business type 、 Summarize the sales amount ( Time range 202201-202205 Zhou .
2. The extraction area is East China 、 Central China 、 West China District 、 In South China , And the customer is self-supporting ,2022 year 3 Monthly and daily sales data for the same period .
【 Their thinking 】
1. For different areas 、 Tag amount of business type 、 Summarize the sales amount ( Time range 202201-202205 Zhou )
Let's first translate this business requirement into Chinese :
1) The fields required for query results are areas 、 Business types 、 Tag amount 、 Sales amount
2) The screening time week is 202201-202205
3) For different areas 、 Tag amount of business type 、 Summarize the sales amount
1) The fields required for query results are areas 、 Business types 、 Tag amount 、 Sales amount
Tag amount 、 The sales amount is in the weekly sales table , Area 、 The business type is in the store information table , Fields involving two tables , So you need to join multiple tables .
Look at the two tables , We know that the name of the two tables is the same and the associated field is store code , Join two tables with store code .
What kind of connection to use ? take out 《 monkey Learn from scratch SQL》 The multi table connection diagram inside .
Because you need to get the business type corresponding to each store code , So use the left link to keep all the sales data in the weekly sales table .
SQL Write the following :
select *
from Weekly sales table a
left join Store information table b
on a. Store code = b. Store code ;2) The screening time week is 202201-202205, Use between Function to filter the time range .
SQL Write the following :
select *
from Weekly sales table a
left join Store information table b
on a. Store code = b. Store code
where Zhou between 202201 and 202205;Query results :

3) For different areas 、 Tag amount of business type 、 Summarize the sales amount
The business demand only displays the tag amount and sales amount , We use group summary to summarize different areas 、 Business class grouping (group by), For the tag amount 、 Sum the sales amount ( Sum function sum)
SQL Write the following :
select Area ,
Business types ,
sum(a. Tag amount ) AS " Total tag amount ",
sum(a. Sales amount ) AS " Total sales amount "
from Weekly sales table a
left join Store information table b
on a. Store code = b. Store code
where Zhou between 202201 and 202205
group by Business types , Area ;Query results :

2、 The extraction area is East China 、 Central China 、 West China District 、 In South China , And the customer is self-supporting ,2022 year 3 Monthly and daily sales data for the same period .
Demand display field area 、 Customer nature 、 Sales date , Daily sales details include , Now we only need to classify the customer as “ Self-support ” And the time is 2022 year 3 The month and 2021 year 3 month ( At the same time ) Data display .
SQL Use in date_format Set the format of the sales date in the daily sales table to the format of month and year .

SQL Write the following :
select *
from Daily sales schedule
where Area in (
" East China ",
" Central China ",
" West China District ",
" South China " )
and Customer nature = " Self-support "
and date_format( Sales date , '%Y-%m') between "2021-03" and "2022-03";Query results :

【 The test point of this question 】
1. Business requirements are broken down into problems , Try to translate into Chinese , Find out the connection and breakthrough points according to the needs .
2.SQL How to use the most common connections in , Learn reference monkey Free Tutorial 《 The illustration SQL Interview questions 》.
3. If the date format in the data table is not the presentation result we want , Learn to date_format Function to convert .


️ Click on 「 Read the original 」
Free registration Data analysis training camp
边栏推荐
- Leetcode exercise -- two questions about the nearest common ancestor of binary trees
- 响应式pbootcms模板装修设计类网站
- Codeworks 5 questions per day (average 1500) - day 23
- Cmake Getting Started tutorial
- canvas-绘图(鼠标按下 绘制 抬起 结束)
- 什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
- Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
- 5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字
- Halide:: generator instructions
- Network protocol details: TCP part1
猜你喜欢

响应式布局一个网页在不同设备显示不同效果)meta:vp

浅谈领域驱动设计
![[重要通知]星球线上培训第三期来袭!讲解如何在QTYX上构建自己的量化策略!...](/img/37/f9ea9af069f62cadff21415f070223.png)
[重要通知]星球线上培训第三期来袭!讲解如何在QTYX上构建自己的量化策略!...

How CAD draws arrows with arcs

About rapidssl certificate

Jmeter+influxdb+grafana pressure measurement real-time monitoring platform construction

Loadrunner12 installation, recording the first script and the proxy server did not respond to the solution

BPG notes (III)

Distributed resource management and task scheduling framework yarn

The communication principle between native components, applets and clients, and the operation principle of video, map, canvas, picker, etc
随机推荐
Hydrogen entrepreneurship competition | Liu Xiaoqi, chairman of Guohua Investment: take advantage of the integration of scenery, hydrogen storage and finance to host the entrepreneurship competition a
[Luogu] p1972 HH Necklace
Draw pictures with canvas
Express operates mysql. What is wrong with the SQL?
图解数组和链表详细对比,性能测试
ggplot2显示png
Share two interesting special effects
Distributed resource management and task scheduling framework yarn
College degree want to 0 basic programming after looking for a job feasible?
响应式布局一个网页在不同设备显示不同效果)meta:vp
Go基础笔记_5_数组切片
Ardunio - ULN2003 drive board and DC motor fan - control fan speed
Writing of graph nodes that trigger different special effects during the day and at night in Tiktok
C -- bit operation
奔走相告,行情与量化页面功能优化!股票量化分析工具QTYX-V2.4.5
Ggplot2 displays png
Leetcode 70 climbing stairs, 199 right view of binary tree, 232 realizing queue with stack, 143 rearranging linked list
Pbootcms template calls the tag ordinal number from 2 or automatic number
On Domain Driven Design
杂志特稿:元宇宙将重塑我们的生活,我们要确保它变得更好
