求sql写法 过滤 汇总
表1表2
编号月份工资编号月份姓名性别年龄收入类别
12011011001201101张三男22a
22011012001201101张三男22b
32011013001201101张三男22c
42011014002201101李四男20a
52011015002201101李四男20b
62011016003201101王五男19a
3201101王五男19b
3201101王五男19c
4201101赵六男25a
5201101陈七男26a
6201101刘八男25a
6201101刘八男25b
6201101刘八男25c
6201101刘八男25d
我要的结果如下
编号月份姓名性别年龄收入类别工资
1201101张三男22a100
1201101张三男22b0
1201101张三男22c0
2201101李四男20a200
2201101李四男20b0
3201101王五男19a300
3201101王五男19b0
3201101王五男19c0
4201101赵六男25a400
5201101陈七男26a500
6201101刘八男25a600
6201101刘八男25b0
6201101刘八男25c0
6201101刘八男25d0
左边的是表1,右边的是表2,我想把表1的工资复制到表2中,条件是编号和月份相等,
但是只复制重复数据中的第一行,后面自动为0
[解决办法]
row_number()
[解决办法]
create table t1(编号 int,月份 varchar(6),工资 int)create table t2(编号 int,月份 varchar(6),姓名 nvarchar(10),性别 nvarchar(10),年龄 int,收入类别 nvarchar(10))insert into t1 select 1,'201101',100 insert into t1 select 2,'201101',200 insert into t1 select 3,'201101',300 insert into t1 select 4,'201101',400 insert into t1 select 5,'201101',500 insert into t1 select 6,'201101',600 insert into t2 select 1,'201101','张三','男',22,'a'insert into t2 select 1,'201101','张三','男',22,'b'insert into t2 select 1,'201101','张三','男',22,'c'insert into t2 select 2,'201101','李四','男',20,'a'insert into t2 select 2,'201101','李四','男',20,'b'insert into t2 select 3,'201101','王五','男',19,'a'insert into t2 select 3,'201101','王五','男',19,'b'insert into t2 select 3,'201101','王五','男',19,'c'insert into t2 select 4,'201101','赵六','男',25,'a'insert into t2 select 5,'201101','陈七','男',26,'a'insert into t2 select 6,'201101','刘八','男',25,'a'insert into t2 select 6,'201101','刘八','男',25,'b'insert into t2 select 6,'201101','刘八','男',25,'c'insert into t2 select 6,'201101','刘八','男',25,'d'goselect a.编号,a.月份,a.姓名,a.性别,a.年龄,a.收入类别,(case when a.收入类别='a' then b.工资 else 0 end) as 工资from t2 a inner join t1 b on a.编号=b.编号/*编号 月份 姓名 性别 年龄 收入类别 工资----------- ------ ---------- ---------- ----------- ---------- -----------1 201101 张三 男 22 a 1001 201101 张三 男 22 b 01 201101 张三 男 22 c 02 201101 李四 男 20 a 2002 201101 李四 男 20 b 03 201101 王五 男 19 a 3003 201101 王五 男 19 b 03 201101 王五 男 19 c 04 201101 赵六 男 25 a 4005 201101 陈七 男 26 a 5006 201101 刘八 男 25 a 6006 201101 刘八 男 25 b 06 201101 刘八 男 25 c 06 201101 刘八 男 25 d 0(14 行受影响)*/godrop table t1,t2
[解决办法]
------------------------------ Author :fredrickhu(小F,向高手学习)-- Date :2011-09-16 13:28:53-- Verstion:-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) -- Apr 22 2011 11:57:00 -- Copyright (c) Microsoft Corporation-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)--------------------------------> 测试数据:[a]if object_id('[a]') is not null drop table [a]go create table [a]([编号] int,[月份] int,[工资] int)insert [a]select 1,201101,100 union allselect 2,201101,200 union allselect 3,201101,300 union allselect 4,201101,400 union allselect 5,201101,500 union allselect 6,201101,600--> 测试数据:[b]if object_id('[b]') is not null drop table [b]go create table [b]([编号] int,[月份] int,[姓名] varchar(4),[性别] varchar(2),[年龄] int,[收入类别] varchar(1),[工资] int)insert [b]select 1,201101,'张三','男',22,'a',null union allselect 1,201101,'张三','男',22,'b',null union allselect 1,201101,'张三','男',22,'c',null union allselect 2,201101,'李四','男',20,'a',null union allselect 2,201101,'李四','男',20,'b',null union allselect 3,201101,'王五','男',19,'a',null union allselect 3,201101,'王五','男',19,'b',null union allselect 3,201101,'王五','男',19,'c',null union allselect 4,201101,'赵六','男',25,'a',null union allselect 5,201101,'陈七','男',26,'a',null union allselect 6,201101,'刘八','男',25,'a',null union allselect 6,201101,'刘八','男',25,'b',null union allselect 6,201101,'刘八','男',25,'c',null union allselect 6,201101,'刘八','男',25,'d',null--------------开始查询--------------------------update bset 工资= a.工资from a,(select px=row_number()over(partition by 姓名 order by GETDATE()),* from b)bwhere a.编号=b.编号and a.月份=b.月份and b.px=1 select * from b----------------结果----------------------------/* 编号 月份 姓名 性别 年龄 收入类别 工资----------- ----------- ---- ---- ----------- ---- -----------1 201101 张三 男 22 a 1001 201101 张三 男 22 b NULL1 201101 张三 男 22 c NULL2 201101 李四 男 20 a 2002 201101 李四 男 20 b NULL3 201101 王五 男 19 a 3003 201101 王五 男 19 b NULL3 201101 王五 男 19 c NULL4 201101 赵六 男 25 a 4005 201101 陈七 男 26 a 5006 201101 刘八 男 25 a 6006 201101 刘八 男 25 b NULL6 201101 刘八 男 25 c NULL6 201101 刘八 男 25 d NULL(14 行受影响)*/