亚洲综合av网_中文字幕一区二区三区在线观看_91免费看片在线观看_久久精品久久精品亚洲人

技術知識
NEWS CENTRE
首頁
>
新聞中心
>
完成按月累加PostgreSQL
完成按月累加PostgreSQL
2021-08-13 閱讀:2686

這篇文章主要介紹了PostgreSQL完成按月累加的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。

背景

統計某個指標,指標按照月進行累加,注意需要按省份和年份進行分組。

方法一、使用自關聯

-- with 按月統計得到中間結果
WITH yms AS (SELECT regionid,SUM(getnum) AS getnum,SUM(dealnum) AS dealnum,to_char(qndate,'yyyy-MM') AS yearmonth
FROM t_queuenumber
GROUP BY regionid,to_char(qndate,'yyyy-MM')
ORDER BY regionid,yearmonth)-- 查用子查詢解決。
SELECT s1.regionid,s1.yearmonth, getnum,dealnum,
(SELECT SUM(getnum) FROM yms s2 WHERE s2.regionid = s1.regionid AND s2.yearmonth <= s1.yearmonth AND SUBSTRING(s1.yearmonth,0,5) = SUBSTRING(s2.yearmonth,0,5) ) AS getaccumulatednum,
(SELECT SUM(dealnum) FROM yms s2 WHERE s2.regionid = s1.regionid AND s2.yearmonth <= s1.yearmonth AND SUBSTRING(s1.yearmonth,0,5) = SUBSTRING(s2.yearmonth,0,5) ) AS accumulatednum
FROM yms s1;

 

查詢的結果如下:

方法二、使用窗口函數

更多關于窗口函數的用法,可以參考以前的文章。窗口函數十分適合這樣的場景:

 WITH yms AS (SELECT regionid,SUM(getnum) AS getnum,SUM(dealnum) AS dealnum,to_char(qndate,'yyyy-MM') AS yearmonth
 FROM t_queuenumber
 GROUP BY regionid,to_char(qndate,'yyyy-MM')
 ORDER BY regionid,yearmonth)
 -- 窗口函數的使用
 SELECT regionid,yearmonth,
 SUM(getnum) OVER(PARTITION BY regionid,SUBSTRING(yearmonth,0,5) ORDER BY yearmonth) AS getaccumulatednum,
 SUM(dealnum) OVER(PARTITION BY regionid ,SUBSTRING(yearmonth,0,5) ORDER BY yearmonth) AS dealaccumulatednum
 FROM yms;

 

后記

可以使用子查詢、可以使用窗口函數完成上面業務場景。

補充:PostgreSQL實現按秒按分按時按日按周按月按年統計數據

提取時間(年月日時分秒):

import datetime
from dateutil.relativedelta import relativedelta
today = str(datetime.datetime.now())
print(today)
print(today[:4], today[:7], today[:10],today[:13])
 
print("************分隔符***************")
 
yesterday = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d %H:%M:%S")
yesterday2 = (datetime.datetime.now() + datetime.timedelta(days=-2)).strftime("%Y-%m-%d %H:%M:%S")
nextmonths = str(datetime.date.today() - relativedelta(months=-1))[:7]
lastmonths = str(datetime.date.today() - relativedelta(months=+1))[:7]
lastyears = str(datetime.date.today() - relativedelta(years=+1))[:4]
nextyears = str(datetime.date.today() - relativedelta(years=-1))[:4]
 
print(yesterday)
print(yesterday2)
print(nextmonths)
print(lastmonths)
print(lastyears)
print(nextyears)

 

結果:

2020-03-05 13:49:59.982555
2020 2020-03 2020-03-05 2020-03-05 13
************分隔符***************
2020-03-04 13:49:59
2020-03-03 13:49:59
2020-04
2020-02
2019
2021

 

昨日每時:

select s.acceptDate, s.data_num
 from (select to_char(acceptDate, 'yyyy-mm-dd hh24') || '點' as acceptDate,
        count(1) as data_num
     from table_name t
     where t.acceptDate >= to_date('20190506', 'yyyymmdd')
      and t.acceptDate < to_date('20190507', 'yyyymmdd') and organization_ = 'abcdefghijklmnopqrstuvwxyz'
     group by to_char(acceptDate, 'yyyy-mm-dd hh24') || '點') s

 

本月每天:

select s.acceptDate, s.data_num
 from (select to_char(acceptDate, 'yyyy-mm-dd') as acceptDate,
        count(1) as data_num
     from table_name t
     where t.acceptDate >= to_date('201905', 'yyyymm')
      and t.acceptDate < to_date('201906', 'yyyymm') and organization_ = 'abcdefghijklmnopqrstuvwxyz'
     group by to_char(acceptDate, 'yyyy-mm-dd') ) s

 

本年每月:

select s.acceptDate, s.data_num
 from (select to_char(acceptDate, 'yyyy-mm') as acceptDate,
        count(1) as data_num
     from table_name t
     where t.acceptDate >= to_date('2019', 'yyyy')
      and t.acceptDate < to_date('2020', 'yyyy') and organization_ = 'abcdefghijklmnopqrstuvwxyz'
     group by to_char(acceptDate, 'yyyy-mm') ) s

 

2月-7月中每月的人數統計:

sql = """SELECT to_char(rujiaoriqi, 'yyyy-mm') as month,count(1) num
           FROM jibenxx where rujiaoriqi is not null and zhongzhiriqi is null
           AND to_char(rujiaoriqi,'yyyy-mm-dd')>='2020-02-01'
           GROUP BY to_char(rujiaoriqi, 'yyyy-mm') order by to_char(rujiaoriqi, 'yyyy-mm') """

 

統計每年:

select s.acceptDate, s.data_num
 from (select to_char(acceptDate, 'yyyy') as acceptDate,
        count(1) as data_num
     from table_name t
     where t.acceptDate >= to_date('2015', 'yyyy')
      and t.acceptDate < to_date('2021', 'yyyy') and organization_ = 'abcdefghijklmnopqrstuvwxyz'
     group by to_char(acceptDate, 'yyyy') ) s

 

里面時間參數進行傳參即可。

補充:

統計今天(查詢當天或者指定某天數量)

1select count(1) FROM "shequjz_jibenxx" where to_char(zhongzhiriqi,'yyyy-mm-dd')='2019-11-11'

最近七天每天的數量:

select s.acceptDate, s.data_num
 from (select to_char(jiaozheng_jieshushijian, 'yyyy-mm-dd') as acceptDate,
        count(1) as data_num
     from shequjz_jibenxx t
     where t.jiaozheng_jieshushijian >= to_date('2020-11-06', 'yyyy-mm-dd')
      and t.jiaozheng_jieshushijian < to_date('2020-11-13', 'yyyy-mm-dd')
     group by to_char(jiaozheng_jieshushijian, 'yyyy-mm-dd') ) s ORDER BY acceptDate ASC

 

最近七天(1天、3天、7天、一個月、一年、1h、1min、60s)的數量(總量):

# 包括今天向前推6天的總量
select count(1) from shequjz_jibenxx where jiaozheng_jieshushijian
between (SELECT current_timestamp - interval '7 day')
and current_timestamp
# 最近一天(昨天)
SELECT current_timestamp - interval '1 day'
# 最近三天
SELECT current_timestamp - interval '3 day'
# 最近一周
SELECT current_timestamp - interval '7 day'
# 最近一個月(當前時間向前推進一個月)
SELECT current_timestamp - interval '1 month'
# 最近一年(當前時間向前推進一年)
SELECT current_timestamp - interval '1 year'
# 最近一小時(當前時間向前推一小時)
SELECT current_timestamp - interval '1 hour'
# 最近一分鐘(當前時間向前推一分鐘)
SELECT current_timestamp - interval '1 min'
# 最近60秒(當前時間向前推60秒)
SELECT current_timestamp - interval '60 second'

 

最近七天中每天的累計歷史總量:

步驟:

1)先統計出近7天每天的數量

2)后統計出7天前的累計歷史總量

3)再對第(1)步中獲取的結果進行累計求和,使用cumsum()函數

4)最后在第(3)步結果的基礎上,加上7天前的累計歷史總量(也就是第2步的結果)

# 趨勢
def getWeekTrends(self):
  try:
    database = DataBase()
    sql = """select s.zhongzhi_Date, s.data_num
        from (select to_char(jiaozheng_jieshushijian, 'yyyy-mm-dd') as zhongzhi_Date,
        count(1) as data_num
        from shequjz_jibenxx t
        where t.jiaozheng_jieshushijian >= to_date('{}', 'yyyy-mm-dd')
        and t.jiaozheng_jieshushijian < to_date('{}', 'yyyy-mm-dd')
        group by to_char(jiaozheng_jieshushijian, 'yyyy-mm-dd') ) s""".format(lastweek, today[:10])
    res_df = database.queryData(sql, flag=True)
 
    sql_total = """select count(1) FROM "shequjz_jibenxx" where rujiaoriqi is not null
           and zhongzhiriqi is null and to_char(rujiaoriqi,'yyyy-mm-dd')<'{}'""".format(lastweek)
    res_total = database.queryData(sql_total, count=1, flag=False)  #7131
 
    res_df['cumsum'] = res_df['data_num'].cumsum() # 累計求和
    res_df['cumsum'] = res_df['cumsum'] + res_total[0]
    res_df = res_df[['zhongzhi_date', 'cumsum']].to_dict(orient='records')
    res = {'code': 1, 'message': '數據獲取成功', 'data': res_df}
    print(res)
    return res
  except Exception as e:
    error_info = '數據獲取錯誤:{}'.format(e)
    logger.error(error_info)
    res = {'code': 0, 'message': error_info}
    return res
{'code': 1, 'message': '數據獲取成功', 'data': [
{'zhongzhi_date': '2020-11-13', 'cumsum': 7148},
{'zhongzhi_date': '2020-11-10', 'cumsum': 7161},
{'zhongzhi_date': '2020-11-11', 'cumsum': 7195},
{'zhongzhi_date': '2020-11-12', 'cumsum': 7210},
{'zhongzhi_date': '2020-11-09', 'cumsum': 7222},
{'zhongzhi_date': '2020-11-14', 'cumsum': 7229},
{'zhongzhi_date': '2020-11-15', 'cumsum': 7238}]}

 

postgresql按周統計數據

(實際統計的是 上周日到周六 7天的數據):

因為外國人的習慣是一周從周日開始,二我們中國人的習慣一周的開始是星期一,這里 -1 即將顯示日期從周日變成了周一,但是內部統計的數量還是從 上周日到周六進行 統計的,改變的僅僅是顯示星期一的時間。

提取當前星期幾: 1

1SELECT EXTRACT(DOW FROM CURRENT_DATE)

提取當前日期: 2020-11-16 00:00:00

1SELECT CURRENT_DATE-(EXTRACT(DOW FROM CURRENT_DATE)-1||'day')::interval diffday;

按周統計數據一:

select to_char(jiaozheng_jieshushijian::DATE-(extract(dow from "jiaozheng_jieshushijian"::TIMESTAMP)-1||'day')::interval, 'YYYY-mm-dd') date_,
count(1) from shequjz_jibenxx where jiaozheng_jieshushijian BETWEEN '2020-01-01' and '2020-11-16'
 GROUP BY date_ order by date_

 

其中date_為一周中的第一天即星期一

按周統計數據二:

SELECT
to_char ( cda.jiaozheng_jieshushijian, 'yyyy ' ) || EXTRACT ( WEEK FROM cda.jiaozheng_jieshushijian ) :: INTEGER AS date_,
count( cda.id ) AS count,
cda.jiaozheng_jieshushijian AS times
FROM
shequjz_jibenxx AS cda
 
WHERE
1 = 1
AND to_char ( cda.jiaozheng_jieshushijian, 'YYYY-MM-DD HH24:MI:SS' ) BETWEEN '2020-10-01 00:00:00' AND '2020-11-12 00:00:00'
GROUP BY
date_,
times
ORDER BY
date_,
times DESC

 

postgresql中比較日期的四種方法

select * from user_info where create_date >= '2020-11-01' and create_date <= '2020-11-16'
select * from user_info where create_date between '2020-11-01' and '2020-11-16'
select * from user_info where create_date >= '2020-11-01'::timestamp and create_date < '2020-11-16'::timestamp
select * from user_info where create_date between to_date('2020-11-01','YYYY-MM-DD') and to_date('2020-11-16','YYYY-MM-DD')


13560189272
地址:廣州市天河區黃埔大道西201號金澤大廈808室
COPYRIFHT ? 2010-2020 廣州市名聯網絡科技有限公司 ALL RIGHTS RESERVED 粵ICP備10203057號
  • 這里是二維碼
亚洲综合av网_中文字幕一区二区三区在线观看_91免费看片在线观看_久久精品久久精品亚洲人
精品一区二区三区免费播放| 国产不卡在线视频| 精品成人在线观看| 91精品久久久久久久久99蜜臂| 欧美亚洲图片小说| 欧美日产在线观看| 欧美一二三区在线| 精品少妇一区二区三区在线视频| 日韩午夜在线观看视频| wwww国产精品欧美| 中文字幕乱码亚洲精品一区 | 欧美日韩一区二区三区不卡| 欧美视频你懂的| 91精品在线观看入口| 精品三级在线观看| 国产精品网站在线观看| 亚洲三级电影全部在线观看高清| 亚洲综合丝袜美腿| 欧美aⅴ一区二区三区视频| 黑人巨大精品欧美黑白配亚洲| 国产精品主播直播| 一本色道久久综合狠狠躁的推荐| 欧美色男人天堂| 久久久青草青青国产亚洲免观| 最新不卡av在线| 蜜臀av性久久久久蜜臀av麻豆| 国产一区二区伦理片| 91丨九色丨国产丨porny| 91麻豆精品91久久久久同性| 国产女人aaa级久久久级| 亚洲人成网站色在线观看| 午夜久久电影网| 国产精品综合二区| 欧美视频在线一区二区三区| 欧美精品一区二区久久婷婷| 亚洲美女在线国产| 韩日欧美一区二区三区| 色女孩综合影院| 精品国产乱码久久久久久影片| **网站欧美大片在线观看| 视频一区二区不卡| 国产成人午夜99999| 欧美人xxxx| 亚洲视频一区二区免费在线观看| 久久精品72免费观看| 欧美亚洲国产bt| 中文字幕亚洲不卡| 国产精品一区二区久久精品爱涩| 欧美日韩小视频| 最新国产精品久久精品| 韩国精品主播一区二区在线观看 | 激情综合色综合久久综合| 91麻豆免费看片| 久久精品人人做人人综合| 日韩高清不卡一区二区三区| 91在线云播放| 中文字幕精品一区| 国产尤物一区二区| 日韩欧美123| 日本美女一区二区三区| 欧美性videosxxxxx| 亚洲欧美精品午睡沙发| 99久久精品99国产精品| 国产精品女同互慰在线看| 国产美女视频一区| 欧美va天堂va视频va在线| 日韩av中文字幕一区二区三区| 欧美性色黄大片手机版| 一区二区三区在线看| 91在线码无精品| 亚洲日本一区二区三区| 99r精品视频| 中文字幕人成不卡一区| 99国产精品国产精品久久| 中文字幕在线一区| 99热国产精品| 亚洲猫色日本管| 91久久精品午夜一区二区| 一区二区三区日韩| 欧美少妇bbb| 日产欧产美韩系列久久99| 欧美一区二区三区性视频| 久色婷婷小香蕉久久| 久久在线观看免费| 国产盗摄女厕一区二区三区| 国产亚洲精品福利| www.av亚洲| 依依成人综合视频| 欧美亚洲国产一区二区三区| 丝袜脚交一区二区| 欧美变态口味重另类| 国产传媒欧美日韩成人| **欧美大码日韩| 欧美日韩国产成人在线免费| 美腿丝袜在线亚洲一区| 久久久久久免费毛片精品| www.亚洲激情.com| 亚洲午夜电影在线观看| 日韩免费高清av| 成人性生交大片免费看在线播放| 亚洲视频免费在线观看| 欧美日韩亚洲综合在线| 国产专区欧美精品| 亚洲老妇xxxxxx| 欧美成人国产一区二区| 成人高清视频免费观看| 婷婷综合在线观看| 国产欧美日韩亚州综合| 在线观看亚洲成人| 久久99国产乱子伦精品免费| 亚洲欧美综合另类在线卡通| 欧美老女人第四色| 高清不卡一区二区| 日日夜夜免费精品| 国产精品久久网站| 日韩一级完整毛片| 色诱亚洲精品久久久久久| 乱一区二区av| 亚洲精品免费看| 久久精品一区八戒影视| 欧美午夜电影在线播放| 国产成都精品91一区二区三| 亚洲成人在线观看视频| 国产精品久久久久久久浪潮网站 | 欧美日韩三级在线| 国产一区二区伦理片| 亚洲国产毛片aaaaa无费看 | 99视频在线精品| 男女男精品视频网| 亚洲美女少妇撒尿| 国产亚洲va综合人人澡精品| 欧美精品第1页| 91老师片黄在线观看| 国产一区二区福利| 日本91福利区| 亚洲国产欧美日韩另类综合| 国产精品污网站| 久久夜色精品国产欧美乱极品| 欧美亚洲免费在线一区| 成av人片一区二区| 国产精品一区一区| 久久国产剧场电影| 石原莉奈在线亚洲三区| 亚洲综合图片区| 亚洲蜜臀av乱码久久精品| 欧美国产综合色视频| 精品国偷自产国产一区| 9191成人精品久久| 欧美日韩一区二区三区不卡| 色久综合一二码| 色婷婷综合视频在线观看| av高清不卡在线| 丰满少妇在线播放bd日韩电影| 久久成人麻豆午夜电影| 天天操天天干天天综合网| 亚洲国产精品久久人人爱蜜臀| 亚洲欧美激情插 | 秋霞午夜av一区二区三区| 午夜久久久久久| 亚洲成a天堂v人片| 亚洲一级二级三级在线免费观看| 亚洲精品视频观看| 亚洲女人****多毛耸耸8| 中文字幕一区免费在线观看| 国产精品久久久久7777按摩| 中文字幕在线观看不卡视频| 中文字幕一区二区三区不卡| 亚洲视频在线观看一区| 亚洲免费在线视频一区 二区| 亚洲乱码一区二区三区在线观看| 亚洲视频 欧洲视频| 亚洲男同性视频| 一区二区三区四区亚洲| 亚洲大片免费看| 蜜桃精品视频在线| 国产一区二区精品在线观看| 国产成人精品亚洲日本在线桃色| 成人午夜av电影| 色综合久久久久综合| 欧美日韩国产一二三| 日韩亚洲欧美在线| 国产午夜精品在线观看| 亚洲丝袜精品丝袜在线| 亚洲第一成人在线| 激情文学综合网| a在线播放不卡| 欧美日韩二区三区| www久久精品| 亚洲人成伊人成综合网小说| 亚洲国产sm捆绑调教视频| 老鸭窝一区二区久久精品| 成人激情黄色小说| 欧美日韩日本视频| 精品美女在线播放| 亚洲欧美激情在线| 久久66热偷产精品| 色婷婷狠狠综合| 久久午夜免费电影| 一区二区久久久久久| 久久99精品久久久|