我要查询数据库,可数据库里只有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里写,还请指教
本人是初学者
是这样的前3列是数据库里的记录可以直接查出来大家主要看第四列的结果是怎么来的。
--------------
aa|a|100|100| 100(是第四列的)是从本行第3列得来的
--------------
bb|b|50 |150| 150(是本行第四列的)=100(第一行第四列)+50(第二行第三列)
--------------
cc|c|60 |210| 210(是本行第四列的)=150(第二行第四列)+60(是本行的第3列)
--------------最后那一列数据库里没有,请问可以在SQL里写吗,怎么在SQL里写,还请指教
本人是初学者
缺点是效率比较低
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没调试,应该可以,或者用函数
from tablename as a2:如果没有序号select identity(1,1) as fid,... from tablename into #t1 --产生一个序号列也可以
fid>=a.fid) as field4
a是什么
你这是查出一个结果吧,不是每行都有一个对吗
把第三列排序按照第三列也可以吧
序号列(fid)的值必须具有唯一性,不然"fid>=a.fid"就会有错误的
老兄你这个也不行吗,
xhh_88(三友)
老兄你这个也不行吗,
_______________________-哪个不行,你用的是什么数据库?
在DELPHI里面就很很好实现
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
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