日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

数据库常用日期统计查询

發(fā)布時間:2023/12/20 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库常用日期统计查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、概述

二、日期查詢

2.1 MySql

  • 統(tǒng)計今年每一個月數據
  • 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 最近一年每個月統(tǒng)計

    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天數據統(tǒng)計

    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

    三、兩者區(qū)別

    方法類型,實現稍有區(qū)別,主要上數據庫自帶日期函數不同。

    總結

    以上是生活随笔為你收集整理的数据库常用日期统计查询的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。