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のクエリを必要としますキー(日付& 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
キー(日& 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
レポートテーブルでの私のクエリでレコードがされているので、私は、余りなしでクリックを節約の一部を行っている。今、私は残りを広めたい
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月1日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
あなたのコードでこれを行うか、ストアドプロシージャを追加する方が簡単でメンテナンスが容易なようです。あなたのユースケースによって最適な方法はどれですか。 – user2120275