table1
number  name
511     北京
512     上海
513     广州
514     深圳
611     成都
612     重庆
...table2(这个是内容表,number对应table1的number)
number  ....
511     
512
512
查询结果
number    name    count
511       北京     1
512       上海     2
513       广州     0
514       深圳     0(成都和重庆都不要,因为他们是‘61’开头的)
1.如果可以,请给下sql代码

解决方案 »

  1.   

    select t1.name,t1.number, sum(nvl(t2.number, 0)) count from t1, t2 where t1.number=t2.number(+) and t1.number not like '61%' group by t1.name,t1.number
      

  2.   

    select t1.name,t1.number, sum(nvl(t2.number, 0)) count from t1, t2 where t1.number=t2.number(+) and t1.number not like '61%' group by t1.name,t1.number
    这个效率怎么样啊?
    因为table2(内容表)中数据量可能会有点多(200w左右的标准)
      

  3.   


    select distinct t1.number,t1.name,count(t2.number) over(partition by t1.name)
    from table1 t1,table2 t2
    where t1.number = t2.number
    瞎寫的~  沒測~
      

  4.   

    不对。
    这样只出来2个结果,
    因为在table2中没有number=513,514的数据
    但是也要统计出来
      

  5.   

    select t1.name,t1.number, sum(nvl(t2.number, 0)) count from t1, t2 where t1.number=t2.number(+) and t1.number not like '61%' group by t1.name,t1.number
    我测了下有问题,
    不是group by表达式!
      

  6.   


      換成 這個啊    暈   t1.number=t2.number(+) 
      

  7.   

    select distinct b.xzqhbh,b.xzqhmc,count(a.gxdwbh) over(partition by b.xzqhmc)
    from smsarinfo_201001 a,dy_xzqh b
    where b.xzqhbh like '51%' and a.gxdwbh=b.xzqhbh(+)
    还是不行啊
      

  8.   

    SELECT a.number, a.NAME, NVL (b.COUNT, 0) COUNT
      FROM t1 a, (SELECT   number, COUNT (*) COUNT
                      FROM t2
                  GROUP BY number) b
     WHERE a.number = b.number(+) AND a.number NOT LIKE '61%';
      

  9.   

    group by t1.name,t1.number 
    改成
    group by t1.name,t2.number 
      

  10.   

    2楼正解,如果需要一次性出这种数据,速度慢一点也是没有办法的事。
    如果对性能要求很高,可以先建立与查询结果结构相同的一张表result(number,name,count),在table2数据增减的时候,相应增减result表中count值
      

  11.   

    查询结果(因为我plsql中字段为number建不起名字,所以改成了bh(编号))
      NAME BH COUNT 
    1 北京 511 1022 
    2 上海 512 512 
    3 深圳 513 0 
    4 广州 514 0 为什么count1022?512?
    table2中只有的数据
    511   
    512
    512 
      

  12.   


    这个是正确的。但是感觉好多count,效率会不会很低啊!
      

  13.   

    改了下2楼的代码:
    select t1.name,t1.numbe, nvl(count(t2.numbe), 0) count from  t1,  t2 
    where t1.numbe=t2.numbe(+) and t1.numbe not like '61%' group by t1.name,t1.numbe
      

  14.   


    SQL> select * from tab1;       NUM NAME
    ---------- --------------------------------------------------------------------------------
           511 北京
           512 上海
           513 广州
           514 深圳
           611 成都
           612  重庆6 rows selectedSQL> select * from tab2;       NUM
    ----------
           511
           512
           512SQL> 
    SQL> select tab2.*, count(tab2.num), tab1.name
      2    from tab2, tab1
      3   where tab2.num = tab1.num(+)
      4     and (tab1.num < 610 or tab1.num > 619)
      5   group by tab2.num, tab1.name
      6  union
      7  select tab1.num, 0, tab1.name
      8    from tab1
      9   where (tab1.num < 610 or tab1.num > 619)
     10     and tab1.num not in (select tab2.num from tab2)
     11  ;       NUM COUNT(TAB2.NUM) NAME
    ---------- --------------- --------------------------------------------------------------------------------
           511               1 北京
           512               2 上海
           513               0 广州
           514               0 深圳SQL> 
      

  15.   


    --只需要一个子查询就OK
    select number,name ,(select count(1) from table2 b where a.number=b.number) as count
    from table 1 a
      

  16.   

    select t1.name,t1.numbe, nvl(count(t2.numbe), 0) count from  t1,  t2 
    where t1.numbe=t2.numbe(+) and t1.numbe not like '61%' group by t1.name,t1.numbe
      

  17.   


    create table table1 
    (
    num int
    ,
    vname varchar2(10))create table table2
    (
    num int
    )
    insert into table1 select 511,    '北京' from dual ;
    insert into table1 select 512   , '上海'  from dual ;
    insert into table1 select 513  ,  '广州'  from dual ;
    insert into table1 select 514  ,  '深圳'  from dual ;
    insert into table1 select 611 ,   '成都'  from dual ;
    insert into table1 select 612    ,'重庆'  from dual ;insert into table2 select 511 from dual union all select 512 from dual union all select 512 from dual;
    --哥,别用关键字来做字段名好不--查询如下
    select distinct table1.num,
           table1.vname,
           count(table2.num) over(partition by table2.num) as cc
      from table1,table2
     where table1.num = table2.num(+)