一、概述

二、日期查询

2.1 MySql

  1. 统计今年每一个月数据
select res.day,ifnull(warn.count+warn.pcount,0) as count,ifnull(warn.pcount,0)as pcount,ifnull(warn.fcount,0)as fcount,ifnull(warn.handled,0)as handledfrom (SELECT concat(year(curdate()),'-12') as dayunion allSELECT concat(year(curdate()),'-11') as dayunion allSELECT concat(year(curdate()),'-10') as dayunion allSELECT concat(year(curdate()),'-09') as dayunion allSELECT concat(year(curdate()),'-08') as dayunion allSELECT concat(year(curdate()),'-07') as dayunion allSELECT concat(year(curdate()),'-06') as dayunion allSELECT concat(year(curdate()),'-05') as dayunion allSELECT concat(year(curdate()),'-04') as dayunion allSELECT concat(year(curdate()),'-03') as dayunion allSELECT concat(year(curdate()),'-02') as dayunion allSELECT concat(year(curdate()),'-01') as day) resleft join (select DATE_FORMAT(create_time, '%Y-%m') as datetime, count(event_level=1 or null) as pcount,count(event_level=2 or null) as count, count(event_level=3 or null) as fcount,count(event_state='1' or null) as handledfrom t_run_equ_eventwhere  dept_id='101'   and YEAR(create_time)=YEAR(NOW())group by datetime) warn on res.day = warn.datetimeorder by res.day asc

其中create_time格式为:yyyy-mm-dd hh:mm:ss。

1.2 最近7天

 select res.day,ifnull(warn.count+warn.pcount,0) as count,ifnull(warn.pcount,0)as pcount,ifnull(warn.fcount,0)as fcount,ifnull(warn.handled,0)as handledfrom (SELECT curdate() as dayunion allSELECT date_sub(curdate(), interval 1 day) as dayunion allSELECT date_sub(curdate(), interval 2 day) as dayunion allSELECT date_sub(curdate(), interval 3 day) as dayunion allSELECT date_sub(curdate(), interval 4 day) as dayunion allSELECT date_sub(curdate(), interval 5 day) as dayunion allSELECT date_sub(curdate(), interval 6 day) as day) resleft join (select date(event_time) as datetime, count(event_level=1 or null) as pcount,count(event_level=2 or null) as count,count(event_level=3 or null) as fcount, count(event_state='1' or null) as handledfrom t_run_equ_eventwhere  dept_id='101'   and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(event_time)group by date(event_time)) warn on res.day = warn.datetimeorder by res.day asc

1.3 最近30天告警数和整改数+去年同期数

select curYear.warns,curYear.handled,lYear.lwarns,lYear.lhandled
from(select ifnull(warn.warns,0)warns,ifnull(handle.handled,0)handled from (select count(*) warns,dept_idfrom t_run_equ_event ewhere e.dept_id=101 and  DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(create_time)
)warnLEFT JOIN(SELECT count(*)handled,dept_id from t_run_work_order where dept_id=101  and work_state='1' and  DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(create_time))handle on warn.dept_id=handle.dept_id) curYear
LEFT JOIN (select ifnull(warn.warns,0)lwarns,ifnull(handle.handled,0)lhandled from (select count(*) warns,dept_idfrom t_run_equ_event ewhere e.dept_id=101 and  DATE_SUB(CURDATE(), INTERVAL 1 YEAR) >= DATE(create_time)>=DATE_ADD(DATE_SUB(CURDATE(), INTERVAL 1 YEAR), INTERVAL 30 day))warnLEFT JOIN(SELECT count(*)handled,dept_id from t_run_work_order where dept_id=101  and work_state='1' and  DATE_SUB(CURDATE(), INTERVAL 1 YEAR) >= DATE(create_time)>=DATE_ADD(DATE_SUB(CURDATE(), INTERVAL 1 YEAR), INTERVAL 30 day))handle on warn.dept_id=handle.dept_id)lYear on 1=1

1.4 今年告警数和整改数+去年同期数

select curYear.warns,curYear.handled,lYear.lwarns,lYear.lhandledfrom(select ifnull(warn.warns,0)warns,ifnull(handle.handled,0)handled from (select count(*) warns,dept_idfrom t_run_equ_event ewhere e.dept_id=#{deptId} and e.event_level='2' and   YEAR(create_time)=YEAR(NOW()))warnLEFT JOIN(SELECT count(*)handled,dept_id from t_run_work_orderwhere dept_id=#{deptId}  and work_state='1' and  YEAR(create_time)=YEAR(NOW()))handle on warn.dept_id=handle.dept_id) curYearLEFT JOIN (select ifnull(warn.warns,0)lwarns,ifnull(handle.handled,0)lhandled from (select count(*) warns,dept_idfrom t_run_equ_event ewhere e.dept_id=#{deptId} and  YEAR(create_time)=YEAR(date_sub(now(),interval 1 year)))warnLEFT JOIN(SELECT count(*)handled,dept_id from t_run_work_orderwhere dept_id=#{deptId}  and work_state='1' and  YEAR(create_time)=YEAR(date_sub(now(),interval 1 year)))handle on warn.dept_id=handle.dept_id)lYear on 1=1

2.2 SqlServer

2.1 本月每一天

   select res.day as cal_date, isnull(warn.total_num,0)total_num, isnull(warn.fire_alarm_num, 0)fire_alarm_num,isnull(warn.error_num,0)error_num, isnull(warn.leave_post_count,0)leave_post_count, isnull(warn.other_num,0)other_numfrom (
SELECT  (number + 1)as day FROM master..spt_valuesWHERE type = 'p' AND DATEADD(DAY,number,CAST (CONVERT (VARCHAR(7), getdate(), 120) + '-01' AS datetime)) < DATEADD(MONTH,1,CAST (CONVERT (VARCHAR(7), getdate(), 120) + '-01' AS datetime))) resleft join (select convert(varchar,datepart(day, cal_date))cal_date ,sum(total_num) total_num,sum(t.fire_alarm_num) fire_alarm_num, sum(t.error_num) error_num,sum(t.leave_post_count) leave_post_count, sum(t.total_num-t.fire_alarm_num-t.error_num-t.leave_post_count) other_num from T_RMCF_DAY_STATISTICS t inner join T_RMCF_SYS_DWXX dw on dw.dwid=t.dwid and dw.del_flag='0' where t.cal_date like '202205%' group by cal_date ) warn on res.day = warn.cal_dateorder by res.day asc

2.2 最近一年每个月统计

 select res.month as sta_month, isnull(warn.total_num,0)total_num, isnull(warn.fire_alarm_num, 0)fire_alarm_num,isnull(warn.error_num,0)error_num, isnull(warn.leave_post_count,0)leave_post_count, isnull(warn.other_num,0)other_numfrom (SELECT 1 as monthunion all SELECT 2  as monthunion all SELECT 3  as monthunion all  SELECT 4  as monthunion all  SELECT 5  as monthunion all SELECT 6  as monthunion all SELECT 7  as monthunion all SELECT 8  as monthunion all SELECT 9  as monthunion all SELECT 10  as monthunion all SELECT 11  as monthunion all SELECT 12  as month) resleft join (select sta_month,sum(total_num) total_num,sum(t.fire_alarm_num) fire_alarm_num, sum(t.error_num) error_num,sum(t.leave_post_count) leave_post_count,sum(t.total_num-t.fire_alarm_num-t.error_num-t.leave_post_count) other_num from T_RMCF_MONTH_STATISTICS t inner join T_RMCF_SYS_DWXX dw on dw.dwid=t.dwid and dw.del_flag='0' where t.sta_year='2022' group by sta_month) warn on res.month = warn.sta_monthorder by res.month asc

2.3 最近7天数据统计

 select res.day as cal_date, warn.total_num, warn.fire_alarm_num, warn.error_num, warn.leave_post_count, warn.other_numfrom (SELECT convert(varchar(10), getdate(), 112) as dayunion allSELECT convert(varchar(10), dateadd(dd,-1,getdate()), 112)  as dayunion allSELECT convert(varchar(10), dateadd(dd,-2,getdate()), 112)  as dayunion allSELECT convert(varchar(10), dateadd(dd,-3,getdate()), 112)  as dayunion allSELECT convert(varchar(10), dateadd(dd,-4,getdate()), 112)  as dayunion allSELECT convert(varchar(10), dateadd(dd,-5,getdate()), 112)  as dayunion allSELECT convert(varchar(10), dateadd(dd,-6,getdate()), 112)  as day) resleft join (select cal_date, sum(total_num) total_num,sum(t.fire_alarm_num) fire_alarm_num, sum(t.error_num) error_num,sum(t.leave_post_count) leave_post_count, sum(t.total_num-t.fire_alarm_num-t.error_num-t.leave_post_count) other_numfrom T_RMCF_DAY_STATISTICS t inner join T_RMCF_SYS_DWXX dw on dw.dwid=t.dwid and dw.del_flag='0' where t.cal_date>='2022-05-06'  group by cal_date) warn on res.day = warn.cal_dateorder by res.day asc

三、两者区别

方法类型,实现稍有区别,主要上数据库自带日期函数不同。

数据库常用日期统计查询相关推荐

  1. IBM的DB2数据库常用命令及查询

    1. 打开命令行窗口 #db2cmd 2. 打开控制中心 #db2cmd db2cc 3. 打开命令编辑器 #db2cmd db2ce =====操作数据库命令===== 4. 启动数据库实例 #db ...

  2. Oracle数据库常用SQL语句查询

    查询第一条记录 where语句后面跟上  and rownum=1 2.日期所相差分钟数 ceil((LOGOUT_TIME - LOGIN_TIME) * 24 * 60) 3.group by分组 ...

  3. Redis数据库常用操作命令(查询db、key、value)

    ①打开Redis可视化工具,点击console,进入查询操作界面 ②打开db库: select 5 ③获取指定 key 的值 get key """ key:Mary_c ...

  4. 数据库的分组统计查询

    使用聚合函数查询 对查询结果进行计算或分类汇总 使用分组查询 若要对查询结果进行分类汇总时,需要使用Group by子句 Group by子句将查询结果按某列(或多列)的值分组,值相等的为一组 若未对 ...

  5. Oracle数据库按月统计(候,旬,月,季,年)

    Oracle数据库按月统计 SELECT TO_CHAR(ds.date_time,'YYYY-MM'),count(*) FROM tab_name ds GROUP BY TO_CHAR(ds.d ...

  6. mysql日期格式化季度_mysql按年度、季度、月度、周、日统计查询的sql语句

    本文介绍一些mysql中用于查询的sql语句,包括按年度.季度.月度.周.日统计查询等,有需要的朋友,可以参考下. 一.年度查询 查询 本年度的数据 SELECT * FROM blog_articl ...

  7. Between 的开始日期和结束日期是同一天没有查询结果,附SQL server数据库的日期时间格式转换大全

    Between 的开始日期和结束日期是同一天没有查询结果 原因: 条件和参数不是一个格式)如字段的类型里包含了时间分钟秒,但是给的条件里没有这些,只有时间 between会在日期后面自动追加" ...

  8. 《数据库系统原理及应用教程》(苗雪兰等,第五版) 实验五:数据库的组合查询和统计查询实验

    阅读前注意: 1. 本实验报告配套<数据库系统原理及应用教程>(苗雪兰等,第五版) 实验五:数据库的组合查询和统计查询实验(书上10.4节以及第六章部分内容),书本中采用Microsoft ...

  9. 数据库第四次试验:数据库的组合查询、统计查询及视图

    数据库第四次试验:数据库的组合查询.统计查询及视图 前言 一.实验目的 二.实验要求 三.实验原理.方法和手段 四.实验组织运行要求 五.实验条件 六.实验步骤 七.思考题 八.实验报告 九.其他说明 ...

  10. oracle数据库常用的99条查询语句

    1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...

最新文章

  1. 将 iPhone 定位设置在法国,手机速度就能迅速提升?
  2. python进程数上限_python-使用multiprocessing.Process并发进程数最多
  3. 小程序 | 微信小程序二级选择器
  4. OneAPM NI 基于旁路镜像数据的真实用户体验监控
  5. PowerDesigner(二)-项目和框架矩阵
  6. vue3 @/cli脚手架搭建项目
  7. react.js学习笔记02
  8. Excel:VBA编程入门(一)
  9. oracle -varchar ,varchar2
  10. Chrome 浏览器扩展神器暴力猴
  11. shal()函数绕过和session验证绕过
  12. 【LeetCode每日一题】633. 平方数之和
  13. python拟合线性函数_Python线性拟合实现函数与用法示例
  14. 做跨境电商一年买了房: 未来五年的风口行业,90%的人都不知道 !
  15. 载银纳米TiO2/壳聚糖水凝胶/pH/GSH响应羧甲基壳聚糖水凝胶和纳米凝胶的制备
  16. 不懂面试官想要哪种数据分析师,简历写的再好也没用!文末有福利
  17. python做题记录之切西瓜
  18. Flutter Ticker类的用法
  19. 让my97 datepicker兼容ie9、ie10、ie11
  20. php只取时间的下士_闲来无聊,用python抓取天气信息,简单就是美啊

热门文章

  1. TinyMCE自定义表情包
  2. 关于#!/bin/bash
  3. TQ210 —— LCD
  4. Unsupported OS Version In Xcode
  5. 顺丰快递单号查询API开发指南-快递鸟
  6. QQ 聊天机器人API
  7. Mediawiki安装经验分享
  8. PAT乙级1068 万绿丛中一点红(测试点3、测试点5)
  9. 一根网线实现双机互联共享文件
  10. linux 三网卡 双网关,三网卡双机互联共享Internet