rpjd
05-24-2011, 07:04 PM
I have two tables Users and Changes
Users
Username, Address, County, MemberType, Gender
Changes
Filename, Username
the query is to count the number of male and female users in various counties who make changes to certain files.
What I have so far is,
select distinct filename, county, sum(if(C.Username=U.Username and U.Gender='Male',1,0) as Male, sum(if(C.Username=U.Username and U.Gender='Female',1,0) as Female from Changes as C inner join Users as U where U.County='Durham' order by Filename
Problem is I have 1 user who has has changed file 1 twice and file 2 once, user is male.
the query result will look like this
filename Male Female County
1 1 0 Durham
2 1 0 Durham
The actual result I'm getting is
filename Male Female County
1 2 0 Durham
I don't know why is not selecting the second file. Its also counting file 1 as being changed by two users not 1.
Can anyone help with this?
Users
Username, Address, County, MemberType, Gender
Changes
Filename, Username
the query is to count the number of male and female users in various counties who make changes to certain files.
What I have so far is,
select distinct filename, county, sum(if(C.Username=U.Username and U.Gender='Male',1,0) as Male, sum(if(C.Username=U.Username and U.Gender='Female',1,0) as Female from Changes as C inner join Users as U where U.County='Durham' order by Filename
Problem is I have 1 user who has has changed file 1 twice and file 2 once, user is male.
the query result will look like this
filename Male Female County
1 1 0 Durham
2 1 0 Durham
The actual result I'm getting is
filename Male Female County
1 2 0 Durham
I don't know why is not selecting the second file. Its also counting file 1 as being changed by two users not 1.
Can anyone help with this?