求sql
时间:2011-12-23
来源:互联网
名字 现金 银行
a 5 66
a 6 77
b 5 23
数据集2
名字 累计
a 599
b 444
要这样的结果
a 5 66 599
a 6 77
b 5 23 444
语句应该怎么写,谢谢!
作者: caoyang0299 发布时间: 2011-12-23
作者: fredrickhu 发布时间: 2011-12-23
a 6 77 599
b 5 23 444
那样写的话是这样的结果,我就需要1个599
作者: caoyang0299 发布时间: 2011-12-23
if object_id('tb1') is not null drop table tb1 go create table tb1 ( 名字 varchar(10), 现金 int, 银行 int ) go insert into tb1 select 'a','5',66 union all select 'a','6',77 union all select 'b','5',23 go if object_id('tb2') is not null drop table tb2 go create table tb2 ( 名字 varchar(10), 累计 int ) go insert into tb2 select 'a',599 union all select 'b',444 go select t1.名字,现金,银行,累计=isnull(ltrim(t2.累计),'') from ( select *,row=row_number() over(partition by 名字 order by getdate()) from tb1 )t1 left join ( select *,row=row_number() over(partition by 名字 order by getdate()) from tb2 )t2 on t1.名字=t2.名字 and t1.row=t2.row go /* 名字 现金 银行 累计 ---------- ----------- ----------- ------------ a 5 66 599 a 6 77 b 5 23 444 (3 行受影响) */
作者: pengxuan 发布时间: 2011-12-23
create table 数据集1 ( 名字 varchar(10), 现金 int, 银行 int ) go insert into 数据集1 select 'a','5',66 union all select 'a','6',77 union all select 'b','5',23 go create table 数据集2 ( 名字 varchar(10), 累计 int ) go insert into 数据集2 select 'a',599 union all select 'b',444 go select m.*, case when 现金 = (select top 1 现金 from 数据集1 where 名字 = m.名字) then cast(n.累计 as varchar) else '' end 累计 from 数据集1 m , 数据集2 n where m.名字 = n.名字 drop table 数据集1,数据集2 /* 名字 现金 银行 累计 ---------- ----------- ----------- ------------------------------ a 5 66 599 a 6 77 b 5 23 444 (所影响的行数为 3 行) */
作者: dawugui 发布时间: 2011-12-23
作者: caoyang0299 发布时间: 2011-12-23
SQL code
create table 数据集1
(
名字 varchar(10),
现金 int,
银行 int
)
go
insert into 数据集1
select 'a','5',66 union all
select 'a','6',77 union all
select 'b','5',23
go
create table 数据集2
(
名字……
作者: szstephenzhou 发布时间: 2011-12-23
from (select 名字,现金,银行 from 数据集1 )a
left join
(select 名字,累计 from 数据集1) b
on a.名字=b.名字
这里的数据集1、2分别代表 你的数据表。
作者: qq517995084 发布时间: 2011-12-23
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28