【问题标题】:Count all type devices by week按周计算所有类型的设备
【发布时间】:2013-04-02 20:32:34
【问题描述】:

我有一张桌子:

idint(11) NOT NULL
type varchar(64) NOT NULL
created_on datetime NOT NULL

我可以在一个查询中按周计算所有类型的设备(iSO、Android...)吗?

样本输出:

Week iOS Android Other
1     4    5       6
2     3    5       9

【问题讨论】:

    标签: mysql symfony doctrine-orm


    【解决方案1】:

    这是完全可能的,使用类似于Output Sum of some column in week intervals throughout a year, week dates consistent with day 中详述的方法,但在您描述的输出中并不容易。处理此问题的最佳方法是执行 SELECT,按 WEEK 和 TYPE 分组。即

    SELECT
    
        -- Get the week and month of each row
        YEAR(created_on) AS Year,
        WEEK(created_on) AS Week,
        type
    
        -- Count how many rows are in this group
        COUNT(*) AS frequency
    
    FROM yourTable AS yt
    
    -- Order by month and then week, so that week 4 in Jan comes before week 4 in Feb
    GROUP BY
        Year ASC,
        Week ASC,
        type ASC
    

    这将给出如下输出

    Year    Week    Type       frequency
    2013    1       'iOS'      4
    2013    1       'Android'  5
    2013    1       'Other'    6
    2013    2       'iOS'      3
    2013    2       'Android'  5
    2013    2       'Other'    9
    

    【讨论】:

      猜你喜欢
      • 2015-02-13
      • 2023-03-29
      • 2015-02-19
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多