How to query GROUP BY Month in a Year

后端 未结 5 827
情书的邮戳
情书的邮戳 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:25

    I am doing like this in MSSQL

    Getting Monthly Data:

     SELECT YEAR(DATE_CREATED) [Year], MONTH(DATE_CREATED) [Month], 
         DATENAME(MONTH,DATE_CREATED) [Month Name], SUM(Num_of_Pictures) [Pictures Count]
        FROM pictures_table
        GROUP BY YEAR(DATE_CREATED), MONTH(DATE_CREATED), 
         DATENAME(MONTH, DATE_CREATED)
        ORDER BY 1,2
    

    Getting Monthly Data using PIVOT:

    SELECT *
    FROM (SELECT YEAR(DATE_CREATED) [Year], 
           DATENAME(MONTH, DATE_CREATED) [Month], 
           SUM(Num_of_Pictures) [Pictures Count]
          FROM pictures_table
          GROUP BY YEAR(DATE_CREATED), 
          DATENAME(MONTH, DATE_CREATED)) AS MontlySalesData
    PIVOT( SUM([Pictures Count])   
        FOR Month IN ([January],[February],[March],[April],[May],
        [June],[July],[August],[September],[October],[November],
        [December])) AS MNamePivot
    

提交回复
热议问题