再會!
我想加入會導致以下結果的表:
------- ----------- --------
| area | confirmed | active |
------- ----------- --------
| area1 | 2 | 0 |
------- ----------- --------
| area2 | 1 | 1 |
------- ----------- --------
| area3 | 0 | 0 |
------- ----------- --------
從包含此的單個表中:
---- ------- -----------
| id | area | status |
---- ------- -----------
| 1 | area1 | confirmed |
---- ------- -----------
| 2 | area1 | confirmed |
---- ------- -----------
| 3 | area2 | active |
---- ------- -----------
| 4 | area2 | confirmed |
---- ------- -----------
| 5 | area3 | inactive |
---- ------- -----------
我嘗試過的是完全連接各自的“選擇”以充當要在 FROM 子句中使用的表,但這不是我想要的結果。我不知道這是否可能,或者我應該在使用子選擇獲取資料時操作資料。
有誰有想法嗎?謝謝!
uj5u.com熱心網友回復:
這是條件聚合的一個簡單案例,您可以使用FILTERPostgreSQL 中的子句
SELECT
area,
COUNT(*) FILTER (WHERE status = 'confirmed') AS confirmed,
COUNT(*) FILTER (WHERE status = 'active' ) AS active
FROM YourTable
GROUP BY area
在其他 RDBMS 中,您可以FILTER使用COUNT(CASE WHEN
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337122.html
標籤:sql PostgreSQL
上一篇:如何優化我們使用事務板的查詢?
