SQL command to sum by day/month

前端 未结 2 788
有刺的猬
有刺的猬 2021-01-07 05:08

I have the following table:

+----+---------------------+-------------+-----------------+
| id | stat_time           |       reads |          writes |
+----+-         


        
相关标签:
2条回答
  • 2021-01-07 05:24

    You want to convert the stat_time to a day to do this. The method depends on the database. Here is one way:

    SELECT cast(stat_time as date) as stat_day, SUM(reads), SUM(writes)
    from this_table
    GROUP BY cast(stat_time as date)
    order by stat_day;
    

    Here are some other approaches (for MySQL and Oracle):

    SELECT date(stat_time) as stat_day, SUM(reads), SUM(writes)
    from this_table
    GROUP BY date(stat_time)
    order by stat_day;
    
    SELECT trunc(stat_time) as stat_day, SUM(reads), SUM(writes)
    from this_table
    GROUP BY trunc(stat_time)
    order by stat_day;
    
    0 讨论(0)
  • 2021-01-07 05:25

    let's assume you have a column with name D-DATE_START so

    $query = 'SELECT cast(D_DATE_START as date) as stat_day ,'
            .'sum(I_REV_MODEL) as totalDayRevenu '
            .'FROM t_show WHERE FK_MODEL=136 '
            ."and t_show.D_DATE_START between '".$after."' and '".$before."'"
            .' GROUP BY cast(D_DATE_START as date) '
            .' ORDER BY stat_day ';
    
    0 讨论(0)
提交回复
热议问题