Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从sql server中的可用数量中减去订单数量,获得如下输出_Sql Server - Fatal编程技术网

Sql server 从sql server中的可用数量中减去订单数量,获得如下输出

Sql server 从sql server中的可用数量中减去订单数量,获得如下输出,sql-server,Sql Server,请点击链接获取输出 可用数量-订单数量=正值 如果正值为绿色 然后从第二行中减去剩余的可用值,得到结果 如果结果为阳性,则将绿色变为黄色 output Date Item OrderQty AvailQty A B 12-Aug-16 A 10 50 40 Green 13-Aug-16

请点击链接获取输出

可用数量-订单数量=正值

如果正值为绿色

然后从第二行中减去剩余的可用值,得到结果 如果结果为阳性,则将绿色变为黄色

                                             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

如果最终可用为零,则将其设为红色

请同时检查附加的图像链接


谢谢

请将结果/表格粘贴为文本,并粘贴预期输出,这样您就有机会通过复制进行复制。谢谢您的快速回答。
*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 item=@item)

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

begin

update #test set st='G' where Item=@item and id=@id

update #itemValue set AvailValue=AvailValue-@order where item=@item

end

else if(@tem2<@order) 

begin

update #test set st='Y' where Item=@item and id=@id

update #itemValue set AvailValue=0 where item=@item

end

else

begin

update #test set st='R' where Item=@item and id=@id

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 AvailValue=AvailValue-@order where item=@item

set @tem1=(select AvailValue from #itemValue where item=@item)

if(@tem1>@order )

begin

update #test set st='G' where Item=@item and id=@id

update #itemValue set AvailValue=AvailValue-@order where item=@item

end

else if(@tem1=0) 

begin

update #test set st='R' where Item=@item and id=@id

end

else if(@tem1<@order) 

begin

update #test set st='Y' where Item=@item and id=@id

update #itemValue set AvailValue=0 where item=@item

end

Fetch next from curname

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