2014-07-03 67 views
0

計算與日期間隔統計我要計算的日期和動態間隔(周,月,年)整理一些狀態信息(與MySQL後端)。 這裏是一個小例子:如何通過MySQL的

MySQL表: tracker_click

| ID | SITE_ID | CREATED_AT   | 
| ---- |---------| --------------------| 
| 153 | 2  | 2013-07-22 15:43:25 | 
| 154 | 2  | 2013-07-25 16:45:46 | 
| 2501 | 2  | 2013-09-15 17:45:48 | 

我想SITE_ID按周來獲得總點擊數上個月與一個查詢 並按月同樣的事情去年。

什麼,我想通過本週上個月的一個例子是:

| click number | SITE_ID | BEGIN_DATE   | END_DATE   | 
|----------------|---------|----------------------|---------------------| 
| 25    | 2 | 2013-07-01 00:00:00 | 2013-07-08 00:00:00 | 
| 19    | 2 | 2013-08-09 00:00:00 | 2013-08-16 00:00:00 | 
| 53    | 2 | 2013-0717- 00:00:00 | 2013-08-24 00:00:00 | 

我不知道是否有一個解決方案,使exaclty這個數組只有一個查詢沒有任何其他進程。

謝謝

+0

要做到這一點的幾個月,假設你要個月上市,其中計數爲零,爲你生成所需的時間週期的列表,然後左邊加入你的數據。 – Kickstart

回答

2

這應該讓你上個月的計數(即,過去4周),包括周其中計數爲每個站點id爲0。如果您有一個站點表來從中獲取站點ID,則意味着交叉連接到子查詢可以用一個簡單的交叉連接替換爲一個表。

這會生成一系列從0到5的數字,並從當前日期中減去該週數,格式爲給出結果周的週日和週六,並檢查結束周是當前日期之間的一週並且當前日期減去1個月(以這種方式完成,而不是僅僅減去4周以應付可變長度的月份)。

SELECT Weeks.aWeek_start, Weeks.aWeek_end, all_site_id.site_id, COUNT(tracker_click.id) 
FROM 
(
    SELECT STR_TO_DATE(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL units.i WEEK), '%Y%U Sunday 00:00:00'), '%X%V %W %H:%i:%s') AS aWeek_start, 
      STR_TO_DATE(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL units.i WEEK), '%Y%U Saturday 23:59:59'), '%X%V %W %H:%i:%s') AS aWeek_end 
    FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5)units 
    WHERE DATE_FORMAT(DATE_SUB(NOW(), INTERVAL units.i WEEK), '%Y%U') BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%U') AND DATE_FORMAT(NOW(), '%Y%U') 
) Weeks 
CROSS JOIN 
(
    SELECT DISTINCT site_id 
    FROM tracker_click 
) AS all_site_id 
LEFT OUTER JOIN tracker_click 
ON tracker_click.CREATED_AT BETWEEN Weeks.aWeek_start AND Weeks.aWeek_end 
AND tracker_click.site_id = all_site_id.site_id 
GROUP BY Weeks.aWeek_start, Weeks.aWeek_end, all_site_id.site_id 

類似的查詢可以完成全年

SELECT Months.aMonth_start, Months.aMonth_end, all_site_id.site_id, COUNT(tracker_click.id) 
FROM 
(
    SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL units.i MONTH), '%Y/%m/01 00:00:00') AS aMonth_start, 
      DATE_FORMAT(LAST_DAY(DATE_SUB(NOW(), INTERVAL units.i MONTH)), '%Y/%m/%d 23:59:59') AS aMonth_end 
    FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11)units 
) Months 
CROSS JOIN 
(
    SELECT DISTINCT site_id 
    FROM tracker_click 
) AS all_site_id 
LEFT OUTER JOIN tracker_click 
ON tracker_click.CREATED_AT BETWEEN Months.aMonth_start AND Months.aMonth_end 
AND tracker_click.site_id = all_site_id.site_id 
GROUP BY Months.aMonth_start, Months.aMonth_end, all_site_id.site_id 
+0

謝謝你的答案的Kickstart, 我有正確的行號,日期也是正確的,但跟蹤計數不正常, 每一行給我點擊的錯誤計數。 我有隻有一行在我的表站點954 tracker_count應始終爲0,除了最後一行(1) – Jaybe

+0

看來,「計數(tracker_click_id)」計數每次都行不僅每間行日期 – Jaybe

+0

我看不到它的錯誤(雖然我可能會錯過一些東西)。你可以在SQL小提琴上設置表格和少量數據來演示它嗎? – Kickstart

0
SELECT count(*), site_id, extract(WEEK from created_at) as start_date, 
date_add(extract(WEEK from created_at), 1, week) as end_date 
FROM click_tracker 
GROUP BY site_id, extract(WEEK from created_at) 

,你可以添加where子句額外篩選結果。