当前位置:网站首页>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 ;
边栏推荐
- 自动浏览拼多多商品
- 城市选择器组件实现原理
- 2022 Q2 - 提升技能的技巧总结
- Six lessons to be learned for the successful implementation of edge coding
- With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
- Architecture evolution from MVC to DDD
- Redis有序集合如何使用
- Word search applet design report based on cloud development +ppt+ project source code + demonstration video
- Sword finger offer 62 The last remaining number in the circle
- mysql列转行函数指的是什么
猜你喜欢
Data analysis on the disaster of Titanic
How to turn off debug information in rtl8189fs
leetcode373. 查找和最小的 K 对数字(中等)
跨域?同源?一次搞懂什么是跨域
城市选择器组件实现原理
leetcode2312. Selling wood blocks (difficult, weekly race)
软件开发生命周期 --瀑布模型
如何用一款产品推动「品牌的惊险一跃」?
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
MySQL constraints and multi table query example analysis
随机推荐
医药管理系统(大一下C语言课设)
Construction and maintenance of business websites [12]
There are spaces in the for loop variable in the shell -- IFS variable
How to solve MySQL master-slave delay problem
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
Design and implementation of key value storage engine based on LSM tree
Architecture evolution from MVC to DDD
pytest 测试框架
How to build and use redis environment
np. Where and torch Where usage
剑指 Offer 62. 圆圈中最后剩下的数字
Deep learning: a solution to over fitting in deep neural networks
MySQL view concept, create view, view, modify view, delete view
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
[graduation season] graduate seniors share how to make undergraduate more meaningful
mysql列转行函数指的是什么
JMeter (I) - download, installation and plug-in management
This is the form of the K-line diagram (pithy formula)
An analysis of circuit for quick understanding
Four basic strategies for migrating cloud computing workloads