gpt4 book ai didi

MySQL 查询 : Aggregation at two different level

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:29 25 4
gpt4 key购买 nike

我有两张 table

mysql> select * from report;
+----+----------+------------+------------------+-------------+
| id | campaign | advertiser | impression_count | click_count |
+----+----------+------------+------------------+-------------+
| 1 | camp1 | adv1 | 20 | 6 |
| 2 | camp2 | adv2 | 10 | 2 |
| 3 | camp1 | adv1 | 15 | 3 |
| 4 | camp2 | adv2 | 6 | 1 |
+----+----------+------------+------------------+-------------+
4 rows in set (0.00 sec)

mysql> select * from device;
+-----------+-----------+
| report_id | device_id |
+-----------+-----------+
| 1 | d1 |
| 1 | d2 |
| 2 | d1 |
| 2 | d3 |
| 2 | d4 |
| 3 | d2 |
| 3 | d4 |
| 4 | d3 |
| 4 | d4 |
| 4 | d5 |
+-----------+-----------+
10 rows in set (0.00 sec)

我想要在广告系列和广告商级别汇总的报告,其中包含展示次数和点击次数的总和以及不同的 device_id。所以我在下面写了查询

SELECT 
campaign,
advertiser,
sum(impression_count),
sum(click_count),
count(DISTINCT device_id)
FROM report
LEFT JOIN device ON report.id = device.report_id
GROUP BY campaign, advertiser;
+----------+------------+-----------------------+------------------+---------------------------+
| campaign | advertiser | sum(impression_count) | sum(click_count) | count(distinct device_id) |
+----------+------------+-----------------------+------------------+---------------------------+
| camp1 | adv1 | 70 | 18 | 3 |
| camp2 | adv2 | 48 | 9 | 4 |
+----------+------------+-----------------------+------------------+---------------------------+

这里是因为加入印象计数和 click_count 是针对多行聚合的。想要的是

+----------+------------+-----------------------+------------------+---------------------------+
| campaign | advertiser | sum(impression_count) | sum(click_count) | count(distinct device_id) |
+----------+------------+-----------------------+------------------+---------------------------+
| camp1 | adv1 | 35 | 9 | 3 |
| camp2 | adv2 | 16 | 3 | 4 |
+----------+------------+-----------------------+------------------+---------------------------+

http://sqlfiddle.com/#!2/05dd9d/1

找到不太好的解决方案

select campaign,advertiser,ic,cc,count(distinct device_id) 
from (
select
group_concat(id) as id,
sum(impression_count)as ic,
sum(click_count)as cc,
campaign,advertiser
FROM report har GROUP BY campaign,advertiser) a
LEFT JOIN device dr ON FIND_IN_SET(dr.report_id, a.id)
group by a.id
);

但这使用组连接,因此如果 group_concat 结果的长度很大,可能会出现问题。

最佳答案

您要做的是执行两个不同的查询,然后加入结果集。外层的select只是选择我们真正想要的信息,将两个临时表join在一个共同的值上。如果您不想为整个事件选择设备表中的不同设备,您也可以使用 id 和 report_id 执行此操作。

select `firsttable`.campaign, `firsttable`.advertiser, a, b, c from 
(select id, campaign, advertiser, sum(impression_count) as a, sum(click_count) as b
from report
group by campaign, advertiser
) as firsttable
left join
(select campaign, advertiser, count(distinct device_id) as c
from device, report
where id=report_id
group by campaign, advertiser
) as secondtable on `firsttable`.campaign=`secondtable`.campaign and
`firsttable`.advertiser=`secondtable`.advertiser;

SQLFiddle:http://sqlfiddle.com/#!2/8bd63/20

这个查询是这两个临时表的组合:

| ID | CAMPAIGN | ADVERTISER |   A |   B |
|----|----------|------------|-----|-----|
| 1 | camp1 | adv1 | 35 | 9 |
| 5 | camp1 | adv2 | 900 | 900 |
| 2 | camp2 | adv2 | 16 | 3 |

| CAMPAIGN | ADVERTISER | C |
|----------|------------|---|
| camp1 | adv1 | 3 |
| camp2 | adv2 | 4 |

结果:

| CAMPAIGN | ADVERTISER |   A |   B |      C |
|----------|------------|-----|-----|--------|
| camp1 | adv1 | 35 | 9 | 3 |
| camp1 | adv2 | 900 | 900 | (null) |
| camp2 | adv2 | 16 | 3 | 4 |

您的查询的问题是在将报告表与设备表合并时会出现重复行。你最终会得到这样的结果:

| CAMPAIGN | ADVERTISER | IMPRESSION_COUNT | CLICK_COUNT | DEVICE_ID |
|----------|------------|------------------|-------------|-----------|
| camp1 | adv1 | 20 | 6 | d1 |
| camp1 | adv1 | 20 | 6 | d2 |
| camp2 | adv2 | 10 | 2 | d1 |
| camp2 | adv2 | 10 | 2 | d3 |
| camp2 | adv2 | 10 | 2 | d4 |
| camp1 | adv1 | 15 | 3 | d2 |
| camp1 | adv1 | 15 | 3 | d4 |
| camp2 | adv2 | 6 | 1 | d3 |
| camp2 | adv2 | 6 | 1 | d4 |
| camp2 | adv2 | 6 | 1 | d5 |
| camp1 | adv2 | 900 | 900 | (null) |

关于MySQL 查询 : Aggregation at two different level,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com