将多行项目分组为单行

时间:2016-11-23 07:39:05

标签: sql postgresql pivot

有可能这样吗?

datetime           | sysname | resource   | metric      | value 
-------------------+---------+------------+-------------+-------
Nov 23,2016 2:00   |     A   |     A-2    | Utilization | 0.30
Nov 23,2016 2:00   |     A   |     A-2    | Throughput  | 300 
Nov 23,2016 2:00   |     B   |     B-2    | Utilization | 10.8
Nov 23,2016 2:00   |     B   |     B-2    | Throughput  | 1080

预期产出:

datetime          | sysname | resource | Utilization | Throughput 
------------------+---------+----------+-------------+-------
Nov 23,2016 2:00  | A       |  A-2     |  0.3        |  300
Nov 23,2016 2:00  | B       |  B-2     | 10.8        | 1080   

1 个答案:

答案 0 :(得分:1)

SELECT datetime, sysname, resource, a.value, b.value
FROM metrics a
     JOIN metrics b
        USING (datetime, sysname, resource)
WHERE a.metric = 'Utilization' AND b.metric = 'Throughput';