2013-03-17 81 views
0

我有項目表中採用以下格式:MySQL查詢的分組列

enter image description here

而且,我需要有MYSQL,可以給我數據,格式如下:

enter image description here基本上,我不得不根據位置對數據進行分組。然後必須計算成功和不成功的項目。 「成功」列包含PercentRaised大於或等於1的項目總數,不成功列包含的百分比數量小於1的項目總數。

我只是對mysql有基本的瞭解。需要您的建議。

回答

2
select location 
,  sum(case when PercentageRaised >= 1.0 then 1 end) as successful 
,  sum(case when PercentageRaised < 1.0 then 1 end) as unsuccessful 
from YourTable 
group by 
     location 
0

MySQL支持布爾運算。

SELECT Location, 
     SUM(percentageRaised > 0) successful, 
     SUM(percentageRaised < 0) unsuccessful, 
FROM tableName 
GROUP BY Location