麻烦了,难度很大,请大家帮帮忙

JiangHongTao 2007-12-27 08:58:28
有三个表:
订单表(t_order):存放订单号(orderid) 和订单金额(summoney)
订单明细(t_orderitem):存放订单号(orderid),商品号(wareid),订单数量(quantity)
商品表(t_ware):存放商品号(wareid),商品名称(warename),商品价格(price)

现在的问题是:商品表的商品价格被误操作清零了,能否通过订单表和订单明细恢复商品价格。
订单表中的订单金额等于订单明细表中订单号相同的所有订单数量和商品价格乘积之和。
所有商品的价格都没有改变过。

举个例子,但表中不只4种商品,也不只4张订单:
结果为:
select wareid,price from t_ware
wareid price
1 1.00
2 100.00
3 1000.00
4 10.00

例子数据如下:

create table t_order(orderid int,summoney numeric(12,2) primary key(orderid))
create table t_orderitem(orderid int,wareid int,quantity int primary key(orderid,wareid))
create table t_ware(wareid int,warename varchar(20),price numeric(8,2) primary key(wareid))
insert t_ware select 1,'铅笔',0
union all select 2,'餐桌',0
union all select 3,'彩电',0
union all select 4,'蛋糕',0

insert t_order select 1,3351
union all select 2,8241
union all select 3,2334
union all select 4,7533

insert t_orderitem select 1,1,1
union all select 1,2,3
union all select 1,3,3
union all select 1,4,5
union all select 2,1,5
union all select 2,2,1
union all select 2,3,2
union all select 2,4,8
union all select 3,1,4
union all select 3,2,3
union all select 3,3,2
union all select 3,4,3
union all select 4,1,3
union all select 4,2,5
union all select 4,3,7
union all select 4,4,3






...全文
216 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
netcup 2007-12-29
  • 打赏
  • 举报
回复
楼主的本帖子已经被收藏
pt1314917 2007-12-28
  • 打赏
  • 举报
回复
楼主自己解出来了?
赞一个。。。
JiangHongTao 2007-12-28
  • 打赏
  • 举报
回复


create table t_order (orderid int,summoney numeric(12,2) primary key(orderid))
create table t_orderitem(orderid int,wareid int,quantity int primary key(orderid,wareid))
create table t_ware (wareid int,warename varchar(20),price numeric(8,2) primary key(wareid))
insert t_ware select 1,'铅笔',0
union all select 2,'餐桌',0
union all select 3,'彩电',0
union all select 4,'蛋糕',0

insert t_order select 1,3351
union all select 2,2185
union all select 3,2334
union all select 4,7533

insert t_orderitem select 1,1,1
union all select 1,2,3
union all select 1,3,3
union all select 1,4,5
union all select 2,1,5
union all select 2,2,1
union all select 2,3,2
union all select 2,4,8
union all select 3,1,4
union all select 3,2,3
union all select 3,3,2
union all select 3,4,3
union all select 4,1,3
union all select 4,2,5
union all select 4,3,7
union all select 4,4,3

go
create proc xyz
as
begin

--定义方程式表及变量
declare @ii int,@i int,@j int
declare @ic varchar(10),@jc varchar(10)
declare @sql varchar(1000),@sql1 varchar(1000)
declare @price numeric(8,2)

create table #pp (p numeric(8,2))
--计算变量数
select @ii = count(*) from t_order
if @ii < 2
begin
select '没有足够的数据,不能计算'
return
end

--生成方程式
create table #tt(id int,s numeric(20,2))
set @i = 1
set @sql = 'select a.orderid,a.summoney'
while @i <= @ii
begin
set @ic = rtrim(@i)
set @sql = @sql +',0,sum(case wareid when '+@ic+' then quantity else 0 end)'
exec('alter table #tt add a'+@ic+' bigint,b'+@ic+' bigint')
set @i = @i +1
end
set @sql = @sql +' from t_order a,t_orderitem b where a.orderid = b.orderid group by a.orderid,a.summoney'
insert #tt exec(@sql)


--消除

set @i = 1
while @i < @ii
begin
set @ic = rtrim(@i)
set @sql = 'update #tt set s = a.s*b.b'+@ic+'-a.b'+@ic+'*b.s,b'+@ic+'=0'
set @sql1 = ' from #tt a,(select s,b'+@ic
set @j = @i+1
while @j <=@ii
begin
set @jc = rtrim(@j)
set @sql = @sql + ',b'+@jc+'=a.b'+@jc+'*b.b'+@ic+'-a.b'+@ic+'*b.b'+@jc
set @sql1 = @sql1+',b'+@jc
set @j = @j + 1
end
set @sql = @sql + @sql1 + ' from #tt where id = '+@ic+' ) b where a.id > '+@ic
set @i = @i + 1
exec(@sql)
end

--计算
set @i = @ii
while @i >0
begin
set @ic = rtrim(@i)
set @sql = 'select (s '
set @j = @i+1
while @j <=@ii
begin
set @jc = rtrim(@j)
set @sql = @sql + '-a'+@jc+'*b'+@jc
set @j = @j + 1
end
set @sql = @sql + ')/b'+@ic+' from #tt where id = '+@ic
delete #pp
insert #pp exec(@sql)
select @price = p from #pp
set @sql ='update #tt set a'+@ic+'='+rtrim(@price)
exec(@sql)
set @sql ='update t_ware set price = '+rtrim(@price) +' where wareid ='+@ic
exec(@sql)
set @i = @i - 1
end
select * from t_ware
end
go
xyz
drop table t_order
drop table t_orderitem
drop table t_ware
drop proc xyz


结果:
1 铅笔 1.00
2 餐桌 100.00
3 彩电 1000.00
4 蛋糕 10.00

richardyuan 2007-12-28
  • 打赏
  • 举报
回复
商品多的话就很麻烦咯
chensijian 2007-12-28
  • 打赏
  • 举报
回复
老龟厉害,这个用数学解好过用代码解,复杂!!
qiuming0306 2007-12-28
  • 打赏
  • 举报
回复
所有的订单都是拥有所有物品种类吗?
我想可以找到两份单子只差一个物品的,这样可以很简单的算出来一部分!然后根据算出来的这部分在计算其他的就很方便了!
要不都是多元方程,但来那个很大的话很难算出来,会很慢
JiangHongTao 2007-12-28
  • 打赏
  • 举报
回复
终于可解多元一次方程了,自己顶。
WhyAndAnswer 2007-12-28
  • 打赏
  • 举报
回复
每个定单都是n元方程组,呵呵,还是另想办法吧
JiangHongTao 2007-12-28
  • 打赏
  • 举报
回复


create table t_order (orderid int,summoney numeric(12,2) primary key(orderid))
create table t_orderitem(orderid int,wareid int,quantity int primary key(orderid,wareid))
create table t_ware (wareid int,warename varchar(20),price numeric(8,2) primary key(wareid))
insert t_ware select 1,'铅笔',0
union all select 2,'餐桌',0
union all select 3,'彩电',0
union all select 4,'蛋糕',0

insert t_order select 1,3351
union all select 2,2185
union all select 3,2334
union all select 4,7533

insert t_orderitem select 1,1,1
union all select 1,2,3
union all select 1,3,3
union all select 1,4,5
union all select 2,1,5
union all select 2,2,1
union all select 2,3,2
union all select 2,4,8
union all select 3,1,4
union all select 3,2,3
union all select 3,3,2
union all select 3,4,3
union all select 4,1,3
union all select 4,2,5
union all select 4,3,7
union all select 4,4,3
go

create function dbo.getq(@i int,@j int)
returns numeric(8,2)
as
begin
declare @s int
set @s = 0
if @j > 0 select @s = quantity from t_orderitem where orderid = @i and wareid = @j
if @j =0 select @s = summoney from t_order where orderid = @i
if @j < 0 select @s = price from t_ware where wareid = @i
return @s
end
go
--定义变量
declare @ii int,@i int
select @ii = count(*) from t_order
--消除
set @i = 1
while @i < @ii
begin
update t_orderitem set quantity = quantity*dbo.getq(@i,@i) - dbo.getq(@i,wareid)*dbo.getq(orderid,@i)
where orderid > @i and wareid > @i
update t_order set summoney = summoney*dbo.getq(@i,@i) - dbo.getq(@i,0)*dbo.getq(orderid,@i) where orderid > @i
update t_orderitem set quantity = 0 where orderid > @i and wareid = @i
set @i = @i + 1
end
--计算
set @i = @ii
while @i > 0
begin
update t_ware set price = summoney / quantity
from t_orderitem a,t_order b,t_ware c where a.orderid = b.orderid and a.orderid = @i and a.wareid = @i and c.wareid = @i
update t_order set summoney = summoney - quantity * dbo.getq(@i,0-@i)
from t_orderitem a,t_order b where a.orderid = b.orderid and a.wareid = @i
set @i = @i -1
end
--结果
select * from t_ware
--清除
drop table t_order
drop table t_orderitem
drop table t_ware
drop function dbo.getq

这个更简单
结果
1 铅笔 1.00
2 餐桌 100.00
3 彩电 1000.00
4 蛋糕 10.00
pt1314917 2007-12-27
  • 打赏
  • 举报
回复
解这个方程组:
a+3b+3c+5d=3351
5a+b+2c+8d=2185
4a+3b+2c+3d=2334
3+5b+7c+3d=7533
pt1314917 2007-12-27
  • 打赏
  • 举报
回复
果然是四元一次方程组。顶。。。
wzy_love_sly 2007-12-27
  • 打赏
  • 举报
回复
老龟都搞出什么方程了?
dawugui 2007-12-27
  • 打赏
  • 举报
回复
这不是小学的,是中学的.
dawugui 2007-12-27
  • 打赏
  • 举报
回复
四元一次方程,有四个方程,按数学角度是可以解出来的.
我试试.
JiangHongTao 2007-12-27
  • 打赏
  • 举报
回复
真的难度很大,分不够再加
JiangHongTao 2007-12-27
  • 打赏
  • 举报
回复
铅笔数量×铅笔价格+餐桌数量×餐桌价格+彩电数量×彩电价格+蛋糕数量×蛋糕价格 = 订单金额
订单1: 1 * A1 + 3 * A2 + 3 * A3 + 5 * A4 = 3351
订单2: 5 * A1 + 1 * A2 + 2 * A3 + 8 * A4 = 2185
订单3: 4 * A1 + 3 * A2 + 2 * A3 + 3 * A4 = 2334
订单4: 3 * A1 + 5 * A2 + 7 * A3 + 3 * A4 = 7533

不能算出A1,A2,A3,A4吗??????
netcup 2007-12-27
  • 打赏
  • 举报
回复
好象不对,你的ORDER表中的金额是整个订单的合计金额。但是你一个订单如果有多个品种多个数量,那肯定不能用你的定单合计金额除以数量了,这个定单明细必须有个每个品种的合计金额才能算出每个品种的单价。总之,必须得有单品金额才能算出单品单价。
netcup 2007-12-27
  • 打赏
  • 举报
回复
update t_ware set price=c.summoney/b.quntity from t_ware a join t_orderitem b on a.wareid=b.wareid join t_order c on b.orderid=c.orderid
JiangHongTao 2007-12-27
  • 打赏
  • 举报
回复
自己做的例子数据
dawugui 2007-12-27
  • 打赏
  • 举报
回复
wareid price
1 1.00
2 100.00
3 1000.00
4 10.00
你这结果怎么算出来的?

加载更多回复(6)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧