2014-03-26 2 views
0

지금 당장 데이터베이스에있는 다른 테이블에 대한 액세스에 대한 메타 정보가 들어있는 테이블이 있습니다. 특정 테이블에서 특정 유형의 조치를 트리거하는 트리거를 사용하여 데이터를 채 웁니다. 이 데이터는 다음 필드가있는 테이블에 기록됩니다.PostgreSQL 테이블 통계 쿼리

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 

나는이를 얻기 위해 두 조합을 사용하려고 시도했습니다 노동 조합의 내 사용이 꺼져 aparently뿐만 아니라 그룹화를 사용하여 내 요구하지만 결과는

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 

을 설정합니다.

답변

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