日期:2014-05-18  浏览次数:20399 次

累计查询怎么写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 @t
select 10 union all
select -20 union all
select 30 union all
select -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         -10
30          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……