2016-08-15 11 views
-2

以下のような出力を得る出力Last two column is need output減算注文数量は

利用可能な数量のためのリンクをクリックしてください - 正の値は、それが残りの後

緑作る場合Orderqtyは=正の値

2番目の行から空きが減算され、結果が得られます 肯定結果が得られた場合は緑、それ以外は黄色になります。

           output 
Date   Item OrderQty AvailQty  A B 
12-Aug-16  A 10   50    40 Green 
13-Aug-16  A 20   50    20 Green 
14-Aug-16  A 30   50    -10 Yellow 
15-Aug-16  B 40   30    -10 Yellow 
16-Aug-16  B 50   30    0 Red 
17-Aug-16  C 100  300    200 Green 
18-Aug-16  C 170  300    30 green 
19-Aug-16  C 40   300   -10 Yellow 
20-Aug-16  C 30   300    0 Red 

50-10 = 40緑色

40-20 = 20緑色

20-30 = -10次いで黄色

によるアイテムの日付とした場合、ASC

あります最終的にはゼロになってから赤くしてください

添付の画像も確認してください電子リンクhttp://i.stack.imgur.com/GvJlD.png

おかげ

+2

1は – TheGameiswar

答えて

0
*I used here cursor and temp table to to get the output y for yellow,R for Red and G for Green. 

create table #test(
[Date] date,  
Item varchar(20), 
OrderQty int,  
AvailQty int 
) 

insert into #test values 

('2016-08-11', 'A', 10, 50), 

('2016-08-12','A', 20, 50), 

('2016-08-13','A', 30, 50), 

('2016-08-12','B', 40, 30), 

('2016-08-13','B', 50, 30), 

('2016-08-13','C', 100, 300), 

('2016-08-14','C', 170, 300), 

('2016-08-15','C', 40, 300), 

('2016-08-16','C', 30, 300) 


Alter table #TEST 
Add id int identity(1,1) 

--drop table #itemValue 
create table #itemValue (item varchar(100),AvailValue int) 

insert into #itemValue select distinct item,AvailQty from #TEST 

declare @item varchar(50) 

declare @order int 

declare @id int 

declare curname cursor 

Local scroll static 

for 

select Id,Item,OrderQty from #test 

open curname 

Fetch next from curname 

into @id,@item,@order 

declare @tem2 as int 

print cast(@id as varchar(1000)) 

set @tem2=(select AvailValue from #itemValue where [email protected]) 

-------------------------------------------------------------------- 
if(@tem2>@order) 

begin 

update #test set st='G' where [email protected] and [email protected] 

update #itemValue set [email protected] where [email protected] 

end 

else if(@tem2<@order) 

begin 

update #test set st='Y' where [email protected] and [email protected] 

update #itemValue set AvailValue=0 where [email protected] 

end 

else 

begin 

update #test set st='R' where [email protected] and [email protected] 

end 
-------------------------------------------------------------------- 

while @@FETCH_STATUS=0 

begin 

declare @tem1 as int 

print cast(@id as varchar(1000)) 

print cast(@tem1 as varchar(100)) 

--update #itemValue set [email protected] where [email protected] 

set @tem1=(select AvailValue from #itemValue where [email protected]) 

if(@tem1>@order) 

begin 

update #test set st='G' where [email protected] and [email protected] 

update #itemValue set [email protected] where [email protected] 

end 

else if(@tem1=0) 

begin 

update #test set st='R' where [email protected] and [email protected] 

end 

else if(@tem1<@order) 

begin 

update #test set st='Y' where [email protected] and [email protected] 

update #itemValue set AvailValue=0 where [email protected] 

end 

Fetch next from curname 

into @id,@item,@order 
--print cast(@item as varchar(1000)) 
end* 
+0

をコピーすることによってREPROする機会を持つことになりますように、テキストとして結果/テーブルを貼り付けるとも予想される出力を貼り付けてくださいは非常に迅速な答えをいただき、ありがとうございます。 – Ankush

関連する問題