psql --no-align --field-separator 'TtTt' --pset footer -A -X -c "select query_start, usename, application_name, since_state_change, 'https://ia64.org/~dddh/rmis/' || full_query_md5 || '.sql' as \"sql\", case when since_state_change >= interval '10 minutes' then '10 minutes' end as \"10 minutes\", count(*) over(partition by full_query_md5) cnt from dw.dw_view where (snapshotted_at >= '2022-01-01 00:00:00'::timestamp and snapshotted_at < '2022-04-01 00:00:00') and since_state_change > interval '5 minutes' and application_name not ilike 'dba%' and usename <> 'postgres' order by id;"|sed -e 's/TtTt/\t/g' > /tmp/p01.xls

mkdir -pv /tmp/list;for i in $(psql -t -A -X -c "select distinct full_query_md5 from dw.dw_view where (snapshotted_at >= '2022-01-01 00:00:00'::timestamp and snapshotted_at < '2022-04-01 00:00:00') and since_state_change > interval '5 minutes' and application_name not ilike 'dba%' and usename <> 'postgres';");do psql -t -A -X -c "select full_query from dw.dw_queries where full_query_md5 = '${i}';" > /tmp/list/${i}.sql;done;cd /tmp;tar -cf p01list.tar list
