如果我有表日志:
ID Date P_id TYPE
-------------------------------
1 2016-9-1 11 adClick
2 2016-9-1 22 adComplete
3 2016-9-1 11 adClick
4 2016-9-3 22 adClick
5 2016-9-3 22 adClick
6 2016-9-1 11 adClick
7 2016-9-3 22 adComplete
8 2016-9-1 11 adClick
9 2016-9-3 11 adClick
-------------------------------和另一个具有相同日期& P_id的表报告,如下所示:
ID Date P_id clicks
--------------------------------
1 2016-9-1 11
2 2016-9-3 11
3 2016-9-1 11
4 2016-9-3 11
5 2016-9-1 22
6 2016-9-1 11
5 2016-9-1 22
---------------------------------我需要MySQL查询来根据键(Date & P_id)在报表中分散点击,如果有余数,则随机添加一个,直到选择了相同的键完成余数:
clicks =
count of rows having (Date & P_id) in logs table
------------------- Divistion (/) -------------------
count of rows having (Date & P_id) in Report and Type is adClick日志表中按键(Date & P_id)和事件类型为adClick的每个组的计数为:
2016-9-1 11 --- count--> 4
2016-9-1 22 --- count--> 0
2016-9-3 11 --- count--> 1
2016-9-3 22 --- count--> 2和报表中的计数:
2016-9-1 11 --- count--> 3
2016-9-1 22 --- count--> 2
2016-9-3 11 --- count--> 2
2016-9-3 22 --- count--> 0因此,该表将为:
ID Date P_id clicks
--------------------------------
1 2016-9-1 11 4 / 3 = 1 and 1 as remainder
2 2016-9-3 11 1 / 2 = 0 and 1 as remainder
3 2016-9-1 11 4 / 3 = 1 and 1 as remainder
4 2016-9-3 11 1 / 2 = 0 and 1 as remainder
5 2016-9-1 22 0 / 2 = 0
6 2016-9-1 11 4 / 3 = 1 and 1 as remainder
5 2016-9-1 22 0 / 2 = 0
---------------------------------示例解释更多信息,第一行:
2016-9-1 11 4 / 3
4 rows (2016-9-1 11) in logs table with type=adClick by
3 row (2016-9-1 11) in report table 我已经完成了保存点击的部分,没有剩余的部分,所以我在report表中查询的记录是:
ID Date P_id clicks
--------------------------------
1 2016-9-1 11 1
2 2016-9-3 11 0
3 2016-9-1 11 1
4 2016-9-3 11 0
5 2016-9-1 22 0
6 2016-9-1 11 1
5 2016-9-1 22 0
---------------------------------现在,我想将剩余部分--可能是随机的--扩展到报表中,这样相同键的总和就保持不变:
ID Date P_id clicks
--------------------------------
1 2016-9-1 11 1 + 1
2 2016-9-3 11 0 + 1
3 2016-9-1 11 1
4 2016-9-3 11 0
5 2016-9-1 22 0
6 2016-9-1 11 1
5 2016-9-1 22 0
---------------------------------因此,(2016-9- 11)的点击量之和现在对于两个表都是4。
下面是我在使用剩余部分之前用来更新的查询:
UPDATE report AS r
INNER JOIN
(
SELECT DATE(ctr_date) as report_date, report.placement_id, count(*) as cnt_report, cnt_event_type
FROM report
INNER JOIN
(
SELECT DATE(ctr_date) as event_date, placement_id, SUM(event_type = 'adClick') as cnt_event_type
FROM logs
GROUP BY DATE(ctr_date),placement_id
) inner_report
ON report.date = inner_report.event_date AND report.placement_id = inner_report.placement_id
GROUP BY report.date, report.placement_id
) result_table
ON r.date = result_table.report_date AND r.placement_id= result_table.placement_id
SET r.clicks = COALESCE( (cnt_event_type - MOD(cnt_event_type,cnt_report))/cnt_report ,0);我在考虑对相同的键和用法使用相同的查询,但带有限制(余数):
SET r.clicks = r.clicks + 1发布于 2017-02-05 10:12:43
我将相同密钥的剩余部分保存在一个临时表中。并使用该表遍历同一关键字的报告表,并根据临时表中的余数值递增先前的值。
https://stackoverflow.com/questions/42034250
复制相似问题