日期:2014-05-17 浏览次数:20995 次
select t1.name,t1.id,count(t2.type) from news t1 right join zbhqw t2 on t1.type = t2.id group by t1.name,t1.id;
------解决方案--------------------
select count(1), zdhqw.id, zdhqw.time
  from news
  left outer join zdhqw
    on zdhqw.id = news.type
 where zdhqw.id is not null
------解决方案--------------------
--看来我猜对了楼主的需求了
WITH T1 AS(
  SELECT CONNECT_BY_ROOT ID AS ID,NAME,PARENTID,TIME
    FROM t_zbys LEFT JOIN ZDNEWS ON ID = TYPE 
   CONNECT BY PRIOR ID = PARENTID
),T2 AS(
  SELECT ID,COUNT(TYPE) NUM
    FROM T1
   GROUP BY ID
)SELECT A.ID,A.PARENTID,A.TIME,T2.NUM
   FROM t_zbys A INNER JOIN T2 ON A.ID = T2.ID;
------解决方案--------------------
定义一个临时表和游标
create table tmp_zbys(id varchar2(100),num number);
CREATE OR REPLACE PACKAGE PKG_CONST AS
 TYPE ZRAR_CURSOR IS REF CURSOR;
END  PKG_CONST;
存储过程
[code=SQL][/code]
create or replace procedure pro_xxxx(v_cur out PKG_CONST.ZRAR_CURSOR)
is
 vid varchar2(100); --id
 num number;--数量
 cursor zbys_cursor is
   select type,count(1) num from news group by type;--获取类型
begin
 delete tmp_zbys;
 open zbys_cursor;
 loop
   fetch zbys_cursor into vid,num;
   exit when zbys_cursor%notfound;
   insert into tmp_zbys
   select id,num from zdhqw
   start with id=vid
   connect by prior parentid=id;
 end loop;
 close zbys_cursor;
 open v_cur for
 select t.name,t.id,t.time,nvl(c.num,0),num from zdhqw t,
 (select id,sum(num) num from tmp_zbys group by id) c
 where t.id=c.id(+);
commit;
end xxxx;