当前位置:网站首页>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 ;
边栏推荐
- Based on configured schedule, the given trigger will never fire
- How to debug apps remotely and online?
- Medical management system (C language course for freshmen)
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- C language 3-7 daffodils (enhanced version)
- What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
- Ar Augmented Reality applicable scenarios
- JMeter (I) - download, installation and plug-in management
- RTL8189FS如何关闭Debug信息
- What is AQS and its principle
猜你喜欢

MySQL view concept, create view, view, modify view, delete view

花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案

Selection of field types for creating tables in MySQL database

How does MySQL solve the problem of not releasing space after deleting a large amount of data

Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing

软件开发生命周期 --瀑布模型
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/ba/dcb276768b1a9cc84099f093677d29.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)

With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry

Matlab uses audioread and sound to read and play WAV files
随机推荐
Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
The role of artificial intelligence in network security
1222. Password dropping (interval DP, bracket matching)
Four basic strategies for migrating cloud computing workloads
Three core problems of concurrent programming
Makefile simple induction
STM32F103——两路PWM控制电机
479. Additive binary tree (interval DP on the tree)
This is the form of the K-line diagram (pithy formula)
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
Laravel artisan common commands
* and & symbols in C language
大学的知识是否学而无用、过时?
Failed to transform file 'xxx' to match attributes
自动浏览拼多多商品
Niuke - Huawei question bank (51~60)
5g/4g pole gateway_ Smart pole gateway
Construction and maintenance of business websites [11]
Six lessons to be learned for the successful implementation of edge coding
pytest 测试框架