PostgreSQL result rows can be converted to Columns using Array function called unnest.
For example, the below query result
can be converted like below.
For example, the below query result
uid | name | mail
---------------------------------
01 | admin | admin@test.com
can be converted like below.
ColumnName | Value
---------------------------------
UID | 01
Name | admin
Mail | admin@test.com
---------- | ----------
Total Count | 1
Use this query to convert row values into columns:
--unnest to give array as rows
WITH
x AS (
SELECT *
FROM users t where uid > 0
),
y AS (
SELECT ARRAY [uid::varchar, name, mail] AS val,
ARRAY ['UID','Name','Mail'] AS item
FROM x
)
SELECT unnest(item) AS ColumnName,
unnest(val) AS Value
FROM x,y
UNION ALL
SELECT '----------'::text, '----------'::text
UNION ALL
SELECT 'Total Count'::text, count(x)::text from x
No comments:
Post a Comment