我得到了下表
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 非常感谢您的帮助。
答案 0 :(得分:0)
一种方法使用横向连接然后聚合:
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;