你想要什么样的sql语句?
在sql server里也不可能一个select就能满足你说的条件吧

解决方案 »

  1.   

    哪位大哥帮我写一下,它年龄段没规律,我都不知道怎么group
      

  2.   

    to armyyd(不会游泳的猫)
    你在http://community.csdn.net/Expert/topic/3280/3280668.xml?temp=.5988886
    也犯了错误,你举的例子根本编译不通.slq server 中在
    beginend 
    中只有一个select 语句行的,Oracle就不行。
      

  3.   

    这里好像Oracle高手很少,又顶到没发顶了,这样都n 次了..
      

  4.   

    最终是可以形成sql并执行的。不像你说的不能执行select * from tablename
      

  5.   

    sql server 可以在
    beginend 
    中 只写
    select * from aa;
    在 Oracle 中这样写是没意义的,是错误的.

    在oracle中是select * from dual;
      

  6.   

    我写了一个
    我默认楼主的意思是:找出年龄在n1,n2 之间的人数时,年龄可以等于n1,但要小于n2,否则有的人要被计算2次了~
    根据要求建立表ageverge:
    SQL> select * from ageverge;  BEGINAGE     ENDAGE
    ---------- ----------
             0          0
             1          1
             2          9
            10         14
            15         98sql>
    create or replace procedure p1 as
    total number(2);
    v_beginage ageverge.BEGINAGE%type;
    v_endage ageverge.ENDAGE%type;
    cursor mycursor is
       select beginage,endage from ageverge;
    begin
     open mycursor;
     loop
     fetch mycursor into v_beginage,v_endage;
     exit when mycursor%notfound;
     select count(age) into total from stu where age between v_beginage and v_endage;
     dbms_output.PUT_LINE('the number of sutdents between '||v_beginage||' and '||v_endage||' is '||total);
     end loop;
     close mycursor;
    end;
    /过程已创建。SQL> execute p1;
    the number of sutdents between 0 and 0 is 0
    the number of sutdents between 1 and 1 is 2
    the number of sutdents between 2 and 9 is 2
    the number of sutdents between 10 and 14 is 0
    the number of sutdents between 15 and 98 is 2PL/SQL 过程已成功完成。
      

  7.   

    对的,你指出来的确实是我写错了。CREATE OR REPLACE PROCEDURE MY_Procedure 
    IS
    begin
    null;
    end;这个才是最简单的。楼上的写的过程是可以实现你的功能的
    create or replace procedure 
    p1 (v_beginage ageverge.BEGINAGE%type;v_endage ageverge.ENDAGE%type;)as
    total number(2);这样写比较好吧
      

  8.   

    SQL> select * from range;  FROM_AGE     TO_AGE
    ---------- ----------
             0          0
             1          1
             2          9
            10         14
            15         98create or replace view student_count_v
     (from_age, to_age, student_count) as
    select from_age, to_age, count(*)
      from range x, student y
     where y.age between x.from_age and x.to_age - 1;
      

  9.   

    出差了一天,大家已经帮我回答了这么多,我的意思好像跟大家有一点不一样,
    比如我有表:
    name   age 
    小猛   2
    小麦   3
    小明   9
     .     .  
     .     . 
    我想搜索到的结果为
    年龄段     人数
    0-1         4
    1-2         7
     .          .
     .          .
      

  10.   

    to  liuyi8903(西西) ,你说最终能执行select 语句,为什么编译都通不过。
      

  11.   

    用decode试试看:select sum(decode(sign(age-1),1,0,1)) as "0-1",
        sum(decode(sign(age-2),1,0,decode(sign(age-1),1,1,0)) as "1-2",
        sum(decode(sign(age-10),1,0,decode(sign(age-2),1,1,0)) as "2-10",
        sum(decode(sign(age-15),1,0,decode(sign(age-10),1,1,0)) as "10-15",
        sum(decode(sign(age-15),1,1,0) as "15-"
    from tablename
      

  12.   

    select '0-1',sum(case age=0 or age=1  then 1 else 0 end),
           '2',sum(case age=2 then 1 else 0 end),
           '3-10',sum(case age>2 or age<11 then 1 else 0 end),
           '11-15',sum(case age>10 or age<16 then 1 else 0 end),
           '16',sum(case age>15 then 1 else 0 end)
    from tab_namem;
      

  13.   

    try again:select '0-1',count(*)
    from tablename
    where age between 0 and 1
        union all
    select '1-2',count(*)
    from tablename
    where age between 1 and 2
        union all
    select '2-10',count(*)
    from tablename
    where age between 2 and 10
        union all
    select '10-15',count(*)
    from tablename
    where age between 10 and 15
        union all
    select '15-',count(*)
    from tablename
    where age > 15
      

  14.   

    同意Jackyhou2004(波) 和KingSunSha(弱水三千)的做法,建一个年龄段划分的表。
    来解决年龄段没规律的问题,sql也简单多了。
    所以建议用存储过程来实现。
    (用一个sql也可以实现,但会很复杂)
      

  15.   

    zw_yu(鱼猫) 的方法时候,如果要写成存储过程怎么写???
      

  16.   

    create or replace type t_refcur is ref cursor;create or replace function sf_test() return t_refcur
    is
      cs_tmp t_refcur;
    begin
       open cs_tmp for ...;   return cs_tmp;
    end;
      

  17.   

    在oracle过程中不能用select * from...
      

  18.   

    那我想执行一个复杂的select 语句 ,通过什么方法最好???
      

  19.   

    那我想执行一个复杂的select 语句 ,通过什么方法最好???--要根据实际情况吧,有的时候用视图.为了将来维护修改方便,有的时候用过程,比如前端程序取得记录集,这样可以减少网络数据流量.
      

  20.   

    先不考虑视图,因为我可能需要传入变量,对于存储过程:
    下面语句怎么写成存储过程
    select '0-1',count(*)
    from tablename
    where age between 0 and 1
        union all
    select '1-2',count(*)
    from tablename
    where age between 1 and 2
        union all
    select '2-10',count(*)
    from tablename
    where age between 2 and 10
        union all
    select '10-15',count(*)
    from tablename
    where age between 10 and 15
        union all
    select '15-',count(*)
    from tablename
    where age > 15如果年龄端分了很多个这样select 语句那就更加长了。
      

  21.   

    请你先说明参数用什么方式传入,比如用带分割符的字符串,那就肯定不能直接用视图。我的建议是用一个临时表,把传入的参数插入该表中,再用我上面写的sql来获得结果。