各位大侠,情况是这样的,
例如:
某月1号,进货300件,单价50元,
同月2号,进货100件,单价100元,
同月3号,发货200件,单价100元,
同月4号,发货150件,单价50元,问题如下:如何才能用存储过程实现求利润?
以上为例,这中间牵扯到第一次发货200件后,第一次进货还剩下100件,然后第二次发货150件,需发完第一次剩下的100件后,再发第二次进货的50件.各位大侠,各位高手,各位高手之高手之高高手们,
请大家给出出主意吧.
小弟,技穷啦,实在想不出来了.
例如:
某月1号,进货300件,单价50元,
同月2号,进货100件,单价100元,
同月3号,发货200件,单价100元,
同月4号,发货150件,单价50元,问题如下:如何才能用存储过程实现求利润?
以上为例,这中间牵扯到第一次发货200件后,第一次进货还剩下100件,然后第二次发货150件,需发完第一次剩下的100件后,再发第二次进货的50件.各位大侠,各位高手,各位高手之高手之高高手们,
请大家给出出主意吧.
小弟,技穷啦,实在想不出来了.
id name amount(件) price(元)
1 aa 300 50
2 aa 100 100发货表
id name amount(件) price(元)
1 aa 200 100
2 aa 150 50要求利润啊:
利润的公式是,每一次发货的总价,减去进货的成本价啊.
例如:上述例子,(200*100-50*100)+(150*50-100*50[第一次剩余的进货成本]-50*100[第二次进货成本])大侠,帮帮忙吧,谢谢啦.
id name amount(件) price(元)
1 aa 300 50
2 aa 100 100发货表
id name amount(件) price(元)
1 aa 200 100
2 aa 150 50
--------------------------------------------------
不知道LZ出來的結果會是什麼??id name amount(件) price(元) 利潤
1 aa 200 100 ? 10000??
2 aa 150 50 ? 7500??
--先寫個游標的,你看看是不是你要的結果???
--測試環境
create table tabin(id int,name varchar(10),amount int,price int)
insert into tabin
select 1,'aa',300,50 union all
select 2,'aa',100,100create table tabout(id int,name varchar(10),amount int,price int)
insert into tabout
select 1,'aa',200,100 union all
select 2,'aa',150,50
GO/*求利潤*/
select *,[used]=0 into #t1 from tabin
select *,[money]=0 into #t2 from tabout declare @id int,@name varchar(10),@amount int,@price int
declare c1 cursor for
select id,name,amount,price from #t2 order by id
open c1
fetch next from c1 into @id,@name,@amount,@price
while @@fetch_status=0
begin
update a
set [used]=case when isnull((select sum(amount) from #t1 where id<a.id and name=a.name and name=@name),0)>=@amount
then 0
else case when isnull((select sum(amount) from #t1 where id<=a.id and name=a.name and name=@name),0)>=@amount
then @amount-isnull((select sum(amount) from #t1 where id<a.id and name=a.name and name=@name),0)
else amount end
end
from #t1 a
where name=@name update #T2 set [money]=amount*price-(select sum([used]*price) from #t1 where #t1.name=#t2.name)
where id=@id
update #t1 set amount=amount-[used]
delete #t1 where amount<=0
fetch next from c1 into @id,@name,@amount,@price
end
close c1
deallocate c1/*查看結果*/
select * from #t2
/*
id name amount price money
----------- ---------- ----------- ----------- -----------
1 aa 200 100 10000
2 aa 150 50 -2500
*//*刪除測試環境*/
drop table tabin,tabout,#t1,#t2
可我在前台怎么调用啊?
我用的DataGrid控件显示利润啊,
晕了啊有点.........
太强了,太佩服你了,
老大,可我在前台怎么调用啊,
能给出个办法么?
我做在网页里用DataGrid控件显示的,
怎么调用这些Sql语句呢啊?
我会的只有调用存储过程的啊,
老大,帮忙帮到底啊,
谢谢了啊.
太强了,太佩服你了,
老大,可我在前台怎么调用啊,
能给出个办法么?
我做在网页里用DataGrid控件显示的,
怎么调用这些Sql语句呢啊?
我会的只有调用存储过程的啊,
老大,帮忙帮到底啊,
谢谢了啊.
---------------
这个就最好去相关开发环境的论坛问。
AS
set nocount on
select *,[used]=0 into #t1 from tabin
select *,[money]=0 into #t2 from tabout declare @id int,@name varchar(10),@amount int,@price int
declare c1 cursor for
select id,name,amount,price from #t2 order by id
open c1
fetch next from c1 into @id,@name,@amount,@price
while @@fetch_status=0
begin
update a
set [used]=case when isnull((select sum(amount) from #t1 where id<a.id and name=a.name and name=@name),0)>=@amount
then 0
else case when isnull((select sum(amount) from #t1 where id<=a.id and name=a.name and name=@name),0)>=@amount
then @amount-isnull((select sum(amount) from #t1 where id<a.id and name=a.name and name=@name),0)
else amount end
end
from #t1 a
where name=@name update #T2 set [money]=amount*price-(select sum([used]*price) from #t1 where #t1.name=#t2.name)
where id=@id
update #t1 set amount=amount-[used]
delete #t1 where amount<=0
fetch next from c1 into @id,@name,@amount,@price
end
close c1
deallocate c1
select * from #t2
set nocount offGO
SqlDataAdapter da= new SqlDataAdapter();
SqlConnection sc=new SqlConnection("....");
da.SelectCommand=new SqlCommand("[dbo].[usp_test]",sc);
da.SelectCommand.CommandType=CommandType.StoredProcedure;
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
请问如何加上时间呢,
求某段时间内的利润,
时间字段用字符型定义,date varchar(8),例如,20060816
select * from #t2
加上 where 條件啊.要是時間條件還影響進貨出貨的算法,就稍微複雜一些,
舉個例子:
2/1 進貨 100
3/1 進貨 200
3/5 出貨 50假如要算3月份的利潤,出貨的50算2/1的還是3/1的? 不過大體意思都出來了,自己改改好了,ps.你給的2個表還都沒有時間欄位.