sql语句 疑难问题求大仙帮忙

白水2010 2012-12-03 01:39:34
从数据看查询出有两列的数据 生成的表是第一列是从数据库取的数据 第二列是 第一列当列之前的和
2 2
1 3
4 7
22 29

这个sql怎么写啊 救命啊
...全文
146 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 9 楼 baishui2011 的回复:
引用 3 楼 bdmh 的回复:加入每个记录有id,而且是顺序的 SQL code?? 12345 SELECT f1,(SELECT SUM(f1) AS total FROM test AS b ……
WHERE (id <=a.id)
白水2010 2012-12-03
  • 打赏
  • 举报
回复
引用 3 楼 bdmh 的回复:
加入每个记录有id,而且是顺序的



SQL code??



12345

SELECT f1,(SELECT SUM(f1) AS total FROM test AS b WHERE (id < a.id)) AS……
白水2010 2012-12-03
  • 打赏
  • 举报
回复
引用 3 楼 bdmh 的回复:
加入每个记录有id,而且是顺序的



SQL code??



12345

SELECT f1,(SELECT SUM(f1) AS total FROM test AS b WHERE (id < a.id)) AS……

方法 真的很好用 但是 数据好像 错开了一行, 这个该怎么解决呢 麻烦了 嘿嘿

  • 打赏
  • 举报
回复
引用 6 楼 baishui2011 的回复:
引用 3 楼 bdmh 的回复:加入每个记录有id,而且是顺序的 SQL code?? 12345 SELECT f1,(SELECT SUM(f1) AS total FROM test AS b ……
当然可以用,只要主键有序就可以
白水2010 2012-12-03
  • 打赏
  • 举报
回复
引用 3 楼 bdmh 的回复:
加入每个记录有id,而且是顺序的 SQL code?? 12345 SELECT f1,(SELECT SUM(f1) AS total FROM test AS b WHERE (id < a.id)) AS……
这个似乎 是很好的方法 我试试 看看能用不
  • 打赏
  • 举报
回复
引用 4 楼 baishui2011 的回复:
引用 2 楼 hjywyj 的回复:with tb(id) as( select 2 union all select 1 union all select 4 union all select 22 ), source as( select id,rowindex=row_number()over(order by getdate())……
我也只是举个例子 tb是表名,id是字段 select id,(select sum(id) from (select id,rowindex=row_number()over(order by getdate()) from tb)source2 where source2.rowindex<=source1.rowindex)sumid from (select id,rowindex=row_number()over(order by getdate()) from tb) source1
白水2010 2012-12-03
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
with tb(id) as( select 2 union all select 1 union all select 4 union all select 22 ), source as( select id,rowindex=row_number()over(order by getdate()) from tb ) select id,(selec……
我上面的数字是 举个例子 ,我想做的是这种模式
bdmh 2012-12-03
  • 打赏
  • 举报
回复
加入每个记录有id,而且是顺序的

SELECT     f1,(SELECT     SUM(f1) AS total
                            FROM          test AS b
                            WHERE      (id < a.id)) AS Expr1
FROM         test AS a
  • 打赏
  • 举报
回复
with tb(id) as( select 2 union all select 1 union all select 4 union all select 22 ), source as( select id,rowindex=row_number()over(order by getdate()) from tb ) select id,(select sum(id) from source source2 where source2.rowindex<=source1.rowindex)sumid from source source1
bookc-man 2012-12-03
  • 打赏
  • 举报
回复
貌似这个要用到递归

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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