select dn,
           node,
           (select sum(a.clear_during)
              from al_history a
             where a.org_no = 65396
               and a.dn = t.dn
               and a.node = t.node) as Mains,
           (select sum(b.clear_during)
              from al_history b
             where b.org_no = 65397
               and b.dn = t.dn
               and b.node = t.node) as DG,
           (select sum(c.clear_during)
              from al_history c
             where c.org_no = 65398
               and c.dn = t.dn
               and c.node = t.node) as Battery
      from al_history t
     where (org_no = 65396)
        or (org_no = 65397)
        or (org_no = 65398)
     group by t.dn, t.node

解决方案 »

  1.   

    同一张表啊
    不用这么复杂
    用decode+sum就可以实现
      

  2.   

    hebo2005能说的详细点吗,我刚刚在dn和node列上建了索引,查询速度快了很多啊,这个查询是走的存储过程
      

  3.   

    数据库有4万多条数据,未建索引时,需要2分钟才能查询出结果,建立索引后,现在只用4秒钟就可以了,我想问一下是否还能优化一下上面的sql语句
      

  4.   

    SELECT DN, NODE, SUM(DECODE(ORG_NO, 65396, CLEAR_DURING)) MAINS,
           SUM(DECODE(ORG_NO, 65397, CLEAR_DURING)) DG,
           SUM(DECODE(ORG_NO, 65398, CLEAR_DURING)) BATTERY,
      FROM AL_HISTORY
     WHERE ORG_NO IN (65396, 65397, 65398)
     GROUP BY T.DN, T.NODE;