-3

我在 oracle 10g 中有下表(company_details)。

STATE       COMPANY_NAME        AVG_SAL
 KN             GOO               $5500
 IN             SCOPE             $6000
 AR             FB                $8250
 CL             PAL               $4500
 CL             GOO               $6000
 AR             GOO               $7000
 IN             FB                $3999

我想形成如下所示的输出并将其显示在简单的 GWT 数据网格中。

STATE  count(goo)  AVG_SAL  count(SCOPE)  AVG_SAL  count(FB) AVG_SAL count(PAL)  AVG_SAL
KN        1          5500        --            --          --     --        --         --
IN       --           --          1           6000        1        3999     --         --
AR        1          7000         --            --         1       8250      --        --
CL        1          6000        --             --       --          --       1      4500

我尝试编写以下查询,但没有成功。请帮助

select
    state,
    count(*)as "GOO_Records", to_char(SUM(average_sal),'$999,999') as "GOO_AVGSAL"
    from company_details 
where company_status = 'OPEN' and COMPANY_NAME = 'GOO' group by state order by state
union 
select
    state,
    count(*)as "FB_Records", to_char(SUM(average_sal),'$999,999') as "FB_AVGSAL"
    from company_details 
where company_status = 'OPEN' and COMPANY_NAME = 'FB' group by state order by state

任何帮助表示赞赏。提前致谢!!!!

4

1 回答 1

2

尝试这个:

SELECT
  state,
  GooCount,
  GooCount   / Total AS AVG_GOO,
  sCOPECount,
  SCOPECount / Total AS AVG_SCOPE,
  FBCOunt, 
  FBCount    / Total AS AVG_FB,
  PALCount,
  PALCount   / Total AS AVG_PAL
FROM
(
    select
      state,
      SUM(CASE WHEN COMPANY_NAME = 'GOO'   THEN 1 ELSE 0 END) AS GooCount, 
      SUM(CASE WHEN COMPANY_NAME = 'SCOPE' THEN 1 ELSE 0 END) AS SCOPECount, 
      SUM(CASE WHEN COMPANY_NAME = 'FB'    THEN 1 ELSE 0 END) AS FBCount, 
      SUM(CASE WHEN COMPANY_NAME = 'PAL'   THEN 1 ELSE 0 END) AS PALCount,    
      COUNT(*) AS total
    from company_details 
    where company_status = 'OPEN'
    group by state order by state
) AS sub;
于 2013-05-06T10:54:14.430 回答