MySQL或Postgres中的透视表

MySQL或Postgres中的透视表,mysql,sql,postgresql,pivot-table,Mysql,Sql,Postgresql,Pivot Table,我们如何从 到 与此相反的是:示例数据: create table test ( custname text, computer numeric, monitor numeric, software numeric); insert into test values ('Alison', 345.89, 123.45, 78.78), ('Jason', 435.34, 158.23, 243.54); 查询: select custnam

我们如何从

与此相反的是:

示例数据:

create table test (
    custname text, 
    computer numeric, 
    monitor numeric, 
    software numeric);

insert into test values
('Alison', 345.89, 123.45, 78.78),
('Jason', 435.34, 158.23, 243.54);
查询:

select 
    custname "Customer", 
    unnest(array['Computer', 'Monitor', 'Software']) "Item type",
    unnest(array[computer, monitor, software]) "Amount"
from test;

 Customer | Item type | Amount 
----------+-----------+--------
 Alison   | Computer  | 345.89
 Alison   | Monitor   | 123.45
 Alison   | Software  |  78.78
 Jason    | Computer  | 435.34
 Jason    | Monitor   | 158.23
 Jason    | Software  | 243.54
(6 rows)

如果
unnest()
不可用,您可以使用
union

select custname "Customer", 'Computer' "Item type", computer "Amount" from test
union select custname, 'Monitor', monitor from test
union select custname, 'Sofware', software from test
order by 1, 2;

谢谢你的回答,如果我在MySql或Redshift(没有最不必要的功能)中这样做,我会怎么做?太棒了!感谢Klin:)生成MySQL代码:;