在此處輸入影像描述我正在嘗試撰寫 SQL 查詢以獲取以下輸出。
表有以下資料:
ID GENDER
10 M
10 F
10 F
20 F
20 M
輸出:
ID Male Female
10 1 2
20 1 1
我需要用 group by 用例嗎?有人可以在這里幫忙。
uj5u.com熱心網友回復:
使用簡單組
select id,sum(case when GENDER = 'M' then 1 else 0 end) as Male,
sum(case when GENDER = 'F' then 1 else 0 end) as FeMale
from tablename
group by id
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407731.html
標籤:
上一篇:如何計算蜂巢中的行數?
