0
我有一個表有三列 - 一個Id
,一個外鍵和Status
。我需要找到表中的活動(值= 1)狀態的no和表中的總行數。如何在單個查詢中完成。如何從表中獲取總行數和特定值的計數?
此查詢的輸出與另一個表連接。
select FK, Count(1) as active_count, <missing> as total_count from table where status = 1;
我有一個表有三列 - 一個Id
,一個外鍵和Status
。我需要找到表中的活動(值= 1)狀態的no和表中的總行數。如何在單個查詢中完成。如何從表中獲取總行數和特定值的計數?
此查詢的輸出與另一個表連接。
select FK, Count(1) as active_count, <missing> as total_count from table where status = 1;
嘗試
select
FK,
count(1) total_count,
sum(case when status = 1 then 1 else 0 end) active_count
from table
--group by FK