请教如何写SQL:有如下2个表:
表1: Fnumber Fprice Fqty Famout 其中(famout=fprice*fqty)但做了四舍五入
aa 1.222 400 489
bb 2.222 120 267
表2: fnumber Fqty Fprice Famount
aa 100
aa 200
aa 100
bb 50
bb 70
要求如下: 将表1中的单价全部更新到表2中,由于存在误差,将所有误差调到每一个fnumber记录中的最后一笔。两表中的famount相等。得到结果如下:
表2: fnumber Fqty Fprice Famount
aa 100 1.222 122
aa 200 1.222 244
aa 100 1.222 123
bb 50 2.222 111
bb 70 2.222 156
------解决方案--------------------
----------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2013-01-16 13:49:28
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[a]
if object_id('[a]') is not null
drop table [a]
go
create table [a]
(
[Fnumber] varchar(2),
[Fprice] numeric(4,3),
[Fqty] int,
[Famout] int
)
insert [a]
select 'aa',1.222,400,489 union all
select 'bb',2.222,120,267
--> 测试数据:[b]
if object_id('[b]') is not null
drop table [b]
go
create table [b]
(
[fnumber] varchar(2),
[Fqty] int,
[Fprice] int,
[Famount] int
)
insert [b]
select 'aa',100,null,null union all
select 'aa',200,null,null union all
select 'aa',100,null,null union all
select 'bb',50,null,null union all
select 'bb',70,null,null