假設我們有下表:
city gender
abc m
abc f
def m
所需輸出:
city f_count m_count
abc 1 1
def 0 1
請幫助我用 Hive 或 MySQL 或 SQL Server 語法撰寫查詢。我需要 Hive 語法。
謝謝:)
uj5u.com熱心網友回復:
嘗試:
select city,sum(gender='f') as f_count,sum(gender='m') as m_count
from my_table
group by city;
結果:
city f_count m_count abc 1 1 def 0 1
演示
uj5u.com熱心網友回復:
SQL 服務器:
select
city
,count(case when gender = 'm' then 1 else null end) as m_count
,count(case when gender <> 'm' then 1 else null end) as f_count
from table_name
group by city
結果:
city | f_count | m_count abc | 1 | 1 def | 0 | 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/419737.html
標籤:
上一篇:將本地mysql連接到gitlabci/cdpipeline時出錯
下一篇:使用創建日期優化日期相關查詢
