您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關利用Postgresql怎么動態統計某一列的值,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
select to_char(log.date, 'yyyy-MM-dd HH24') as hour, log.exten, sum(case log.grade when '1' then 1 else 0 end) as "1", sum(case log.grade when '2' then 1 else 0 end) as "2", sum(case log.grade when '3' then 1 else 0 end) as "3", sum(case log.grade when '4' then 1 else 0 end) as "4", sum(case log.grade when '5' then 1 else 0 end) as "5", log.direction from iface_satisfaction_investigation as log where log.date >= '2017-08-03 00:00:00' and log.date < '2017-08-04 00:00:00' group by hour,log.exten,log.direction order by hour,log.exten,log.direction asc
to_char:用于查詢時間格式化,to_char(log.date, 'yyyy-MM-dd HH24'),大致的結果是:2017-08-03 13
sum():毫無疑問是用來計算總和的。
sum(case log.grade when '1' then 1 else 0 end)
是計算什么呢?
他的意思就是:
計算grade這個列的值為1的時候有多少行,后面的sum(……)就類推了。
其他的也沒有什么好講的了
補充:PostgreSQL常用的統計信息
我就廢話不多說了,大家還是直接看代碼吧~
/*計算表的空間大小*/ select oid,table_schema as "模式", table_name as "表名", row_estimate::bigint as "表中的行數(估計值)", pg_size_pretty(total_bytes) as "總大小", pg_size_pretty(table_bytes) as "表大小", pg_size_pretty(index_bytes) as "索引大小", pg_size_pretty(toast_bytes) as "toast表總大小" from ( select *, total_bytes-index_bytes-coalesce(toast_bytes,0) as table_bytes from ( select c.oid, nspname as table_schema, relname as table_name, c.reltuples as row_estimate, pg_total_relation_size(c.oid) as total_bytes, pg_indexes_size(c.oid) as index_bytes, pg_total_relation_size(reltoastrelid) as toast_bytes from pg_class c left join pg_namespace n on n.oid = c.relnamespace where relkind = 'r' ) t1 ) t2 order by 2,3; /*統計用戶表信息*/ select schemaname as "模式", relname as "表名", seq_scan as "順序掃描的次數", seq_tup_read as "順序掃描獲取活動行的數量", idx_scan as "索引掃描次數", idx_tup_fetch as "索引掃描獲取活動行的數量", n_tup_ins as "累計插入的行數", n_tup_upd as "累計更新的行數(包含HOT 更新的行)", n_tup_del as "累計刪除的行數", n_live_tup as "當前活動行估計數量", n_dead_tup as "當前死亡行的估計數量", n_mod_since_analyze as "最后一次分析后被修改的行估計數量", last_vacuum as "上次被手動清理的時間(不統計VACUUM FULL)", last_autovacuum as "上次自動清理的時間", last_analyze as "上次手動分析的時間", last_autoanalyze as "上次自動清理分析的時間", vacuum_count as "手動清理的次數", autovacuum_count as "自動清理的次數", analyze_count as "手動分析的次數", autoanalyze_count as "自動分析的次數", pg_size_pretty(pg_table_size(relid)) as "表大小(不包含索引)" from pg_stat_user_tables order by 1; /*統計用戶表IO信息*/ select schemaname as "模式", relname as "表名", heap_blks_read as "讀取的磁盤塊數量", heap_blks_hit as "緩沖區命中數量", idx_blks_read as "表上所有索引讀取的磁盤塊數", idx_blks_hit as "表上的所有索引緩沖區命中數量", toast_blks_read as "TOAST表(如果有)讀取的磁盤塊數", toast_blks_hit as "TOAST表(如果有)緩沖區命中數量", tidx_blks_read as "TOAST表索引(如果有)讀取的磁盤塊數", tidx_blks_hit as "TOAST表索引(如果有)緩沖區命中數量" from pg_statio_user_tables order by 1; /*統計用戶索引信息*/ select indexrelid, schemaname as "模式", relname as "索引所在的表名稱", indexrelname as "索引名稱", idx_scan as "索引掃描次數", idx_tup_read as "索引掃描返回的索引項數量", idx_tup_fetch as "簡單索引掃描獲取的活動行數量", pg_size_pretty(pg_relation_size(indexrelid)) as "索引大小" from pg_stat_user_indexes order by 1,2; /*追蹤函數,需要打開track_functions參數(默認關閉)*/ select * from pg_stat_user_functions;
上述就是小編為大家分享的利用Postgresql怎么動態統計某一列的值了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。