SQL needed: sum over values by month

后端 未结 4 876
离开以前
离开以前 2020-12-31 05:42

i have an table with values like this:

count1   count2  count3  month
12        1       4       01/12/2011
6         5       4       23/12/2011
14                 


        
相关标签:
4条回答
  • 2020-12-31 06:00
    SELECT SUM(count1), SUM(count2), SUM(count3), MONTH(month)
      FROM myTable
     GROUP BY MONTH(month)
    

    Since MONTH is an SQL Server keyword, you might have to escape the column name month (e.g. [month]), if you date column is really called like that. (Thanks Endy for that comment!)

    Also, if data selected span more than one year, you will probably need to group by year as well:

    SELECT SUM(count1), SUM(count2), SUM(count3), MONTH(month), YEAR(month)
      FROM myTable
     GROUP BY MONTH(month), YEAR(month)
    
    0 讨论(0)
  • 2020-12-31 06:01
    SELECT
    SUM(count1) as sum_count1,
    to_char(month, 'yyyy-MM') as month
    FROM
    dataframe 
    GROUP BY
    month
    
    0 讨论(0)
  • 2020-12-31 06:10

    reduce the query burden

    SELECT SUM(count1), SUM(count2), SUM(count3), substring(month,4,2)   
    FROM myTable  
    GROUP BY substring(month,4,2)
    
    0 讨论(0)
  • 2020-12-31 06:16
    SELECT SUM(count1), SUM(count2), SUM(count3), MONTH(month)   
    FROM Table  
    GROUP BY trunc(month,'mm')
    

    This will run in PL/SQL developer :)

    0 讨论(0)
提交回复
热议问题