Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将行转换为列postgreSQL_Sql_Postgresql_Rows_Crosstab - Fatal编程技术网

将行转换为列postgreSQL

将行转换为列postgreSQL,sql,postgresql,rows,crosstab,Sql,Postgresql,Rows,Crosstab,我得到了下表 SHOPPER_TYPE | total_online_shopper | total_online_spent | total_online_visits | total_online_units | total_online_discout -------------+----------------------+--------------------+---------------------+--------------------+-------------------

我得到了下表

SHOPPER_TYPE | total_online_shopper | total_online_spent | total_online_visits | total_online_units | total_online_discout
-------------+----------------------+--------------------+---------------------+--------------------+---------------------
CM           |                   11 |                 22 |                  44 |                 55 |                  443
NM           |                   66 |                667 |                  22 |                 33 |                  448
我想得到以下结果

Measures               |  CM |  NM
-----------------------+-----+----
total_online_shopper   |  11 |  66
total_online_spent     |  22 | 667
total_online_visits    |  44 |  22
total_online_units     |  55 |  33
total_online_discout   | 443 | 448
正如我在标题中提到的,我在pgadmin 9.3上使用PostgrSQL
非常感谢您的帮助。

有一种方法使用横向连接,然后使用聚合:

select v.measure,
       max(case when t.SHOPPER_TYPE = 'CM' then v.val end) as cm,
       max(case when t.SHOPPER_TYPE = 'NM' then v.val end) as nm
from t, lateral
     (values ('total_online_shopper',  t.total_online_shopper), 
             ('total_online_spent', t.total_online_spent),
             ('total_online_visits', t.total_online_visits),
             ('total_online_units', t.total_online_units),
             ('total_online_discount', t.total_online_discount)
     ) v(measure, val)
group by v.measure;

你的数据格式没有意义。现在还不清楚你想要完成什么。或者,它可能被称为交叉表pgsql@LJ01我试着用crosstab解决这个问题,但没有成功。如果您能再详细一点,我们将不胜感激