代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如果日期列有时间戳,如何按月对列进行计数?

sql
  •  -1
  • RustyShackleford  · 技术社区  · 7 年前

    我在一张表中有两列:

    id    date
    1     1/1/18 12:55:00 AM
    2     1/2/18 01:34:00 AM
    3     1/3/18 02:45:00 AM
    

    如果时间被附加到日期列中,我如何计算每月的ID数?

    输出将是:

    Count   month 
    3        1 
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Gordon Linoff    7 年前

    select extract(month from date) as month, count(*) 
    from t
    group by extract(month from date);
    

    month() extract()

        2
  •  0
  •   Zaynul Abadin Tuhin    7 年前

    select DATE_PART('month', date) as month,count(id) from yourtable
    group by DATE_PART('Month', date)