我要查询数据库,可数据库里只有3行记录,我要查询出4行记录,
是这样的前3列是数据库里的记录可以直接查出来大家主要看第四列的结果是怎么来的。
--------------
aa|a|100|100|     100(是第四列的)是从本行第3列得来的
--------------
bb|b|50 |150|     150(是本行第四列的)=100(第一行第四列)+50(第二行第三列)
--------------
cc|c|60 |210|     210(是本行第四列的)=150(第二行第四列)+60(是本行的第3列)
--------------最后那一列数据库里没有,请问可以在SQL里写吗,怎么在SQL里写,还请指教
本人是初学者

解决方案 »

  1.   

    老实的告诉你————————————————————————————没碰到过!这样的需求很难用sql实现,不如先把1~3列的数据查询出来,然后遍历数据集来实现,
    缺点是效率比较低
      

  2.   

    --------------
    aa|a|100|100|     100(是第四列的)是从本行第3列得来的
    --------------
    bb|b|50 |150|     150(是本行第四列的)=100(第一行第四列)+50(第二行第三列)
    --------------
    cc|c|60 |210|     210(是本行第四列的)=150(第二行第四列)+60(是本行的第3列)SQL Server代码:create table t
    (
      col_1 varchar(2),col_2 varchar(1),col_3 int
    )ceate procedure get4Col
    as
    begin
    create table #temp
    (
     col_1 varchar(2),col_2 varchar(1),col_3 int,col_4 int
    )declare @tmp int
    insert into #temp(col_1,col_2,col_3) select * from tset @tmp=0
    update #temp
    set @tmp=@tmp+col_3,
        col_4=@tmpselect * from #temp
    drop table #temp
    endexec get4Col  drop table t
    drop procedure get4Col没调试,应该可以,或者用函数
      

  3.   

    实现起来不是很难!1:如果有序号(或能够唯一排序的字段<这句很难理解;(>)select fid,field1,field2,field3,(select sum(field3) from tablename where fid>=a.fid) as field4
    from tablename as a2:如果没有序号select identity(1,1) as fid,... from tablename into #t1 --产生一个序号列也可以
      

  4.   

    好象不行,
    fid>=a.fid) as field4
    a是什么
    你这是查出一个结果吧,不是每行都有一个对吗
      

  5.   

    xhh_88(三友) 
    把第三列排序按照第三列也可以吧
      

  6.   

    有个错误"2:如果没有序号select identity(1,1) as fid,... into #t1 from tablename --产生一个序号列也可以--------------------------------------------
    序号列(fid)的值必须具有唯一性,不然"fid>=a.fid"就会有错误的
      

  7.   

    xhh_88(三友) 
    老兄你这个也不行吗,
      

  8.   

    回复人: ltzperson(玩人大帝) ( ) 信誉:100  2004-09-09 09:38:00  得分: 0  
     
     
       xhh_88(三友) 
    老兄你这个也不行吗,
     
     
    _______________________-哪个不行,你用的是什么数据库?
      

  9.   

    可以,很简单的,先搞个临时表,再进行SELECT就行了。
      

  10.   

    用SQL语句很难实现
    在DELPHI里面就很很好实现
      

  11.   

    修改一下xhh_88(三友) ( ) 的代码
    SQL SERVER 中执行通过!
    select 
     identity(int,1,1) as fid,field1,field2,field3
    into #t1
    from 
    tablename 
    select
    a.fid,a.field1,a.field2,a.field3
    (select sum(field3) from #t1 where fid<=a.fid) as field4
    from #t1 as a 
    drop Table #t1
      

  12.   

    在ORACLE测试通过
    create table AA
    (
      A VARCHAR2(3),
      B VARCHAR2(3),
      C VARCHAR2(3)
    )
    添加数据
    insert into AA (A, B, C)
    values ('aa', 'bb', 'cc');
    insert into AA (A, B, C)
    values ('a', 'b', 'c');
    insert into AA (A, B, C)
    values ('100', '50', '60');
    commit;
    建立ORACLE函数
    create or replace function is_number(s varchar2)
    return char
    is
      isRight char(1);
      n number;
    begin
      isRight:='1';
      n:=to_number(s);
      return isRight;
      exception
        when others then
        isRight:='0';
        return isRight;
    end;最后你要的查询select a,b,c,1 odr from aa
    union
    select to_char(max(a)) a,to_char(max(b)+max(a)) b ,to_char(max(c)+max(b)+max(a)) c,999 odr from aa where 
    is_number(a)='1' and is_number(b)='1' and
    is_number(c)='1' 
    order by odr