日期:2014-05-18  浏览次数:20558 次

求sql写法 过滤 汇总
表1 表2
编号 月份 工资 编号 月份 姓名 性别 年龄 收入类别
1 201101 100 1 201101 张三 男 22 a
2 201101 200 1 201101 张三 男 22 b
3 201101 300 1 201101 张三 男 22 c
4 201101 400 2 201101 李四 男 20 a
5 201101 500 2 201101 李四 男 20 b
6 201101 600 3 201101 王五 男 19 a
3 201101 王五 男 19 b
3 201101 王五 男 19 c
4 201101 赵六 男 25 a
5 201101 陈七 男 26 a
6 201101 刘八 男 25 a
6 201101 刘八 男 25 b
6 201101 刘八 男 25 c
6 201101 刘八 男 25 d

我要的结果如下
编号 月份 姓名 性别 年龄 收入类别 工资
1 201101 张三 男 22 a 100
1 201101 张三 男 22 b 0
1 201101 张三 男 22 c 0
2 201101 李四 男 20 a 200
2 201101 李四 男 20 b 0
3 201101 王五 男 19 a 300
3 201101 王五 男 19 b 0
3 201101 王五 男 19 c 0
4 201101 赵六 男 25 a 400
5 201101 陈七 男 26 a 500
6 201101 刘八 男 25 a 600
6 201101 刘八 男 25 b 0
6 201101 刘八 男 25 c 0
6 201101 刘八 男 25 d 0



左边的是表1,右边的是表2,我想把表1的工资复制到表2中,条件是编号和月份相等,
但是只复制重复数据中的第一行,后面自动为0


------解决方案--------------------
row_number()
------解决方案--------------------
探讨
row_number()

------解决方案--------------------
SQL code
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'
go
select 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          100
1           201101 张三         男          22          b          0
1           201101 张三         男          22          c          0
2           201101 李四         男          20          a          200
2           201101 李四         男          20          b          0
3           201101 王五         男          19          a          300
3           201101 王五         男          19          b          0
3           201101 王五         男          19          c          0
4           201101 赵六         男          25          a          400
5           201101 陈七         男          26          a          500
6           201101 刘八         男          25          a          600
6           201101 刘八         男          25          b          0
6           201101 刘八         男          25          c          0
6           201101 刘八         男          25          d          0

(14 行受影响)

*/
go
drop table t1,t2

------解决方案--------------------
SQL code
----------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2011-09-16 13:28:53
-- Verstion:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel