有两个表,
工资表 gongzi
select emp_code,fenqi,gz form gongzi.存储所有员工工资信息。
emp_code fenqi gz
001 200905 2500
001 200906 2600
001 200907 2600
002 200905 2000
002 200906 2100
002 200907 2300
empcode = 员工工号 主键
fenqi = 每月分期 主键
gz = 每月总工资
奖金表
select emp_code ,date,money,rem form jiangjin
emp_code date money rem
001 2009-05-08 200 五一奖金
001 2009-05-31 400 奖金
001 2009-06-15 100 奖金
001 2009-06-19 150 奖金
001 2009-06-30 100 奖金
002 2009-05-08 300 五一奖金
002 2009-05-31 500 奖金
002 2009-06-15 200 奖金
002 2009-06-19 250 奖金
002 2009-06-30 300 奖金
emp_code = 员工工号,与工资表关联。。
date = 记录奖金调整的日期,也必须与工资表的分期关联。如2009-05-08,2009-05-31 应对应工资表的200905 分期
money = 奖金,以最后一次调整后的为准,月末计入总工资。
rem = 备注,记录调整奖金的原因。
现在我写了一条语句
select top 1 g.emp_code,fenqi,gz,date,money,rem
from gongzi g,jiangjin j
where g.emp_code = j.emp_code and g.fenqi = '200905'
and (DATENAME ( yy , date )+DATENAME ( mm , date )) = g.fenqi
and g.emp_code = '001'
order by date desc
查询一个人在指定分期内的 信息,包括组后调整的奖金项,不需要累计入总工资,因为在调整奖金项时就计入
查询结果是
emp_code fenqi gz date money rem
001 200905 2500 2009-05-31 00:00:00.000 400 奖金
再写一条查询001员工的6月份信息
select top 1 g.emp_code,fenqi,gz,date,money,rem
from gongzi g,jiangjin j
where g.emp_code = j.emp_code and g.fenqi = '200906'
and (DATENAME ( yy , date )+DATENAME ( mm , date )) = g.fenqi
and g.emp_code = '001'
order by date desc
查询结果是
emp_code fenqi gz date money rem
001 200906 2600 2009-06-30 00:00:00.000 100 奖金
但是现在遇到一个问题,一个一个员工单独查就没问题
问题是我是多重选取员工一起查询,我要的结果是一个员工在指定分期内,只有一条数据,即与单独查是一致的才是正确的
我试着把sql语句修改成
select top 1 g.emp_code,fenqi,gz,date,money,rem
from gongzi g,jiangjin j
where g.emp_code = j.emp_code and g.fenqi = '200905'
and (DATENAME ( yy , date )+DATENAME ( mm , date )) = g.fenqi
and g.emp_code in ('001','002')
order by date desc
查询结果是
emp_code fenqi gz date money rem
001 200905 2500 2009-05-31 00:00:00.000 400 奖金
我想要的结果是
查询结果是
emp_code fenqi gz date money rem
001 200905 2500 2009-05-31 00:00:00.000 400 奖金
002 200905 2000 2009-05-31 00:00:00.000 500 奖金
不知道如何写?大家帮帮忙,高手帮我一下,万分感激
下面是 两个表的 脚本
if exists (select * from sysobjects where id = OBJECT_ID('[gongzi]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [gongzi]
CREATE TABLE [gongzi] (
[emp_code] [varchar] (50) NOT NULL,
[fenqi] [varchar] (50) NOT NULL,
[gz] [int] NULL)
ALTER TABLE [gongzi] WITH NOCHECK ADD CONSTRAINT [PK_gongzi] PRIMARY KEY NONCLUSTERED ( [emp_code] )
INSERT [gongzi] ([emp_code],[fenqi],[gz]) VALUES ( '001','200905',2500)
INSERT [gongzi] ([emp_code],[fenqi],[gz]) VALUES ( '001','200906',2600)
INSERT [gongzi] ([emp_code],[fenqi],[gz]) VALUES ( '001','200907',2600)
INSERT [gongzi] ([emp_code],[fenqi],[gz]) VALUES ( '002','200905',2000)
INSERT [gongzi] ([emp_code],[fenqi],[gz]) VALUES ( '002','200906',2100)
INSERT [gongzi] ([emp_code],[fenqi],[gz]) VALUES ( '002','200907',2300)
if exists (select * from sysobjects where id = OBJECT_ID('[jiangjin]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [jiangjin]
CREATE TABLE [jiangjin] (
[emp_code] [varchar] (50) NULL,
[date] [datetime] NULL,
[money] [int] NULL,
[rem] [varchar] (50) NULL)
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '001','2009-05-08 0:00:00',200,'五一奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '001','2009-05-31 0:00:00',400,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '001','2009-06-15 0:00:00',100,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '001','2009-06-19 0:00:00',150,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '001','2009-06-30 0:00:00',100,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '002','2009-05-08 0:00:00',300,'五一奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '002','2009-05-31 0:00:00',500,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '002','2009-06-15 0:00:00',200,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '002','2009-06-19 0:00:00',250,'奖金')
INSERT [jiangjin] ([emp_code],[date],[money],[rem]) VALUES ( '002','2009-06-30 0:00:00',300,'奖金')