查询Postgresql表统计信息

时间:2014-03-26 18:28:25

标签: postgresql union

现在我有一个表,其中包含有关访问数据库中某些其他表的元信息。它使用触发器填充,这些触发器触发某些表上的某些类型的操作。然后将该数据记录到具有以下字段的表中;

table_name,operation_type,operation_date

我一直在尝试编写一个查询,让我可以获得以下信息视图

table_name | operation_type | count of the operations
posts      | delete         | 20
posts      | update         | 30
posts      | insert         | 25
pictures   | delete         | 20
pictures   | update         | 30
pictures   | insert         | 25

我试图使用两个联盟来获得此结果集

SELECT COUNT(operation_type), table_name FROM db_stats.db_stats_table_call WHERE operation_type = 'DELETE' UNION 
SELECT COUNT(operation_type), table_name FROM db_stats.db_stats_table_call WHERE operation_type = 'INSERT' UNION
SELECT COUNT(operation_type), table_name FROM db_stats.db_stats_table_call WHERE operation_type = 'UPDATE' UNION

虽然我对工会的使用显然不符合我对使用group by的要求。

1 个答案:

答案 0 :(得分:0)

select table_name, operation_type, count(*)
from db_stats.db_stats_table_call
group by table_name, operation_type
order by table_name, operation_type