当前位置:网站首页>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 ;
边栏推荐
- Open that kind of construction document
- Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
- JMeter (I) - download, installation and plug-in management
- 1222. Password dropping (interval DP, bracket matching)
- How to build and use redis environment
- Construction and maintenance of business websites [10]
- CSDN article underlined, font color changed, picture centered, 1 second to understand
- leetcode2312. Selling wood blocks (difficult, weekly race)
- Opengauss database backup and recovery guide
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
猜你喜欢

Six lessons to be learned for the successful implementation of edge coding

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

Discussion on the idea of platform construction

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

Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory

开发工具创新升级,鲲鹏推进计算产业“竹林”式生长

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

Golang lock
![[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

JMeter (II) - install the custom thread groups plug-in
随机推荐
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
flutter 中間一個元素,最右邊一個元素
JPM 2021 most popular paper released (with download)
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Failed to transform file 'xxx' to match attributes
自动浏览拼多多商品
Based on configured schedule, the given trigger will never fire
Open那啥的搭建文档
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
np. Where and torch Where usage
Parted command
JMeter (I) - download, installation and plug-in management
Redis有序集合如何使用
mysql列转行函数指的是什么
2022 Q2 - Summary of skills to improve skills
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
A quick understanding of digital electricity
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
[C #] use regular verification content
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years