2017-08-08 132 views
0

任何人都可以給出建議,因爲這是否可能在postgresql中。將水平表轉換爲垂直Postgres

我有一個查詢,像這樣:

SELECT a.count as "A", b.count as "B" FROM 
(SELECT count(*) FROM a) as a, 
(SELECT COUNT(*) FROM b) as b; 

這讓我像一個結果如下:

A | B 
----------- 
100 | 300 

是否有可能改變這個要顯示這樣的:

Type | Count 
---------------- 
A  | 100 
B  | 300 

任何建議都會很棒。

回答

1

看一看的UNION/UNION ALL關鍵字:

SELECT 'A', count(*) 
FROM a 
UNION ALL 
SELECT 'B', COUNT(*) 
FROM b; 
+0

YES!作品。方位! – outrunthewolf