我已经找到了这个问题,但是我找不到涉及日期分区的类似问题,这就是我被困住的地方。
所以我有一个包含campaign_id,user_id,impression_count,click_count列的表格,其中包含日,月和年份作为分区。现在我需要查找昨天直播的广告系列,然后汇总这些广告系列过去30天的数据。我基本上想要找到的是向用户显示同一广告系列的展示次数。
这是我为了获取1天数据而编写的查询:
SELECT result.impressions, result.campaign, result.clicks, count(result.user_id) as FREQ
FROM
(SELECT campaign_id as campaign, user_id, sum(impression_count) as impressions, sum(click_count) as clicks
from table
where properties_day={day}
AND properties_month={month}
AND properties_year={year}
group by campaign_id, user_id)AS result
GROUP BY campaign, impressions, clicks;
现在我一直试图在过去30天内这样做,但这是我能够得到的。我不知道如何使用JOIN并同时使用WHERE条件,例如Date_Table2 = Date_Table1 - 30天。
SELECT T1.campaign_id, T1.user_id, T1.sum(impression_count),T1.sum(click_count),
T2.campaign_id, T2.user_id, T2.sum(impression_count), T2.sum(click_count)
FROM Table as T1
LEFT JOIN Table as T2
ON T1.campaign_id = T2.campaign_id
AND T1.user_id=T2.user_id
WHERE T1.properties_day={day}
AND T1.properties_month={month}
AND T1.properties_year={year}
AND T2.properties_day BETWEEN T1.properties_day - 30 AND T1.properties_et_day - 1
AND T2.properties_month<=T1.properties_month
AND T2.properties_year T1.properties_year
group by T1.campaign_id, T1.user_id)AS test
以下是示例数据:
User_ID | Campaign_ID | Impression_count | Click_count
820103109 | 123 | 1 | 0
837216609 | 123 | 1 | 1
820103109 | 456 | 0 | 0
870609097 | 456 | 1 | 1
853977465 | 456 | 1 | 0
842493228 | 456 | 1 | 1
这就是我想要的最终输出:
Campaign_ID Frequency Impressions Clicks
123 0 456 0
123 1 7382 281
123 2 2531 172 and so on...
我是这个的初学者,所以任何帮助将不胜感激。