0

我在 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

我正在尝试根据某些条件形成以下报告。报告应如下所示。我尝试了以下查询,但 count(*) 没有按预期工作

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

我尝试了以下查询,但我无法根据男性人数和女性人数进行计数..

    select State, "NO_OF MALES", "$AVG_sal", "NO_OF_FEMALES", "$AVG_SAL_FEMALE"
    from(
    select State,
    to_char(SUM((CASE WHEN gender = 'M' THEN average_price ELSE 0 END)),'$999,999,999') as "$Avg_sal_men,
     to_char(SUM((CASE WHEN gender = 'F' THEN average_price ELSE 0 END)), '$999,999,999') as "$Avg_sal_women,
     (select count (*) from table where gender='M')"NO_OF MALES",
    (select count (*) from table where gender='F')"NO_OF_FEMALES"
     from table group by State order by state);
4

3 回答 3

1

您可以case用作表达式(您已经知道...)。并且子查询是不必要的。

select State
     , sum(case gender when 'M' then 1 else 0 end) as "no.of males"
     , sum(case gender when 'F' then 1 else 0 end) as "no.of females"
     , to_char(
           SUM(
               (
                   CASE
                   WHEN gender = 'M' THEN average_price
                   ELSE 0
                   END
               )
           )
         , '$999,999,999'
       ) as "Avg_sal_men",
       to_char(SUM((CASE WHEN gender = 'F' THEN average_price ELSE 0 END))
              ,'$999,999,999'
     ) as "Avg_sal_women"
from table
group by State;
于 2013-05-14T10:40:23.793 回答
0

你正在通过这个子查询select count (*) from table where gender='M'来计算,它总是计算你整个表中的男性总数......你在计算女性时也是如此......所以你可以这样尝试......

select State, "NO_OF MALES", "$AVG_sal", "NO_OF_FEMALES", "$AVG_SAL_FEMALE"
        from(
        select State,
        to_char(SUM((CASE WHEN gender = 'M' THEN average_price ELSE 0 END)),'$999,999,999') as "$Avg_sal_men",
         to_char(SUM((CASE WHEN gender = 'F' THEN average_price ELSE 0 END)), '$999,999,999') as "$Avg_sal_women,
        Sum(Case when gender='M' then 1 else 0 end) "NO_OF MALES",
         Sum(Case when gender='F' then 1 else 0 end) "NO_OF_FEMALES"
         from table group by State order by state);
于 2013-05-14T10:35:15.280 回答
0

试试下面的。

select state
      ,sum(case when gender = 'M' then 1 else 0 end)      as nof_males
      ,sum(case when gender = 'F' then 1 else 0 end)      as nof_females
      ,avg(case when gender = 'M' then average_price end) as avg_sal_male
      ,avg(case when gender = 'F' then average_price end) as avg_sal_female
  from table
 group 
    by state;

..根据需要添加格式。

于 2013-05-14T10:58:41.897 回答