2016-04-12 58 views
1

我需要做一些SELECT ... COUNT() ... GROUP BY在Android上的SQLite數據庫的2列(月和類型)請求。我可以到以下要求:由多個colums請求結果在一條線上 - Android SQLite

select Month, Type, Count() 
from MyTable 
group by Month, Type 

我的問題是,我想使用遊標和在Android的列表視圖來顯示結果:

  • 一個單行每月
  • (固定和已知數量)類型的COUNT個數。

也許是這樣的:

select Month, Count(Type = A), Count(Type = B), Count(Type = C) 
from MyTable 
group by Month 

數據庫

Month | Type 
-------- --------- 
Feb 15 | A 
Feb 15 | B 
Dec 15 | A 
Dec 15 | A 
Dec 15 | B 
Dec 15 | C 

預期結果

Month | 'COUNT(A)' | 'COUNT(B)' | 'COUNT(C)' 
-------- -------------- --------------- ------------- 
Feb 15 | 1   | 1   | 0 
Dec 15 | 2   | 1   | 1 

那是POSS可以嗎?

回答

1
select 
month, 
sum(case when "Type" = 'A' then 1 else 0 end) as "count(a)", 
sum(case when "Type" = 'B' then 1 else 0 end) as "count(b)", 
sum(case when "Type" = 'C' then 1 else 0 end) as "count(c)" 
from 
mytable 
group by 
month 
相關問題