1

我是mysql的新手。

我有点击次数,期间(日期)的调查。现在我必须找出每月的点击次数,例如:

MON  CLICKS
nov  0 
oct  34 
sep  67 
aug  89

我使用过这样的代码:

select MONTHNAME(period) mon,  IFNULL(count(id),0) as Clicks
from survey
where period > DATE_SUB(now(), INTERVAL 3 MONTH)
group by EXTRACT(MONTH FROM period)

如果没有记录,它就不起作用。

这里有一件事我假设那个月没有记录它应该显示0:如果没有nov点击次数的记录应该是0

my table structure was like this 
CREATE TABLE `survey` (
  `id` int(2) NOT NULL auto_increment,
   `period` datetime default NULL)

在过去的四个星期里,我使用了

 SELECT uq.timespan, COALESCE(tsq.TotalClicks, 0) as Clicks FROM (
SELECT '22-28 days' as timespan
union SELECT '15-21 days'
union SELECT '8-14 days'
union SELECT 'up to 7 days'

)uq LEFT JOIN (
SELECT CASE 
    WHEN submitdate >= NOW() - INTERVAL 4 WEEK
                AND submitdate < NOW() - INTERVAL 3 WEEK THEN '22-28 days'
    WHEN submitdate >= NOW() - INTERVAL 3 WEEK
           AND submitdate < NOW() - INTERVAL 2 WEEK THEN '15-21 days'
WHEN submitdate >= NOW() - INTERVAL 2 WEEK
    AND submitdate < NOW() - INTERVAL 1 WEEK THEN '8-14 days'
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN 'up to 7 days'
       END Weeksubmitdate, 
           count(id) TotalClicks
FROM  survey 
WHERE submitdate >= NOW() - INTERVAL 4 WEEK
GROUP BY Weeksubmitdate
)tsq ON uq.timespan = tsq.Weeksubmitdate

有什么帮助吗?

4

3 回答 3

3

我通常做数据透视表来实现这一点。假设您的点击信息存储在名为 SURVEY 的表中,并假设只有点击的日期/时间存储在 SURVEY 表的一列中(这就是您所需要的),那么这是一种方法:

select   year(period),
         sum(case when month(period)=1 then 1 else 0 end) jan,
         sum(case when month(period)=2 then 1 else 0 end) feb,
         sum(case when month(period)=3 then 1 else 0 end) mar,
         sum(case when month(period)=4 then 1 else 0 end) apr,
         sum(case when month(period)=5 then 1 else 0 end) may,
         sum(case when month(period)=6 then 1 else 0 end) jun,
         sum(case when month(period)=7 then 1 else 0 end) jul,
         sum(case when month(period)=8 then 1 else 0 end) aug,
         sum(case when month(period)=9 then 1 else 0 end) sep,
         sum(case when month(period)=10 then 1 else 0 end) oct,
         sum(case when month(period)=11 then 1 else 0 end) nov,
         sum(case when month(period)=11 then 1 else 0 end) dec
from     survey
group by year(period)

输出类似于:

---------------------------------------------------------------------------------
| Year  | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
---------------------------------------------------------------------------------
| 2012  |  5  |  20 |  13 | 0   |  0  |  65 |  15 |  0  |  0  |  21 |  0  |  0  |
---------------------------------------------------------------------------------

我什至为你设置了相同的 Fiddle SQL SQL Fiddle Demo

另一种方法(基于过去 4 个月的列,即使计数为零): SQL Fiddle Demo

SELECT mon,
       sum(clicks) clicks
FROM   ( SELECT month(period) mnth,
                date_format(period,'%b') mon,
                count(1) clicks
         FROM   survey
         WHERE  month(period) BETWEEN month(curdate()) - 4 AND month(curdate())
         GROUP BY 1, 2
         UNION ALL
         SELECT 1 mnth, 'Jan' mon, 0 clicks
         UNION ALL
         SELECT 2 mnth, 'Feb' mon, 0 clicks
         UNION ALL
         SELECT 3 mnth, 'Mar' mon, 0 clicks
         UNION ALL
         SELECT 4 mnth, 'Apr' mon, 0 clicks
         UNION ALL
         SELECT 5 mnth, 'May' mon, 0 clicks
         UNION ALL
         SELECT 6 mnth, 'Jun' mon, 0 clicks
         UNION ALL
         SELECT 7 mnth, 'Jul' mon, 0 clicks
         UNION ALL
         SELECT 8 mnth, 'Aug' mon, 0 clicks
         UNION ALL
         SELECT 9 mnth, 'Sep' mon, 0 clicks
         UNION ALL
         SELECT 10 mnth, 'Oct' mon, 0 clicks
         UNION ALL
         SELECT 11 mnth, 'Nov' mon, 0 clicks
         UNION ALL
         SELECT 12 mnth, 'Dec' mon, 0 clicks) a
WHERE  mnth BETWEEN month(curdate()) - 4 AND month(curdate())
GROUP BY 1
ORDER BY mnth
于 2012-11-06T12:22:48.557 回答
0

您需要加入一个包含所有月份名称的表。这是一种方法:

select
    mon,
    ifnull(count(id), 0) as Clicks
from (select 'nov' as mon union select 'oct' union select 'sep' union select 'aug') m
left join survey on MONTHNAME(period) = mon
where submitdate > DATE_SUB(now(), INTERVAL 3 MONTH) 
group by 1
于 2012-11-06T12:25:25.653 回答
0
select MONTHNAME(STR_TO_DATE(month(period), '%m'))as 'month',count(*) as clicks
 from survey group by month(period)
于 2012-11-06T12:59:58.437 回答