Atul
Atul

Reputation: 1774

MYSQL Query for Grouping Column Based on Different Conditions

I have project table in following format:

enter image description here

And, I need to have MYSQL that can give me data in following format:

enter image description here

Basically, I have to group the data based on location. Then have to count the successful and unsuccessful projects. "Successful" column has total count of projects for which percentageRaised is more than or equal to 1 and Unsuccessful column has total count of project for which percetageRaised in less than 1.

I just have basic understanding of mysql. Need your advise.

Upvotes: 0

Views: 234

Answers (2)

John Woo
John Woo

Reputation: 263933

MySQL Supports boolean arithmetic.

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

Upvotes: 0

Andomar
Andomar

Reputation: 238296

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

Upvotes: 2

Related Questions