I have user
table like as follows
user_id name gender age
--------------------------------
1 AAA Male 45
2 BBB Female 22
3 CCC Male 47
.................................
..............................
I want to get total no of users and total no of male users ans percent for male and female users
select count(*) as total , SUM(IF(v.gender = 'Male',1,0)) as totalMale ,
SUM(IF(v.gender = 'Female',1,0)) as totalFemale , totalMale/total *100 ,
totalFeMale/total *100 from user;
This query is not working when using aliases to calculate male and female percent.
I am getting the error like unknown columns..........
select count(*) as total , SUM(IF(v.gender = 'Male',1,0)) as totalMale ,
SUM(IF(v.gender = 'Female',1,0)) as totalFemale , SUM(IF(v.gender = 'Male',1,0))/count(*) *100 ,SUM(IF(v.gender = 'Female',1,0))/count(*) *100 from user;
But this is working.
But is this i used SUM(IF(v.gender = 'Female',1,0))
2 times.I think it will degrade the performance.
Can't i use aliases in my situation ?
Thanks in advance...