现在有二个表 临时表(帐号,姓名,余额)
帐户信息表(帐号,姓名,上次余额,余额,累计积数)
临时表中有好多数据,要更新到帐户信息表中,如果帐户信息表中没有此帐号,就插入,上次余额就等于临时表中的余额,余额也等于临时表中的余额,累计积数也等于临时表中的余额,如果帐户信息表中有此帐号,就更新上次余额等于临时表中的余额,余额等于临时表中的余额,累计积数等于累计积数+临时表中的余额,求这条SQL,我也不知道我表达是否清楚,我在线等,谢谢!
帐户信息表(帐号,姓名,上次余额,余额,累计积数)
临时表中有好多数据,要更新到帐户信息表中,如果帐户信息表中没有此帐号,就插入,上次余额就等于临时表中的余额,余额也等于临时表中的余额,累计积数也等于临时表中的余额,如果帐户信息表中有此帐号,就更新上次余额等于临时表中的余额,余额等于临时表中的余额,累计积数等于累计积数+临时表中的余额,求这条SQL,我也不知道我表达是否清楚,我在线等,谢谢!
解决方案 »
- 求解sql语句优化。(已改语法)
- 远程连接,telnet数据库端口能打开,程序连接无反应
- Error reading data from static cursor cache是什么错误??
- 要搜索最近n天的数据,这条SQL怎么写?
- 用SQL语句如何去除列的IDENTITY约束?
- 非服务器的pc想attach一个数据库文件,被提示数据库文件必须限制在2G以内,如何解决,文件有3G
- sql合并相同记录到另一个表中<求助>
- 排序问题:libin_ftsafe(子陌红尘|潇湘剑公子@dev-club)请进来,非常着急,请你一定帮帮忙,感激不尽
- 菜鸟提问,用sql数据累加
- Vfp中,关于控件顺序的问题?
- top 进来
- 这段代码 对数据库的操作 错了吗?
帐户信息表(帐号,姓名,上次余额,余额,累计积数)
临时表中有好多数据,要更新到帐户信息表中,如果帐户信息表中没有此帐号,就插入一条新记录,上次余额就等于临时表中此帐号记录的余额,余额也等于临时表中此帐号记录的余额,累计积数也等于临时表中此帐号记录的余额,如果帐户信息表中有此帐号,就更新上次余额等于临时表中此帐号记录的余额,余额等于临时表中此帐号记录的余额,累计积数等于累计积数+临时表中此帐号记录的余额,求这条SQL,我也不知道我表达是否清楚,我在线等,谢谢!
1001 A 500
1002 B 300
1003 C 100
帐号 姓名 上次余额 余额 累计积数
1002 B 100 500 1500
1003 C 300 300 300
更新后的结果是:
帐号 姓名 上次余额 余额 累计积数
1001 A 500 500 500
1002 B 100 300 1800
1003 C 300 100 400
正确的是:
帐号 姓名 余额
1001 A 500
1002 B 300
1003 C 100
帐号 姓名 上次余额 余额 累计积数
1002 B 100 500 1500
1003 C 300 300 300
更新后的结果是:
帐号 姓名 上次余额 余额 累计积数
1001 A 500 500 500
1002 B 500 300 1800
1003 C 300 100 400
上次余额=IsNull(tb2.余额,tb1.余额)
,tb1.余额
,累计积数=(IsNull(tb2.余额,tb1.余额)+IsNull(tb2.余额,0))
from tb1
left join tb2 on tb1.帐号=tb2.帐号
union all
select t.帐号 , t.姓名 , 上次余额 = t.余额 , 余额 = t.余额 , 累计积数 = t.余额 from tb1 t where t.帐号 not in (select 帐号 from tb2)
order by 帐号
declare @tb1 table([帐号] int,[姓名] varchar(1),[余额] int)
insert @tb1
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
declare @tb2 table([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert @tb2
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
update t2
set t2.累计积数 = t2.累计积数 + t1.余额,
t2.余额 = t1.余额
from @tb1 t1 inner join @tb2 t2 on t1.帐号 = t2.帐号insert into @tb2(帐号, 姓名, 上次余额, 余额, 累计积数)
select t1.帐号, t1.姓名, t1.余额, t1.余额, t1.余额
from @tb1 t1 left join @tb2 t2 on t1.帐号 = t2.帐号
where t2.帐号 is nullselect * from @tb2 order by 帐号
/*
帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 500 500 500
1002 B 100 300 1800
1003 C 300 100 400(所影响的行数为 3 行)这样可以么?
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-19 21:29:22
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([帐号] int,[姓名] varchar(1),[余额] int)
insert [a]
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert [b]
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
--------------开始查询--------------------------update
b
set
累计积数=累计积数+余额
where
exists(select 1 from a where 帐号=b.帐号)
insert into
b
select
帐号,姓名,余额 as 上次余额,余额,余额 as 累计积数
from
a
where
not exists(select 1 from b where 帐号=a.帐号)
select * from b order by 1
----------------结果----------------------------
/*帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 500 500 500
1002 B 100 500 2000
1003 C 300 300 600(3 行受影响)
*/
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-19 21:29:22
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([帐号] int,[姓名] varchar(1),[余额] int)
insert [a]
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert [b]
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
--------------开始查询--------------------------update
b
set
累计积数=累计积数+a.余额
from
a,b
where
a.帐号=b.帐号
insert into
b
select
帐号,姓名,余额 as 上次余额,余额,余额 as 累计积数
from
a
where
not exists(select 1 from b where 帐号=a.帐号)
select * from b order by 1
----------------结果----------------------------
/*帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 500 500 500
1002 B 100 500 1800
1003 C 300 300 400(3 行受影响)
*/
正确的是:
帐号 姓名 余额
1001 A 500
1002 B 300
1003 C 100
帐号 姓名 上次余额 余额 累计积数
1002 B 100 500 1500
1003 C 300 300 300
更新后的结果是:
帐号 姓名 上次余额 余额 累计积数
1001 A 0 500 500
1002 B 500 300 1800
1003 C 300 100 400
declare @tb1 table([帐号] int,[姓名] varchar(1),[余额] int)
insert @tb1
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
declare @tb2 table([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert @tb2
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
update t2
set t2.累计积数 = t2.累计积数 + t1.余额,
t2.上次余额 = t2.余额,
t2.余额 = t1.余额
from @tb1 t1 inner join @tb2 t2 on t1.帐号 = t2.帐号insert into @tb2(帐号, 姓名, 上次余额, 余额, 累计积数)
select t1.帐号, t1.姓名, 0, t1.余额, t1.余额
from @tb1 t1 left join @tb2 t2 on t1.帐号 = t2.帐号
where t2.帐号 is nullselect * from @tb2 order by 帐号/*(
帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 0 500 500
1002 B 500 300 1800
1003 C 300 100 400(所影响的行数为 3 行)
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-19 21:29:22
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([帐号] int,[姓名] varchar(1),[余额] int)
insert [a]
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert [b]
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
--------------开始查询--------------------------update
b
set
累计积数=累计积数+a.余额
from
a,b
where
a.帐号=b.帐号
insert into
b
select
帐号,姓名,0 as 上次余额,余额,余额 as 累计积数
from
a
where
not exists(select 1 from b where 帐号=a.帐号)
select * from b order by 1
----------------结果----------------------------
/*帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 0 500 500
1002 B 100 500 1800
1003 C 300 300 400(3 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-19 21:29:22
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([帐号] int,[姓名] varchar(1),[余额] int)
insert [a]
select 1001,'A',500 union all
select 1002,'B',300 union all
select 1003,'C',100
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([帐号] int,[姓名] varchar(1),[上次余额] int,[余额] int,[累计积数] int)
insert [b]
select 1002,'B',100,500,1500 union all
select 1003,'C',300,300,300
--------------开始查询--------------------------update
b
set
累计积数=累计积数+a.余额,
上次余额 = b.余额,
余额 = a.余额
from
a,b
where
a.帐号=b.帐号
insert into
b
select
帐号,姓名,0 as 上次余额,余额,余额 as 累计积数
from
a
where
not exists(select 1 from b where 帐号=a.帐号)
select * from b order by 1
----------------结果----------------------------
/*帐号 姓名 上次余额 余额 累计积数
----------- ---- ----------- ----------- -----------
1001 A 0 500 500
1002 B 500 300 1800
1003 C 300 100 400(3 行受影响)*/
帐号 姓名 余额
1001 A 500
1002 B 300
1003 C 100
帐户信息表
帐号 姓名 上次余额 余额 累计积数
1002 B 100 500 1500
1003 C 0 300 300
开始两个表是这样的,对吧,里面都有数据。好现在开始更新
临时表中第一条记录,帐户信息表中没有,那就插入
帐号 1001 姓名 A ,上次余额为0(因为帐户信息表没有1001数据,所以说上次余额为0)
余额就等于临时表的余额=500,累计积数也等于临时表中的500
临时表中第二条记录,帐户信息表中有,那就更新
帐号 1002 姓名 B 上次余额为更新帐户信息表中的余额=500
余额就等于临时表中此帐号的余额=300 累计积数=更新的累计积数 1500+本次临时表中此帐号的余额300=1800
临时表中第三条记录,帐户信息表中有,那就更新
帐号 1003 姓名 c 上次余额为更新帐户信息表中的余额=300
余额就等于临时表中此帐号的余额=100 累计积数=更新的累计积数 300+本次临时表中此帐号的余额100=400所以更新后的结果是:
帐号 姓名 上次余额 余额 累计积数
1001 A 0 500 500
1002 B 500 300 1800
1003 C 300 100 400