CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

请教一个SQL语句

楼主lzccaxwx(天涯寻觅)2004-11-04 10:57:33 在 MS-SQL Server / 基础类 提问

有一个表TB,其结构和数据如下  
  ID       VALUE  
  A           10  
  B           20  
  C           30  
  D           40  
  通过一查询后得到如下数据  
  ID       VALUE  
  A           10  
  B           30  
  C           60  
  D           100  
  请各位大哥帮帮忙 问题点数:20、回复次数:6Top

1 楼lsxaa(小李铅笔刀)回复于 2004-11-04 10:59:17 得分 10

select   id,(select   sum(value)   from   tb   where   value<=a.value)   as   value  
  from   tb   aTop

2 楼davorsuker39(大狐狸)回复于 2004-11-04 11:30:30 得分 0

UP     lsxaa(小李铅笔刀)Top

3 楼shuiniu(飞扬的梦)(我是一头只吃西红柿的水牛)回复于 2004-11-04 11:51:31 得分 0

--or  
  select   a.id,sum(b.value)   from   test   a   join   test   b   on   a.id   >=   b.id  
  group   by   a.id,a.valueTop

4 楼yanzheng1(光原)回复于 2004-11-04 12:58:22 得分 10

楼上的都有问题,如果VALUE不是按顺序排列的话就出错?  
  declare   errors   scroll   cursor   for  
          select   id,value   from   tb  
  open   errors  
  if   exists   (select   name   from   sysobjects   where   name   =   'table_rate_yzg_p3')   drop   table   table_rate_yzg_p3  
  create   table   table_rate_yzg_p3    
  (  
  id   varchar(10),  
  value   int,  
  )  
  go  
  declare   @i   int,@row   int,@id   char(10),@value   int,@zong   int  
  set   @i=0  
  set   @zong=0  
  label:  
  set   @i=@i+1  
  fetch   next   from   errors   into   @id,@value  
  begin  
  set   @zong   =   @zong   +   @value  
  insert   into   table_rate_yzg_p3   select   @id,@zong  
  end  
  if   @i<@@cursor_rows  
  goto   label  
  select   *   from   table_rate_yzg_p3    
  if   exists   (select   name   from   sysobjects   where   name   =   'table_rate_yzg_p3')   drop   table   table_rate_yzg_p3  
  close   errors  
  deallocate   errorsTop

5 楼lsxaa(小李铅笔刀)回复于 2004-11-04 13:00:13 得分 0

ID   总该不重复吧  
  select   id,(select   sum(value)   from   tb   where   ID<=a.ID)   as   value  
  from   tb   a  
  order   by   ID  
  Top

6 楼lzccaxwx(天涯寻觅)回复于 2004-11-04 14:09:12 得分 0

ID不会重复,谢谢各位大哥的帮忙Top

相关问题

  • 求SQL语句
  • sql语句。
  • sql语句?
  • sql 语句?
  • 求SQL语句
  • ***求SQL语句***
  • sql语句??
  • sql语句
  • sql语句
  • SQL语句

关键词

  • p3
  • yzg
  • zong
  • rate
  • table
  • select

得分解答快速导航

  • 帖主:lzccaxwx
  • lsxaa
  • yanzheng1

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo