在sql server 2005中使用两列运行余额查询

时间:2013-12-16 09:28:00

标签: sql sql-server

我想输出如下,我有这四列,请帮我用sql查询生成第五列

31-Mar-13   Product-A   50000   0   50000
2-Apr-13    Product-A   0   2000    48000
4-Apr-13    Product-A   0   3000    45000
6-Apr-13    Product-A   0   2500    42500
9-Apr-13    Product-A   0   2500    40000
11-Apr-13   Product-A   0   3000    37000
15-Apr-13   Product-A   0   3000    34000
16-Apr-13   Product-A   0   2000    32000
18-Apr-13   Product-A   0   1000    31000
20-Apr-13   Product-A   0   2000    29000

我的表格中包含如下数据,数据可能会被更改。

   31-Mar-13    Product-A   50000   0
    2-Apr-13    Product-A   0   2000    
    4-Apr-13    Product-A   0   3000    
    6-Apr-13    Product-A   0   2500    
    9-Apr-13    Product-A   0   2500
    11-Apr-13   Product-A   0   3000    
    15-Apr-13   Product-A   0   3000    
    16-Apr-13   Product-A   0   2000    
    18-Apr-13   Product-A   0   1000    
    20-Apr-13   Product-A   0   2000

2 个答案:

答案 0 :(得分:1)

 SELECT '31-Mar-13' [Date],  'Product-A' product,  50000 val1 , 0 val2 , 50000 total into #temp union all SELECT 
 '2-Apr-13' ,  'Product-A' ,  0  , 2000  ,  48000 union all SELECT 
 '4-Apr-13' ,  'Product-A' ,  0  , 3000  ,  45000 union all SELECT 
 '6-Apr-13' ,  'Product-A' ,  0  , 2500  ,  42500 union all SELECT 
 '9-Apr-13' ,  'Product-A' ,  0  , 2500  ,  40000 union all SELECT 
 '11-Apr-13',  'Product-A' ,  0  , 3000  ,  37000 union all SELECT 
 '15-Apr-13',  'Product-A' ,  0  , 3000  ,  34000 union all SELECT 
 '16-Apr-13',  'Product-A' ,  0  , 2000  ,  32000 union all SELECT 
 '18-Apr-13',  'Product-A' ,  0  , 1000  ,  31000 union all SELECT 
 '20-Apr-13',  'Product-A' ,  0  , 2000  ,  29000

   select Date, product,    val1    ,val2 ,val1+val2 total from #temp

如果val1,val2是varchar / nvarchar则使用

select Date,    product,    val1    ,val2 ,cast(val1 as int)+cast(val2 as int) total from #temp

答案 1 :(得分:0)

检查我的查询。我的日期col是datetime而不是varchar.also排除你不需要的列。你基本上需要我的sql中的Mytotal。

Declare @t table([Date] date,product varchar(50),val1 int,val2 int,total int)
 insert into @t
 SELECT '03-31-13' [Date],  'Product-A' product,  50000 val1 , 0 val2 , 50000 total
 union all SELECT 
 '04-2-13' ,  'Product-A' ,  0  , 2000  ,  48000 union all SELECT 
 '04-4-13' ,  'Product-A' ,  0  , 3000  ,  45000 union all SELECT 
 '04-6-13' ,  'Product-A' ,  0  , 2500  ,  42500 union all SELECT 
 '04-9-13' ,  'Product-A' ,  0  , 2500  ,  40000 union all SELECT 
 '04-11-13',  'Product-A' ,  0  , 3000  ,  37000 union all SELECT 
 '04-15-13',  'Product-A' ,  0  , 3000  ,  34000 union all SELECT 
 '04-16-13',  'Product-A' ,  0  , 2000  ,  32000 union all SELECT 
 '04-18-13',  'Product-A' ,  0  , 1000  ,  31000 union all SELECT 
 '04-20-13',  'Product-A' ,  0  , 2000  ,  29000

--select *,row_number() over(order by date)rn  from @t a
;With CTE as
 (select *,row_number() over(order by date)rn from @t a
 )
 ,MAXCTE as
 (select max(rn) MaxRn from cte)
 ,CTE1 as
 (select *,val1-val2 as Mytotal from cte where rn=1
union all
select a.*,b.Mytotal-a.val2  from cte a 
inner join  cte1 b on a.product=b.product
cross apply MAXCTE mc
where a.rn between b.rn+1 and mc.MaxRn and a.rn-b.rn=1
 )

 select  a.*
from cte1 a