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]

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集