我希望每周从创建日期到当前日期绘制投资的观看次数。
每当有人访问投资时,我都会将其插入到表格中
investment_id
,user_id
和timestamp(datetime)
。
表格如下:
investment_id
investment_id user_id Time_stamp 499 233 2015-01-22 09:00:42 499 256 2015-01-21 09:00:42 499 275 2015-01-20 09:00:42 499 233 2015-01-14 09:00:42 499 233 2015-01-14 09:00:42 499 233 2015-01-10 09:00:42 499 273 2015-01-06 09:00:42 499 347 2015-01-02 09:00:42 499 343 2015-01-01 09:00:42 499 344 2015-01-01 09:00:42
因此,对于2015-01-01 00:00:00创建的investment_id 499,结果如下:
Week Views 1 4 2 3 3 2 4 1
找到了解决方法:
SELECT (CASE (ceiling(datediff(Time_stamp,"2015-01-01 00:00:00")/7)) WHEN 0 THEN 1
ELSE (ceiling(datediff(Time_stamp,"2015-01-01 00:00:00")/7)) END) as weeks,
count(Investment_Id) as viewCount
from log_table where Investment_Id =499 group by weeks
答案 0 :(得分:1)
Needed是这样的临时/虚拟表:
+ ------------- + ---------- + ---------- + ---------- +
| investment_id | week_start | week_end | weeknumber |
+ ------------- + ---------- + ---------- + ---------- +
| 499 | 2015-01-02 | 2015-01-09 | 1 |
| 499 | 2015-01-09 | 2015-01-16 | 2 |
| 499 | 2015-01-16 | 2015-01-23 | 3 |
| 499 | 2015-01-23 | 2015-01-30 | 4 |
+ ------------- + ---------- + ---------- + ---------- +
这可以通过一种生成系列来实现:
SELECT
499 investment_id,
@wstart := @startdate + INTERVAL @wseq * 7 DAY week_start,
@wend := @wstart + INTERVAL 7 DAY week_end,
@wseq := @wseq + 1 weeknumber
FROM
information_schema.collations
CROSS JOIN
(SELECT @startdate := (SELECT MIN(DATE(time_stamp))
FROM log_table
WHERE investment_id = 499), @wseq := 0) u
HAVING
week_start <= CURRENT_DATE
交叉连接是用于启动用户变量,具有足够行的表可以从该用户变量生成系列。 information_schema.collations始终可用,并且有超过200行。
如果将其放在子查询中,则可以在investent_id和week_start与week_end之间的time_stamp上连接具有投资视图的表。
这将导致:
SELECT
s.weeknumber,
COUNT(v.user_id) views
FROM
(
SELECT
499 investment_id,
@wstart := @startdate + INTERVAL @wseq * 7 DAY week_start,
@wend := @wstart + INTERVAL 7 DAY week_end,
@wseq := @wseq + 1 weeknumber
FROM
information_schema.collations
CROSS JOIN
(SELECT @startdate := (SELECT MIN(DATE(start_date))
FROM log_table
WHERE investment_id = 499), @wseq := 0) u
HAVING
weekstart <= CURRENT_DATE
) s
LEFT JOIN
log_table v
ON s.investment_id = v.investment_id
AND v.time_stamp >= s.week_start
AND v.time_stamp < s.week_end
GROUP BY s.weeknumber
答案 1 :(得分:0)
select week(time_stamp), count(investment_id)
group by time_stamp, investment_id
答案 2 :(得分:0)
两个选项:
使用DatePart方法从datetime列获取一周, 但是这不会给你预期的结果,因为你的输出结果显示,6日不会与1月1日和2日的日历周相同。
使用&#34; custom&#34;确定一周是什么的代码,即。第一周是每个月的第1周和第7周等等。
以下两个选项的示例
祝你好运。 SELECT *
into #tmp
FROM
(
select 499 investment_id, 233 user_id, '2015-01-22 09:00:42' Time_stamp
union all
select 499, 256, '2015-01-21 09:00:42'
union all
select 499, 275, '2015-01-20 09:00:42'
union all
select 499, 233, '2015-01-14 09:00:42'
union all
select 499, 233, '2015-01-14 09:00:42'
union all
select 499, 233, '2015-01-10 09:00:42'
union all
select 499, 273, '2015-01-06 09:00:42'
union all
select 499, 347, '2015-01-02 09:00:42'
union all
select 499, 343, '2015-01-01 09:00:42'
union all
select 499, 344, '2015-01-01 09:00:42'
) a
Select DATEPART(wk,Time_stamp) week , count(investment_id)
FROM #tmp
group by DATEPART(wk,Time_stamp)
SELECT b.WeekNumber , count(b.investment_id)
FROM (
SELECT a.* ,
case
when a.DayInMonth between 1 and 7 then 1
when a.DayInMonth between 8 and 14 then 2
when a.DayInMonth between 16 and 22 then 3
when a.DayInMonth between 23 and 29 then 4
else 5
end as WeekNumber
FROM (
Select day(Time_stamp) DayInMonth , *
FROM #tmp
) a
)b
group by b.WeekNumber