当前位置: 代码迷 >> Sql Server >> 累计查询如何写SQL
  详细解决方案

累计查询如何写SQL

热度:95   发布时间:2016-04-27 18:00:50.0
累计查询怎么写SQL
我们要写一个库存表,也就是查询出来如下面的结果,
这个SQL语句该如何写呢,感觉难度很大.
进出数量 累计数量
10 10
-20 -10
30 20
-10 10


------解决方案--------------------
难度确实很大..猜不中.
探讨
我们要写一个库存表,也就是查询出来如下面的结果,
这个SQL语句该如何写呢,感觉难度很大.
进出数量 累计数量
10 10
-20 -10
30 20
-10 10

------解决方案--------------------
SQL code
declare @t table (进出数量 int)insert into @tselect 10 union allselect -20 union allselect 30 union allselect -10;with m as (select row_number() over (order by (select 1)) as rid,进出数量 from @t)select 进出数量,累计数量=(select sum(进出数量) from m where rid<=a.rid) from m a/*进出数量        累计数量----------- -----------10          10-20         -1030          20-10         10*/
------解决方案--------------------
真厉害
探讨
我写出来了,如下
select top 900 a.stcode,b.cinvcode,b.cinvname, b.cinvstd,b.cwhcode,b.intb*b.brdint as intb,b.lotnumber,a.remark1,b.stsa,c.saleb,
d.intb*-1 as yintb,a.date2,e.str2 as dddd,b.remark1 as zrema……

------解决方案--------------------
这东西,累计数量列应该用after触发器来实现吧
------解决方案--------------------
探讨
我写出来了,如下
select top 900 a.stcode,b.cinvcode,b.cinvname, b.cinvstd,b.cwhcode,b.intb*b.brdint as intb,b.lotnumber,a.remark1,b.stsa,c.saleb,
d.intb*-1 as yintb,a.date2,e.str2 as dddd,b.remark1 as zrema……

------解决方案--------------------
探讨

我写出来了,如下
select top 900 a.stcode,b.cinvcode,b.cinvname, b.cinvstd,b.cwhcode,b.intb*b.brdint as intb,b.lotnumber,a.remark1,b.stsa,c.saleb,
d.intb*-1 as yintb,a.date2,e.str2 as dddd,b.remark1 as zre……
  相关解决方案