简单GWT中的动态SQL查询和输出

时间:2013-05-06 10:45:43

标签: java sql gwt

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

感谢任何帮助。提前致谢!!!!

1 个答案:

答案 0 :(得分: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;