• 全部
  • 基础类
  • 应用实例
  • 新技术前沿

SQL中的查询问题

TT008 2003-05-07 08:48:58
表A:
ID NA SY HJ
1 A 100 110
2 B 200 230
3 C 300 360
4 A 40 200
5 A 50 300
6 B 70 400
7 C 40 500
8 C 100 600
9 B 300 700
10 D 800 800

求表B
ID NA SY HJ
1 A 100 110
4 A 40 150
5 A 50 200
2 B 200 230
6 B 70 300
9 B 300 600
3 C 300 360
7 C 40 400
8 C 100 500
10 D 800 800
注只要NA相同则上一记录的HJ加下一记录的SY为HJ
...全文
4 点赞 收藏 21
写回复
21 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenlishu413 2003-05-08
你可以在表b命名时前面加个#号,加了这个符号后,就只是产生一个临时表.当你退出后自动被删除.
你在最后可以加
create view 视图名 as
select * from #表b
这样子就只有一个视图了.临时表会自动被删除.
回复
chenlishu413 2003-05-08
不會是我的吧.
回复
TT008 2003-05-08
结果是正确的但是就差那么一点了
回复
happydreamer 2003-05-07
昨天的测试结果


测试了j老师,老衲和海老大的代码 结果和你们一样
环境 win2k server(chn)+sql2k enterprise(chn)+sp3

大力你的那个我是了两台机器 另一台为2k pro +个人版+无sp3
结果都是
YOki 44 57 90 191 1
Tom 0 45 99 144 2

没想到union all 会有这个问题

--处理过程:
--select f1,f5,identity(int,1,1) as id into #t from (
select * from (
select f1,f5 from @a
union all
select f1,f6 from @a
union all
select f1,f7 from @a
union all
select f1,f8 from @a
) A --order by f1


1 14
2 3
1 6
2 45
1 13
2 25
1 25
2 8





--select f1,f5,identity(int,1,1) as id into #t from (
select * from (
select f1,f5 from @a
union
select f1,f6 from @a
union
select f1,f7 from @a
union
select f1,f8 from @a
) A -- order by f1
--exec('alter table #t add id int identity(1,1)')


1 6
1 13
1 14
1 25
2 3
2 8
2 25
2 45

union 和union all 取记录的方式不同
union 是按顺序先取第1个记录再第二个
union all 则是交叉取记录

回复
happydreamer 2003-05-07

select * into B from A

update b
set sy=a.hj
from (select * from b where hj=(select min(hj) from b a where a.na=b.na) )a
where b.id=a.id


select id,na,sy,sy+isnull((select sum(sy) from b where id<aa.id and na=aa.na),0) from b aa
order by na,id
回复
happydreamer 2003-05-07

select * into B from A

update b
set sy=a.hj
from (select * from b where hj=(select min(hj) from b a where a.na=b.na) )a
where b.id=a.id


select id,na,sy,sy+isnull((select sum(sy) from b where id<aa.id and na=aa.na),0) from b aa
order by na,id
回复
TT008 2003-05-07
crazyfor的方法好
但出来的结果只是在第一笔记录上而不是一条一条笔记录的计算出来
回复
CrazyFor 2003-05-07
TRY:
select id,na,sy,hj+isnull((select sum(sy) from a where na=aa.na and id>aa.id ),0) from a as aa order by Na,Id

回复
Rewiah 2003-05-07
表A为多个VIEW的查询得出的结果

贴出这个查询的代码
回复
TT008 2003-05-07
第二种解法生成的是一个表
我只想建立表B一样的VIEW
可以吗?
回复
TT008 2003-05-07
表A为多个VIEW的查询得出的结果
我想建立表B一样的VIEW
可以吧?
回复
TT008 2003-05-07
第一种解法在表A为VIEW时出错
第二种解法只能运行一次,并且在将它转为VIEW时出错
还有别的方法吗?
回复
select * into B from A
update B set B.HJ = A.HJ - A.sy from B, A where B.NA = A.NA and A.id = (select min(id) from A where NA = B.NA)
update B set HJ = B.HJ + isnull((select sum(A.sy) from A where NA = B.NA and ID <= B.id),0) from B

select * from B order by NA
回复
Rewiah 2003-05-07
更正(已测试):
select * from (
select * from 表A a
where id=(select min(id) from 表A where NA=a.Na)
union all
select ID,NA,SY,(select HJ from 表A b where b.NA=a.Na and b.id=(select min(id) from 表A where NA=a.Na))+(select sum(SY) from 表A where NA=a.NA and id<=a.id and id<>(select min(id) from 表A where NA=a.Na)) as HJ from 表A a
where id<>(select min(id) from 表A where NA=a.Na)
) as x
order by Na,Id

回复
Rewiah 2003-05-07
select * from (
select * from 表A a
where id=(select min(id) from 表A where NA=a.Na)
union all
select ID,NA,SY,(select HJ from 表A b where b.NA=a.Na and b.id=(select min(id) from 表A where NA=b.Na))+(select sum(SY) from 表A where NA=a.NA and id<=a.id and id<>(select min(id) from 表A where NA=b.Na)) as HJ from 表A a
where id<>(select min(id) from 表A where NA=a.Na)
) as x
order by Na,Id

回复
happydreamer 2003-05-07
喔 上面有一段我回错地方了 sorry
回复
TT008 2003-05-07
我都说过不要生成一个新表
我只想用一个VIEW查询出来
不知可不可以
回复
chenlishu413 2003-05-07
程序測試結果正確.
回复
chenlishu413 2003-05-07
TT008 (T T) 快放分吧.
回复
chenlishu413 2003-05-07
SELECT * INTO 表b FROM 表a ORDER BY NA

declare mycursor cursor scroll dynamic for
select distinct(na) from 表b order by na

declare @na1 varchar(50)
declare @a int

open mycursor

fetch first from mycursor into @na1

while @@fetch_status=0
begin
select @a=count(*) from 表b where na=@na1

if @a>1
begin
declare mycursor1 cursor scroll dynamic for
select id,hj from 表b where na=@na1 order by na
declare @b int
declare @b1 int
declare @id1 int
open mycursor1
fetch first from mycursor1 into @id1,@b
while @@fetch_status=0
begin
fetch next from mycursor1 into @id1,@b1
update 表b
set hj=sy+@b
where hj=@b1 and id=@id1
select @b=hj from 表b where hj=sy+@b and id=@id1
end
close mycursor1
deallocate mycursor1
end

fetch next from mycursor into @na1
end

close mycursor
deallocate mycursor


回复
加载更多回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2003-05-07 08:48
社区公告
暂无公告