我來了部分響應:
SELECT string_agg(
'SELECT ' ||
'''' || quote_ident(attname) || ''' as column_name, ' ||
quote_ident(attname) || '::text as value_name, ' ||
' count(*) as value_count' ||
' FROM ' || attrelid::regclass ||
-- ' WHERE ' || quote_ident(attname) || ' is not NULL' ||
' GROUP BY ' || quote_ident(attname) || ' ',
'UNION ALL ')
FROM pg_attribute
WHERE attrelid = '<my_table_name>'::regclass
AND attnum >= 1 -- exclude tableoid & friends (neg. attnum)
AND attisdropped is FALSE -- exclude deleted columns
GROUP BY attrelid;
感謝that thread。 當value_name爲NULL時,我仍然必須得到正確的值,但我非常接近。
給定SQL FIDDLE數據的期望結果是什麼? – java
存儲在expected_result中的一個! – Borbag
@sagi得到了什麼是想要的結果,看到他的反應 – Borbag