How to query GROUP BY Month in a Year

后端 未结 5 911
情书的邮戳
情书的邮戳 2021-02-01 01:07

I am using Oracle SQL Developer. I essentially have a table of pictures that holds the columns:

[DATE_CREATED(date), NUM_of_PICTURES(int)]

and if I do a select *

5条回答
  •  天命终不由人
    2021-02-01 01:33

    I would be inclined to include the year in the output. One way:

    select to_char(DATE_CREATED, 'YYYY-MM'), sum(Num_of_Pictures)
    from pictures_table
    group by to_char(DATE_CREATED, 'YYYY-MM')
    order by 1
    

    Another way (more standard SQL):

    select extract(year from date_created) as yr, extract(month from date_created) as mon,
           sum(Num_of_Pictures)
    from pictures_table
    group by extract(year from date_created), extract(month from date_created)
    order by yr, mon;
    

    Remember the order by, since you presumably want these in order, and there is no guarantee about the order that rows are returned in after a group by.

提交回复
热议问题