我有一个PostgreSQL函数(或表),它给出了以下输出:
Sl.no username Designation salary etc..
1 A XYZ 10000 ...
2 B RTS 50000 ...
3 C QWE 20000 ...
4 D HGD 34343 ...
现在我想要输出如下:
Sl.no 1 2 3 4 ...
Username A B C D ...
Designation XYZ RTS QWE HGD ...
Salary 10000 50000 20000 34343 ...
怎么做?
答案 0 :(得分:24)
将我的答案基于表格的表格:
CREATE TABLE tbl (
sl_no int
, username text
, designation text
, salary int
);
每行都会返回一个新列。使用这样的动态返回类型,通过单次调用数据库几乎不可能使其完全动态化。使用两个步骤展示解决方案:
通常,这受到表可以容纳的最大列数的限制。因此,对于超过1600行(或更少)的表,这不是一个选项。详细说明:
crosstab()
SELECT 'SELECT *
FROM crosstab(
''SELECT unnest(''' || quote_literal(array_agg(attname))
|| '''::text[]) AS col
, row_number() OVER ()
, unnest(ARRAY[' || string_agg(quote_ident(attname)
|| '::text', ',') || ']) AS val
FROM ' || attrelid::regclass || '
ORDER BY generate_series(1,' || count(*) || '), 2''
) t (col text, '
|| (SELECT string_agg('r'|| rn ||' text', ',')
FROM (SELECT row_number() OVER () AS rn FROM tbl) t)
|| ')' AS sql
FROM pg_attribute
WHERE attrelid = 'tbl'::regclass
AND attnum > 0
AND NOT attisdropped
GROUP BY attrelid;
可以包含在具有单个参数的函数中...
生成表单的查询:
SELECT *
FROM crosstab(
'SELECT unnest(''{sl_no,username,designation,salary}''::text[]) AS col
, row_number() OVER ()
, unnest(ARRAY[sl_no::text,username::text,designation::text,salary::text]) AS val
FROM tbl
ORDER BY generate_series(1,4), 2'
) t (col text, r1 text,r2 text,r3 text,r4 text)
产生所需的结果:
col r1 r2 r3 r4
-----------------------------------
sl_no 1 2 3 4
username A B C D
designation XYZ RTS QWE HGD
salary 10000 50000 20000 34343
unnest()
SELECT 'SELECT unnest(''{sl_no, username, designation, salary}''::text[] AS col)
, ' || string_agg('unnest('
|| quote_literal(ARRAY[sl_no::text, username::text, designation::text, salary::text])
|| '::text[]) AS row' || sl_no, E'\n , ') AS sql
FROM tbl;
生成表单的查询:
SELECT unnest('{sl_no, username, designation, salary}'::text[]) AS col
, unnest('{10,Joe,Music,1234}'::text[]) AS row1
, unnest('{11,Bob,Movie,2345}'::text[]) AS row2
, unnest('{12,Dave,Theatre,2356}'::text[]) AS row3
, unnest('{4,D,HGD,34343}'::text[]) AS row4
同样的结果。
crosstab()
如果可以,请使用此功能。打败其他人。
SELECT 'SELECT *
FROM crosstab(
$ct$SELECT u.attnum, t.rn, u.val
FROM (SELECT row_number() OVER () AS rn, * FROM '
|| attrelid::regclass || ') t
, unnest(ARRAY[' || string_agg(quote_ident(attname)
|| '::text', ',') || '])
WITH ORDINALITY u(val, attnum)
ORDER BY 1, 2$ct$
) t (attnum bigint, '
|| (SELECT string_agg('r'|| rn ||' text', ', ')
FROM (SELECT row_number() OVER () AS rn FROM tbl) t)
|| ')' AS sql
FROM pg_attribute
WHERE attrelid = 'tbl'::regclass
AND attnum > 0
AND NOT attisdropped
GROUP BY attrelid;
使用attnum
而不是实际的列名进行操作。更简单,更快捷。再次将结果加入pg_attribute
或集成列名,如第9.3页示例所示
生成表单的查询:
SELECT *
FROM crosstab(
$ct$SELECT u.attnum, t.rn, u.val
FROM (SELECT row_number() OVER () AS rn, * FROM tbl) t
, unnest(ARRAY[sl_no::text,username::text,designation::text,salary::text])
WITH ORDINALITY u(val, attnum)
ORDER BY 1, 2$ct$
) t (attnum bigint, r1 text, r2 text, r3 text, r4 text);
这使用了一系列高级功能。太难解释了。
unnest()
一个unnest()
现在可以并行取消多个阵列。
SELECT 'SELECT * FROM unnest(
''{sl_no, username, designation, salary}''::text[]
, ' || string_agg(quote_literal(ARRAY[sl_no::text, username::text, designation::text, salary::text])
|| '::text[]', E'\n, ')
|| E') \n AS t(col,' || string_agg('row' || sl_no, ',') || ')' AS sql
FROM tbl;
结果:
SELECT * FROM unnest(
'{sl_no, username, designation, salary}'::text[]
,'{10,Joe,Music,1234}'::text[]
,'{11,Bob,Movie,2345}'::text[]
,'{12,Dave,Theatre,2356}'::text[])
AS t(col,row1,row2,row3,row4)
SQL Fiddle在第9.3页上运行。
答案 1 :(得分:9)
SELECT
unnest(array['Sl.no', 'username', 'Designation','salary']) AS "Columns",
unnest(array[Sl.no, username, value3Count,salary]) AS "Values"
FROM view_name
ORDER BY "Columns"
答案 2 :(得分:8)
如果(像我一样)你需要来自bash脚本的这些信息,请注意psql有一个简单的命令行开关,告诉它将表列输出为行:
psql mydbname -x -A -F= -c "SELECT * FROM foo WHERE id=123"
-x
选项是让psql将列输出为行的关键。
答案 3 :(得分:1)
在纯SQL或PL / pgSQL中没有正确的方法。
在应用程序中执行此操作会更好,它可以从数据库中获取数据。
答案 4 :(得分:1)
我有一个比上述Erwin更为简单的方法,那就是Postgres的工作人员(我认为它应该与支持SQL标准的所有主要关系数据库一起工作)
您可以仅使用UNION代替交叉表:
SELECT text 'a' AS "text" UNION SELECT 'b';
text
------
a
b
(2 rows)
当然,这取决于您要应用的情况。考虑到您事先知道需要什么字段,即使查询不同的表也可以采用这种方法。即:
SELECT 'My first metric' as name, count(*) as total from first_table UNION
SELECT 'My second metric' as name, count(*) as total from second_table
name | Total
------------------|--------
My first metric | 10
My second metric | 20
(2 rows)
这是一种更易于维护的方法,恕我直言。请查看此页面以获取更多信息:https://www.postgresql.org/docs/current/typeconv-union-case.html