2013-12-16 56 views
-1

我希望輸出像下面,我有這四列,請幫我生成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 
+0

如何共享您的表格結構,解釋您試圖實現的目標以及迄今爲止嘗試的內容? – Mureinik

+0

由於您是新用戶,因此您不得上傳圖片。嘗試通過添加更多信息來解釋自己,顯示您嘗試過的代碼,表格結構等。 – Yaroslav

+0

您可以隨時發佈鏈接到您的表格圖像。 – FeliceM

回答

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 
+0

我需要計算第i列中的運行餘額。 – user2404603

0

檢查我query.my日期山坳爲datetime不varchar.also排除不need.you列基本上需要MyTotal從我的SQL。

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