欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django中日期時(shí)間型字段進(jìn)行年月日時(shí)分秒分組統(tǒng)計(jì)

 更新時(shí)間:2020年11月27日 08:46:30   作者:燃燈工作室  
這篇文章主要介紹了Django中日期時(shí)間型字段進(jìn)行年月日時(shí)分秒分組統(tǒng)計(jì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.數(shù)據(jù)結(jié)構(gòu)說(shuō)明:

數(shù)據(jù)結(jié)構(gòu)如下:modification字段為修改數(shù)據(jù)時(shí)間字段,格式為 年,月,日,時(shí),分,秒。
案例場(chǎng)景為,根據(jù)modification字段,統(tǒng)計(jì)每個(gè)統(tǒng)計(jì)粒子,產(chǎn)生數(shù)據(jù)的條數(shù)。如需要統(tǒng)計(jì)2020年10月29日 每個(gè)小時(shí)段產(chǎn)生數(shù)據(jù)的條數(shù)。

在這里插入圖片描述

要進(jìn)行這種統(tǒng)計(jì)需要用到 Django的connection庫(kù)。
統(tǒng)計(jì)年月日粒子用 date_trunc_sql,統(tǒng)計(jì)時(shí)分秒用 datetime_extract_sql

2.進(jìn)行年月日粒子的統(tǒng)計(jì)

 2.1 官方 date_trunc_sql 原型

def datetime_trunc_sql(self, lookup_type, field_name, tzname):
 """
 Given a lookup_type of 'year', 'month', 'day', 'hour', 'minute', or
 'second', return the SQL that truncates the given datetime field
 field_name to a datetime object with only the given specificity.
 """
 raise NotImplementedError('subclasses of BaseDatabaseOperations may require a datetime_trunc_sql() method')

解釋說(shuō)明下:

函數(shù)需要傳入三個(gè)參數(shù):

  • lookup_type:統(tǒng)計(jì)粒子(year->年,月->month,day->日,hour->時(shí),minute->分,second->秒)
  • field_name:統(tǒng)計(jì)字段的名次
  • tzname:時(shí)區(qū),在中國(guó)一般用的是東8區(qū),傳入8即可

2.2 年

from django.db import connection
from django.db.models import Count
# 'year', 'month', 'day', 'hour', 'minute', 'second'

select = {'year': connection.ops.datetime_trunc_sql('year', 'establish', 8)}
result = models.FocusOnRecord.objects.extra(select=select).values('year').annotate(number=Count('id')).order_by("year")[0:9]

for key in result:
 print(key)

>>{'year': datetime.datetime(2019, 1, 1, 0, 0), 'number': 2168}
>>{'year': datetime.datetime(2020, 1, 1, 0, 0), 'number': 9369}

2.3 月

from django.db import connection
from django.db.models import Count
# 'year', 'month', 'day', 'hour', 'minute', 'second'

select = {'month': connection.ops.datetime_trunc_sql('month', 'establish', 8)}
result = models.FocusOnRecord.objects.extra(select=select).values('month').annotate(number=Count('id')).order_by("month")[0:9]

for key in result:
 print(key)
 
>>{'month': datetime.datetime(2019, 6, 1, 0, 0), 'number': 8}
>>{'month': datetime.datetime(2019, 7, 1, 0, 0), 'number': 51}
>>{'month': datetime.datetime(2019, 8, 1, 0, 0), 'number': 118}
>>{'month': datetime.datetime(2019, 9, 1, 0, 0), 'number': 7}
>>{'month': datetime.datetime(2019, 10, 1, 0, 0), 'number': 731}
>>{'month': datetime.datetime(2019, 11, 1, 0, 0), 'number': 514}
>>{'month': datetime.datetime(2019, 12, 1, 0, 0), 'number': 739}
>>{'month': datetime.datetime(2020, 1, 1, 0, 0), 'number': 483}
>>{'month': datetime.datetime(2020, 2, 1, 0, 0), 'number': 921}

2.4 日

from django.db import connection
from django.db.models import Count
# 'year', 'month', 'day', 'hour', 'minute', 'second'

select = {'day': connection.ops.datetime_trunc_sql('day', 'establish', 8)}
result = models.FocusOnRecord.objects.extra(select=select).values('day').annotate(number=Count('id')).order_by("day")[0:9]

for key in result:
 print(key)
 
>>{'day': datetime.datetime(2019, 6, 28, 0, 0), 'number': 1}
>>{'day': datetime.datetime(2019, 6, 29, 0, 0), 'number': 7}
>>{'day': datetime.datetime(2019, 7, 2, 0, 0), 'number': 1}
>>{'day': datetime.datetime(2019, 7, 11, 0, 0), 'number': 3}
>>{'day': datetime.datetime(2019, 7, 20, 0, 0), 'number': 32}
>>{'day': datetime.datetime(2019, 7, 21, 0, 0), 'number': 2}
>>{'day': datetime.datetime(2019, 7, 29, 0, 0), 'number': 3}
>>{'day': datetime.datetime(2019, 7, 31, 0, 0), 'number': 10}
>>{'day': datetime.datetime(2019, 8, 1, 0, 0), 'number': 20}

2.5 時(shí)

from django.db import connection
from django.db.models import Count
# 'year', 'month', 'day', 'hour', 'minute', 'second'

select = {'hour': connection.ops.datetime_trunc_sql('hour', 'establish', 8)}
result = models.FocusOnRecord.objects.extra(select=select).values('hour').annotate(number=Count('id')).order_by("hour")[0:9]

for key in result:
 print(key)
 
>>{'hour': datetime.datetime(2019, 6, 28, 17, 0), 'number': 1}
>>{'hour': datetime.datetime(2019, 6, 29, 9, 0), 'number': 6}
>>{'hour': datetime.datetime(2019, 6, 29, 10, 0), 'number': 1}
>>{'hour': datetime.datetime(2019, 7, 2, 14, 0), 'number': 1}
>>{'hour': datetime.datetime(2019, 7, 11, 14, 0), 'number': 2}
>>{'hour': datetime.datetime(2019, 7, 11, 15, 0), 'number': 1}
>>{'hour': datetime.datetime(2019, 7, 20, 11, 0), 'number': 24}
>>{'hour': datetime.datetime(2019, 7, 20, 12, 0), 'number': 3}
>>{'hour': datetime.datetime(2019, 7, 20, 13, 0), 'number': 2}

2.6 分

from django.db import connection
from django.db.models import Count
# 'year', 'month', 'day', 'hour', 'minute', 'second'

select = {'minute': connection.ops.datetime_trunc_sql('minute', 'establish', 8)}
result = models.FocusOnRecord.objects.extra(select=select).values('minute').annotate(number=Count('id')).order_by("minute")[0:9]

for key in result:
 print(key)
 
>>{'minute': datetime.datetime(2019, 6, 28, 17, 22), 'number': 1}
>>{'minute': datetime.datetime(2019, 6, 29, 9, 6), 'number': 4}
>>{'minute': datetime.datetime(2019, 6, 29, 9, 39), 'number': 1}
>>{'minute': datetime.datetime(2019, 6, 29, 9, 41), 'number': 1}
>>{'minute': datetime.datetime(2019, 6, 29, 10, 4), 'number': 1}
>>{'minute': datetime.datetime(2019, 7, 2, 14, 57), 'number': 1}
>>{'minute': datetime.datetime(2019, 7, 11, 14, 48), 'number': 1}
>>{'minute': datetime.datetime(2019, 7, 11, 14, 54), 'number': 1}
>>{'minute': datetime.datetime(2019, 7, 11, 15, 40), 'number': 1}

2.7 秒

from django.db import connection
from django.db.models import Count
# 'year', 'month', 'day', 'hour', 'minute', 'second'

select = {'second': connection.ops.datetime_trunc_sql('second', 'establish', 8)}
result = models.FocusOnRecord.objects.extra(select=select).values('second').annotate(number=Count('id')).order_by("second")[0:9]

for key in result:
 print(key)

>>{'second': datetime.datetime(2019, 6, 28, 17, 22, 54), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 9, 6, 1), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 9, 6, 18), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 9, 6, 35), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 9, 6, 36), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 9, 39, 30), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 9, 41, 17), 'number': 1}
>>{'second': datetime.datetime(2019, 6, 29, 10, 4, 15), 'number': 1}
>>{'second': datetime.datetime(2019, 7, 2, 14, 57, 56), 'number': 1}

到此這篇關(guān)于Django中日期時(shí)間型字段進(jìn)行年月日時(shí)分秒分組統(tǒng)計(jì)的文章就介紹到這了,更多相關(guān)Django 日期時(shí)間型字段統(tǒng)計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于python tkinter的點(diǎn)名小程序功能的實(shí)例代碼

    基于python tkinter的點(diǎn)名小程序功能的實(shí)例代碼

    這篇文章主要介紹了基于python tkinter的點(diǎn)名小程序,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 如何在python中使用selenium的示例

    如何在python中使用selenium的示例

    這篇文章主要介紹了如何在python中使用selenium的示例,selenium提供了一個(gè)通用的接口,可模擬用戶來(lái)操作瀏覽器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • python?ChainMap的使用詳解

    python?ChainMap的使用詳解

    chainMap是邏輯上合并兩個(gè)字典為一個(gè)邏輯單元,合并后的結(jié)構(gòu)實(shí)際上是一個(gè)列表,只是邏輯上是仍然為一個(gè)字典(并未生成新的),對(duì)此列表的操作模擬了各種字典的操作,這篇文章主要介紹了python?ChainMap的使用,需要的朋友可以參考下
    2023-03-03
  • 手把手教你Python yLab的繪制折線圖的畫法

    手把手教你Python yLab的繪制折線圖的畫法

    這篇文章主要介紹了手把手教你Python yLab的繪制折線圖的畫法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python利用sklearn包編寫決策樹(shù)源代碼

    python利用sklearn包編寫決策樹(shù)源代碼

    這篇文章主要為大家詳細(xì)介紹了python利用sklearn包編寫決策樹(shù)源代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • python使用opencv對(duì)圖像添加噪聲(高斯/椒鹽/泊松/斑點(diǎn))

    python使用opencv對(duì)圖像添加噪聲(高斯/椒鹽/泊松/斑點(diǎn))

    這篇文章主要介紹了python使用opencv對(duì)圖像添加噪聲(高斯/椒鹽/泊松/斑點(diǎn)),具有一定的學(xué)習(xí)價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2022-04-04
  • Python的numpy庫(kù)中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法

    Python的numpy庫(kù)中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法

    下面小編就為大家分享一篇Python的numpy庫(kù)中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python實(shí)用技巧之輕松處理大型文件

    Python實(shí)用技巧之輕松處理大型文件

    Python在文件處理方面提供了非常強(qiáng)大的支持,然而,當(dāng)處理大型文件時(shí),標(biāo)準(zhǔn)的文件處理技術(shù)會(huì)導(dǎo)致高內(nèi)存使用,所以下面我們就來(lái)看看如何在Python中有效地處理大型文件吧
    2024-03-03
  • python每次處理固定個(gè)數(shù)的字符的方法總結(jié)

    python每次處理固定個(gè)數(shù)的字符的方法總結(jié)

    使用python每次處理固定個(gè)數(shù)的字符,很多情況下都會(huì)遇到。本文對(duì)可能的方法做下總結(jié),供各位朋友學(xué)習(xí)參考
    2013-01-01
  • Python Web項(xiàng)目Cherrypy使用方法鏡像

    Python Web項(xiàng)目Cherrypy使用方法鏡像

    這篇文章主要介紹了Python Web項(xiàng)目Cherrypy使用方法鏡像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論