表之间的汇总行数差异
Aggregated row count differences between tables
我有两个 MySQL tables A 和 B 都具有此模式
ID
entity_id
asset
asset_type
0
12345
x
1
..
.........
.....
..........
我想获得两个 table 之间行数差异最大的聚合前 10/50/任何 entity_ids
。我 认为 我可以通过 entity_id
获得最高行数来手动完成此操作
select count(*), entity_id
-> from A
-> group by entity_id
-> order by count(*) desc;
并且只是手动比较 table B 的相同查询,但我想知道是否有一种方法可以在一个查询中执行此操作,比较每个不同 entity_id
和聚合行计数之间的差异。一些注意事项
entity_id
上有一个索引 tables
- Table B 对于每个
entity_id
将始终具有相等或更多的行数
示例输出
entity_id
difference
12345
100
3232
75
5992
40
and so on
for top 10/50
在每个 table 中聚合并加入结果以获得差异:
SELECT a.entity_id, b.counter - a.counter diff
FROM (SELECT entity_id, COUNT(*) counter FROM A GROUP BY entity_id) a
INNER JOIN (SELECT entity_id, COUNT(*) counter FROM B GROUP BY entity_id) b
ON a.entity_id = b.entity_id
ORDER BY diff DESC LIMIT 10
我有两个 MySQL tables A 和 B 都具有此模式
ID | entity_id | asset | asset_type |
---|---|---|---|
0 | 12345 | x | 1 |
.. | ......... | ..... | .......... |
我想获得两个 table 之间行数差异最大的聚合前 10/50/任何 entity_ids
。我 认为 我可以通过 entity_id
获得最高行数来手动完成此操作
select count(*), entity_id
-> from A
-> group by entity_id
-> order by count(*) desc;
并且只是手动比较 table B 的相同查询,但我想知道是否有一种方法可以在一个查询中执行此操作,比较每个不同 entity_id
和聚合行计数之间的差异。一些注意事项
entity_id
上有一个索引 tables- Table B 对于每个
entity_id
将始终具有相等或更多的行数
示例输出
entity_id | difference |
---|---|
12345 | 100 |
3232 | 75 |
5992 | 40 |
and so on | for top 10/50 |
在每个 table 中聚合并加入结果以获得差异:
SELECT a.entity_id, b.counter - a.counter diff
FROM (SELECT entity_id, COUNT(*) counter FROM A GROUP BY entity_id) a
INNER JOIN (SELECT entity_id, COUNT(*) counter FROM B GROUP BY entity_id) b
ON a.entity_id = b.entity_id
ORDER BY diff DESC LIMIT 10