当前位置:网站首页>SQL server calculates the daily average and annual average of the whole province
SQL server calculates the daily average and annual average of the whole province
2022-07-02 02:05:00 【Operation and maintenance dumplings】
Knowledge points used :
1. By year 、 Day group convert function
2. Automatically supplement interval data by date cycle
3. Automatic supplementary entry if supplementary entry has been made, the supplementary entry will not be repeated again
Separate dynamic supplementary recording and automatic supplementary recording sql sentence :
Manual supplementary entry sql sentence :
use [GWAMDB]
DECLARE @s_time datetime
DECLARE @e_time datetime
declare @ii INT
declare @jj INT
declare @i datetime
declare @j datetime
declare @h datetime
declare @m datetime
declare @a datetime
set @i='2022-01-01' -- The initial time of the year , Throughout the year , Generally, there is no need to change
set @s_time = '2022-01-01' -- Throughout the year 、 The starting time of the month , You can customize
set @e_time = '2022-01-14' -- Throughout the year 、 The end time of supplementary recording in the current month , You can customize
set @ii = DATEDIFF(DAY,@s_time,@e_time)
set @jj = 0
while @jj <= @ii -- The date cycle , If you manually make a supplementary entry, you need to cancel this line of comments
BEGIN
set @a = DATEADD(DAY, @jj, @s_time)
-- Average annual
INSERT INTO[tt1](datetime, type, PM25)
SELECT @a,' Annual cumulative average concentration ', round(AVG(pm25),0) as PM25 FROM [dbo].[ZHZSJS] where datetime BETWEEN @i and @a and LEFT(citycode,2) in ('13')
group BY CONVERT(VARCHAR(4),datetime,120)
ORDER BY CONVERT(VARCHAR(4),datetime,120);
set @jj = @jj +1
END
-- On the day
INSERT INTO[tt1](datetime, type, PM25)
SELECT DISTINCT CONVERT(VARCHAR(10),datetime,120), ' The average concentration of the day ', round(AVG(pm25),0) as PM25 FROM [dbo].[ZHZSJS] where datetime BETWEEN @s_time and @e_time and LEFT(citycode,2) in ('13')
group BY CONVERT(VARCHAR(10),datetime,120)
ORDER BY CONVERT(VARCHAR(10),datetime,120);
Automatic supplementary recording of yesterday's daily average data :
-- DROP TABLE tt1;
use [GWAMDB]
-- create table tt1
-- (datetime datetime,
-- type varchar(50),
-- PM25 float);
declare @i datetime
declare @j datetime
declare @h datetime
declare @m datetime
set @i='2022-01-01' -- Throughout the year
-- set @m='2022-02-01' -- During the month
-- set @j='2022-02-01' -- Customize all supplementary entry dates
set @j=(select max(datetime) FROM [ZHZSJS])
set @h=(select max(datetime) FROM [tt1])
if @j > @h
BEGIN
-- The daily average concentration does not need to be recorded day by day , It presses datetime Fill in
INSERT INTO[tt1](datetime, type, PM25)
SELECT DISTINCT CONVERT(VARCHAR(10),datetime,120), ' The average concentration of the day ', round(AVG(pm25),0) as PM25 FROM [dbo].[ZHZSJS] where datetime = @j and LEFT(citycode,2) in ('13')
group BY CONVERT(VARCHAR(10),datetime,120)
ORDER BY CONVERT(VARCHAR(10),datetime,120);
-- Annual cumulative average , Need to set up @j,@i The default is 1 month 1 Japan , Make a supplement day by day
INSERT INTO[tt1](datetime, type, PM25)
SELECT @j as aa,' Annual cumulative average concentration ', round(AVG(pm25),0) as PM25 FROM [dbo].[ZHZSJS] where datetime BETWEEN @i and @j and LEFT(citycode,2) in ('13')
group BY CONVERT(VARCHAR(4),datetime,120)
ORDER BY CONVERT(VARCHAR(4),datetime,120);
end
-- select DISTINCT datetime, type as ' The same day in the whole province / Accumulated in the current month ', PM25 from tt1 ORDER BY datetime ;
边栏推荐
- A quick understanding of analog electricity
- Pytest testing framework
- Electronic Society C language level 1 32, calculate the power of 2
- 1222. Password dropping (interval DP, bracket matching)
- MySQL约束与多表查询实例分析
- Using mongodb in laravel
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- How to turn off debug information in rtl8189fs
- Construction and maintenance of business websites [15]
猜你喜欢

This is the form of the K-line diagram (pithy formula)

MySQL约束与多表查询实例分析

WebGPU(一):基本概念

JMeter (II) - install the custom thread groups plug-in

How to use a product to promote "brand thrill"?

leetcode2310. The one digit number is the sum of integers of K (medium, weekly)

Types of exhibition items available in the multimedia interactive exhibition hall

SQLite 3 of embedded database

The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu

479. Additive binary tree (interval DP on the tree)
随机推荐
How to use a product to promote "brand thrill"?
【深度学习】infomap 人脸聚类 facecluster
Software development life cycle -- waterfall model
Three core problems of concurrent programming
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
What is AQS and its principle
A quick understanding of analog electricity
如何用一款产品推动「品牌的惊险一跃」?
剑指 Offer 47. 礼物的最大价值
mysql列转行函数指的是什么
There are spaces in the for loop variable in the shell -- IFS variable
CSDN article underlined, font color changed, picture centered, 1 second to understand
Bash bounce shell encoding
【毕业季】研究生学长分享怎样让本科更有意义
Experimental reproduction of variable image compression with a scale hyperprior
剑指 Offer II 031. 最近最少使用缓存
The difference between new and malloc
剑指 Offer 29. 顺时针打印矩阵
Automatically browse pinduoduo products
PR second training