*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*
1は – TheGameiswar