您好,有 12 个表,我想为所有表中的某些字段获取唯一值。 通过运行以下查询(我在 2 个表之间进行了测试),我得到了一个可接受的结果,但是如果两个表中都存储了唯一值,则会重复计数:
SELECT "mmsi", "type", "l", "w", "flag", COUNT (*) FROM test_1 GROUP BY "mmsi", "type", "l", "w", "flag"
UNION
SELECT "mmsi", "type", "l", "w", "flag", COUNT (*) FROM test_2 GROUP BY "mmsi", "type", "l", "w", "flag"
我该如何解决?谢谢
最佳答案
通过聚合每个表,您会得到如下结果:
mmsi type l w flag count(*) 123 456 A B C 12 123 456 A B C 25 234 567 X Y Z 17 234 567 X Y Z 11
because UNION
doesn't detect any duplicates, because of the different count. And if there where two rows with the same count, one would be removed and the count accordingly too small by 50%.
You probably want this:
mmsi type l w flag count(*) 123 456 A B C 37 234 567 X Y Z 28
instead with the total count, no matter in which tables. So get a set of all records first (with UNION ALL
), then aggregate.
SELECT mmsi, type, l, w, flag, COUNT (*)
FROM
(
SELECT mmsi, type, l, w, flag FROM test_1
UNION ALL
SELECT mmsi, type, l, w, flag FROM test_2
) all_records
GROUP BY mmsi, type, l, w, flag
关于sql - 选择联合计数和组,多个表,postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51595565/