我有一个包含大约200个属性的表。对于每个属性,我想知道所有可能的值。大多数属性都有2到20个可能的值。
如果属性可能有太多不同的值(例如邮政编码),那么我只想知道计数。 (请注意,这不需要是一个独特的计数,我更多地将计数步骤作为占位符,因此我保持源表中的属性顺序比其他任何东西。)
我想保存此输出到csv文件,但如果需要为单个属性保存多个值,则可以接受不同的输出文件类型。
对我要实现的目标的缩短和简化查询是:
COPY(
SELECT
(SELECT COUNT(id) FROM complete_file_301115) AS id,
(SELECT DISTINCT (title) FROM complete_file_301115) AS title,
(SELECT COUNT(firstname) FROM complete_file_301115) AS firstname,
(SELECT COUNT(lastname) FROM complete_file_301115) AS lastname,
(SELECT COUNT(address1) FROM complete_file_301115) AS address1,
(SELECT COUNT(address2) FROM complete_file_301115) AS address2,
(SELECT DISTINCT (city) FROM complete_file_301115) AS city,
(SELECT DISTINCT (county) FROM complete_file_301115) AS county,
(SELECT COUNT(postcode) FROM complete_file_301115) AS postcode,
(SELECT DISTINCT (gender) FROM complete_file_301115) AS gender,
(SELECT DISTINCT (job) FROM complete_file_301115) AS job,
(SELECT DISTINCT (children) FROM complete_file_301115) AS children) TO
'/Users/Shared/test123.csv' (FORMAT CSV, DELIMITER ',', HEADER, ENCODING LATIN1);
当我运行查询时,Postgres给了我错误:
ERROR: more than one row returned by a subquery used as an expression
我知道这个错误基本上是说我不能为csv中的单个属性写多个值。
我的问题是,无论如何都要保存一个输出文件,其中包含每个属性的所有可能值,例如
id:1,2,3,4
标题:先生,夫人,小姐,女士,博士等。
非常感谢提前
答案 0 :(得分:1)
您可以将其转换为文字。它适合你吗?
...
(array_to_string( array(SELECT DISTINCT (city) FROM complete_file_301115), ',') ) AS city,
...