简体   繁体   中英

how to get distinct values and their count for each column in postgresql

I think my question is the same than this one , but it wasn't clear so the responses are not what I'm looking for.

I have a table built by osm2pgsql of 70 columns and about a million rows. Of the 70 columns, only 2 have values for each row (one is a geolocation with an index on it), the remaining ones form a sparse matrix.

I put in this fiddle a simplified version of what I have and what I want. A little explanation here : each value of each column got its count, and each column also got its count, with a NULL value to indicate that it's the column count.

I came with a partial response :

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;

Thanks to that thread . I still have to get the correct values when value_name is NULL, but I'm very close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM