2

i need a report from a database where i need the final result like Number of Male, Number of Female, showing against city and finally against State. I started off with something like.

SELECT     p.StateName,  d.CityName,
 count(api.Gender) as Gender
FROM       dbo.Application_Personal_information as api INNER JOIN
           dbo.state as p ON api.State = p.ID INNER JOIN
           dbo.City as d ON api.City= d.ID
           group by p.StateName, d.CityName

when i do this

   SELECT     p.StateName,  d.CityName,
     count(api.Gender = 'Male) as Male,
     count(api.Gender = 'Female) as Female,
    FROM       dbo.Application_Personal_information as api INNER JOIN
               dbo.state as p ON api.State = p.ID INNER JOIN
               dbo.City as d ON api.City= d.ID
               group by p.StateName, d.CityName

it give's me error. incorrect syntax near =. i also tried with select statement

 COUNT(select api.Gender from api where api.Gender ='Male') as Male,

But it is also not working. ... Any idea?

1
  • See answer below: if you need the output to be exactly as you showed it, it's slightly more tricky. Commented Jul 12, 2012 at 7:32

2 Answers 2

4
SELECT     
    p.StateName,  d.CityName, 
    sum(case when Gender ='Male' then 1 else 0 end ) as Male_count,
    sum(case when Gender ='Female' then 1 else 0 end ) as Female_count

FROM
    dbo.Application_Personal_information as api INNER JOIN 
    dbo.state as p ON api.State = p.ID INNER JOIN 
    dbo.City as d ON api.City= d.ID 
group by 
    p.StateName, d.CityName
Sign up to request clarification or add additional context in comments.

5 Comments

This works too - but I suspect it will be much slower - speed may not be an issue here though.
SUM(Gender ='Male') works too in MySQL (because TRUE is 1 and FALSE is 0)
Thats good ypercube. But it is more generic to MySQL and sometimes may be confuse you :)
Yes, your query is more db-agnostic. ( I really thought this was tagged with MySQL, was it?). I prefer COUNT() over SUM() but I don't think there is any performance difference: COUNT(CASE WHEN Gender ='Male' THEN 1 ELSE NULL END)
But won't you get warning about NULL values when you use COUNT over SUM?
4

You could try the PIVOT function if you are using SQL Server 2005 or later:

WITH CTE AS
(   SELECT  p.StateName,  
            d.CityName,
            api.Gender
    FROM    dbo.Application_Personal_information as api 
            INNER JOIN dbo.state as p 
                ON api.State = p.ID 
            INNER JOIN dbo.City as d 
                ON api.City= d.ID
)
SELECT  *
FROM    CTE
        PIVOT
        (   COUNT(Gender)
            FOR Gender IN ([Male], [Female])
        ) pvt

1 Comment

PIVOT was introduced in version 2005 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.