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
Querying Postgresql Table Statistics
-
22-06-2023 - |
Question
Right now I have a table that contains meta information about access to certain other tables in my database. It is populated using triggers that fire on certain types of actions on certain tables. This data is then recorded into a table with the following fields;
table_name, operation_type, operation_date
I've been trying to write a query that will let me get the following view of the information
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
I was attempting to use two unions to get this result set
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
Though my use of unions is off aparently as well as my requirements for using group by.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow