再次请教关于SQL问题???(100分)

sendy_cy 2003-09-23 10:41:35
以前我有提到一问题的SQL语句为:
select a.[date],
(select sum(b.[count]) from 表 b where b.[date]<=a.[date]) as total
from 表 a

现在的问题是:要是以上语句的“表”是一个很复杂的SQL语句,请问我该怎样写该SQL?


以前的贴子为:
http://expert.csdn.net/Expert/topic/2269/2269074.xml?temp=.635708
(请教SQL问题:关于“叠加分组”?)
...全文
30 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-09-23
  • 打赏
  • 举报
回复
可以直接用:
select a.[date],
(select sum(b.[count]) from (SQL语句) b where b.[date]<=a.[date]) as total
from (SQL语句) a


但从速度上考虑,最好用临时表:
首先将你的SQL语句结果生成一个临时表[#表],再用查询:

select a.[date],
(select sum(b.[count]) from #表 b where b.[date]<=a.[date]) as total
from #表 a

--使用后删除临时表
drop table #表
sdhdy 2003-09-23
  • 打赏
  • 举报
回复
select a.[date],
(select sum(b.[count]) from (你的很复杂的语句) b where b.[date]<=a.[date]) as total from 表 a
sdhdy 2003-09-23
  • 打赏
  • 举报
回复
select a.[date],
(select sum(b.[count]) from (你的很复杂的语句) b where b.[date]<=a.[date]) as total from 表 a
aierong 2003-09-23
  • 打赏
  • 举报
回复


同意大力的说法


select * into #临时表 from (一个很复杂的SQL语句) 表

这样先创建一个临时表

下面的程序写起来就清晰一些
伍子V5 2003-09-23
  • 打赏
  • 举报
回复
select a.[date],
(select sum(b.[count]) from 表 b where b.[date]<=a.[date]) as total
from (SQL 语句) a
pengdali 2003-09-23
  • 打赏
  • 举报
回复
select * into #临时表 from (一个很复杂的SQL语句) 表


select a.[date],(select sum(b.[count]) from #临时表 b where b.[date]<=a.[date]) total from #临时表 a

go
drop table #临时表
yujohny 2003-09-23
  • 打赏
  • 举报
回复
负责的SQL语句,只要是一个完整的子查询,照样可以那样写

select a.[date],
(select sum(b.[count]) from (复杂的SQL语句) b where b.[date]<=a.[date]) as total
from 表 a
Wally_wu 2003-09-23
  • 打赏
  • 举报
回复
表可以是结果集:
select a.[date],
(select sum(b.[count]) from 表 b where b.[date]<=a.[date]) as total
from (select date, total from table1) a
sendy_cy 2003-09-23
  • 打赏
  • 举报
回复
up
TaoGeGe 2003-09-23
  • 打赏
  • 举报
回复
现在的问题是:要是以上语句的“表”是一个很复杂的SQL语句,请问我该怎样写该SQL?
=================================
SQL語句做一個記錄集跟表是一樣的使用方法,隻是記得寫上別名。
txlicenhe 2003-09-23
  • 打赏
  • 举报
回复
select a.[date],
(select sum(b.[count]) from (复杂的SQL语句) b where b.[date]<=a.[date]) as total
from 表 a
killxtt 2003-09-23
  • 打赏
  • 举报
回复
一样用的。就是直接写会乱些
sendy_cy 2003-09-23
  • 打赏
  • 举报
回复
在SQLServer中建立临时表?select ...into
这样每个用户,每次使用都要建立再删除“临时表”???!!!
这样对系统数据库速度、性能等是否有影响?
还请高人解释一下!
subbee 2003-09-23
  • 打赏
  • 举报
回复
up

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧