lirong2008wf 2011-11-04 11:14
浏览 238
已采纳

请问这样的SQL语句怎么样优化呢?查询时间好久啊

[code="java"]select f.pc_name,
f.phc_name,
(select count(1)
from p_resident c
where c.phc_id = f.phc_id
and c.pr_income is not null

       and c.pr_income < 4000) fourqian,
   (select count(1)
      from p_resident c
     where c.phc_id = f.phc_id
       and c.pr_income is not null

       and c.pr_income >= 4000
       and c.pr_income < 5000) fourfiveqian,
   (select count(1)
      from p_resident c
     where c.phc_id = f.phc_id
       and c.pr_income is not null

       and c.pr_income >= 5000
       and c.pr_income < 6000) fivesixqian,
   (select count(1)
      from p_resident c
     where c.phc_id = f.phc_id
       and c.pr_income is not null

       and c.pr_income >= 6000
       and c.pr_income < 7000) sixsevenqian,
   (select count(1)
      from p_resident c
     where c.phc_id = f.phc_id
       and c.pr_income is not null

       and c.pr_income >= 7000) sevenqian

from (select t.pc_name, a.phc_name, a.phc_id
from p_community t, p_home_community a
where t.pc_id = a.pc_id
and t.pc_id in
(select c.po_id
from p_organization c
start with c.po_id = 5027
connect by prior c.po_id = c.po_parent_id)) f
[/code]

按地区统计各个阶段收入的个数,查询要好长时间

  • 写回答

1条回答 默认 最新

  • iteye_8576 2011-11-04 12:17
    关注

    [code="sql"]
    select t.pc_name,
    a.phc_name,
    c.fourqian,
    c.fourfiveqian,
    c.fivesixqian,
    c.sixsevenqian,
    c.sevenqian
    from p_community t, p_home_community a,
    (
    select c.phc_id,
    sum(case when c.pr_income < 4000 then 1 else 0 end) fourqian,
    sum(case when c.pr_income >= 4000 and c.pr_income < 5000 then 1 else 0 end) fourfiveqian,
    sum(case when c.pr_income >= 5000 and c.pr_income < 6000 then 1 else 0 end) fivesixqian,
    sum(case when c.pr_income >= 6000 and c.pr_income < 7000 then 1 else 0 end) sixsevenqian,
    sum(case when c.pr_income >= 7000 then 1 else 0 end) sevenqian
    from p_resident c
    where c.pr_income is not null
    group by c.phc_id
    ) c
    where t.pc_id = a.pc_id
    and a.pc_id = c.pc_id
    and t.pc_id in
    (select c.po_id
    from p_organization c
    start with c.po_id = 5027
    connect by prior c.po_id = c.po_parent_id)
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论