将给定月份的计数作为列返回

时间:2013-11-18 11:01:48

标签: mysql

我正在寻找一个查询,以便将给定月份的计数(分页数)作为列返回。我无法找到一个完全符合我需要的例子。

例如:

Title | Path | Total | September | October | November |
-------------------------------------------------------
Home  | /    |   500 |       200 |     125 |      175 |

它不一定是完全动态的,因为它将是一次性操作以获得一些旧的统计数据。

我希望你们能帮帮我

更新

带有'命中'的表的结构类似于:

id | page | date

我正在加入其他表格以获取路径和标题,但这些与此问题并不相关

3 个答案:

答案 0 :(得分:1)

例如2013年:

SELECT 
    Page as Path,
    COUNT(*) as Total,
    SUM(CASE WHEN MONTH(date)=1 THEN 1 ELSE 0 END) as JAN,
    SUM(CASE WHEN MONTH(date)=2 THEN 1 ELSE 0 END) as FEB,
    ....
    SUM(CASE WHEN MONTH(date)=12 THEN 1 ELSE 0 END) as DEC
    FROM HitsTable
    WHERE YEAR(Date)=2013
GROUP BY Page;  

答案 1 :(得分:0)

假设您的表中包含名为Titlepathrequest_date的列,那么您的查询可能是:

 select Title, path,
        (select count(*)
           from page_views as pv2
          where month(request_date)=2
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as January,
        (select count(*)
           from page_views as pv1
          where month(request_date)=2
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as February,
        (select count(*)
           from page_views as pv3
          where month(request_date)=3
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as April,
        (select count(*)
           from page_views as pv5
          where month(request_date)=5
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as May,
        (select count(*)
           from page_views as pv6
          where month(request_date)=6
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as June,
        (select count(*)
           from page_views as pv7
          where month(request_date)=7
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as July,
        (select count(*)
           from page_views as pv1
          where month(request_date)=1
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as January,
        (select count(*)
           from page_views as pv8
          where month(request_date)=8
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as August,
        (select count(*)
           from page_views as pv9
          where month(request_date)=9
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as September,
        (select count(*)
           from page_views as pv10
          where month(request_date)=10
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as October,
        (select count(*)
           from page_views as pv11
          where month(request_date)=11
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as November,
        (select count(*)
           from page_views as pv12
          where month(request_date)=12
            and pv1.Title = pv.Title
            and pv2.path = pv.path
        ) as December
  from (select distinct Title,path
          from page_views
       ) as pv

答案 2 :(得分:0)

你想要什么,我想是一种叫做 PIVOT TABLE 的东西。对于你的桌面结构,我会og:

select id, path, jan+feb+....+dec as total,
       jan, feb, ... dec
  from (
select
    id, path,
    sum(case when extract(month from date)=1 then 1 else 0 end) as Jan,
    sum(case when extract(month from date)=2 then 1 else 0 end) as feb,
    sum(case when extract(month from date)=3 then 1 else 0 end) as mar,
    sum(case when extract(month from date)=4 then 1 else 0 end) as apr,
    sum(case when extract(month from date)=5 then 1 else 0 end) as may,
    sum(case when extract(month from date)=6 then 1 else 0 end) as jun,
    sum(case when extract(month from date)=7 then 1 else 0 end) as jul,
    sum(case when extract(month from date)=8 then 1 else 0 end) as aug,
    sum(case when extract(month from date)=9 then 1 else 0 end) as sep,
    sum(case when extract(month from date)=10 then 1 else 0 end) as oct,
    sum(case when extract(month from date)=11 then 1 else 0 end) as nov,
    sum(case when extract(month from date)=12 then 1 else 0 end) as dec
from 
    yourtable
group by id, path ) as tb
相关问题