sql - count(*) based on the gender condition -


i have following table in oracle10g.

   state     gender   avg_sal   status     nc           m      5200     single      oh           f      3800     married     ar           m      8800     married     ar           f      6200     single     tn           m      4200     single     nc           f      4500     single 

i trying form following report based on condition. report should 1 below. tried below query count(*) not working expected

state     gender no.of males no.of females   avg_sal_men      avg_sal_women  nc           m       10            0           5200                 0  oh           f        0            5           0                  3800  ar           m        16           0           8800                  0  ar           f        0            12          0                  6200  tn           m        22            0          4200                  0  nc           f        0             8          0                   4500 

i tried following query not able count based onthe no.of males , no.of females..

    select state, "no_of males", "$avg_sal", "no_of_females", "$avg_sal_female"     from(     select state,     to_char(sum((case when gender = 'm' average_price else 0 end)),'$999,999,999') "$avg_sal_men,      to_char(sum((case when gender = 'f' average_price else 0 end)), '$999,999,999') "$avg_sal_women,      (select count (*) table gender='m')"no_of males",     (select count (*) table gender='f')"no_of_females"      table group state order state); 

you can use case expression (which know...). , subquery unnecessary.

select state      , sum(case gender when 'm' 1 else 0 end) "no.of males"      , sum(case gender when 'f' 1 else 0 end) "no.of females"      , to_char(            sum(                (                    case                    when gender = 'm' average_price                    else 0                    end                )            )          , '$999,999,999'        ) "avg_sal_men",        to_char(sum((case when gender = 'f' average_price else 0 end))               ,'$999,999,999'      ) "avg_sal_women" table group state; 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -