2016-11-23 99 views
0

是否有可能得到這樣的結果?將多行項目分組爲單行

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
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'; 
+0

感謝Laurenz ALBE。如果我有更多的指標來完成相同的連接,該怎麼辦? – Samila

+0

然後,您必須更改您的SQL語句以對其進行修改 - 基本上,爲每個表添加另一個表副本。 –