首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么对以下记录进行筛选

2012-03-31 
如何对以下记录进行筛选?表:peopleidnamesexrusjzhiwu1张三男2004-01-01士兵2李五男2003-05-05连长3李九男

如何对以下记录进行筛选?
表:people
id       name         sex     rusj                     zhiwu
1         张三         男       2004-01-01         士兵
2         李五         男       2003-05-05         连长
3         李九         男       2002-05-05         排长
4         孙四         男       2002-02-05         营长
5         孙红         女       2002-02-05         后勤

需要按以下3个条件筛选出结果,符合结果的人员可以获得津贴。
1排长以上级别(包括排长)rusj满1年
2士兵rusj满3年,后勤人员满2年。
我想按时间段查询在时间段内满足以上条件的筛选出来。比如:
2003-01-01     到2004-01-01     结果:
3         李九         男       2002-05-05         排长
4         孙四         男       2002-02-05         营长




[解决办法]
select * from people where datediff(year,rusj,getdate())> (case zhiwu when '士兵 ' then 3 when '后勤 ' then 2 else 1 end)
[解决办法]
create table people(id int, name varchar(20), sex varchar(10), rusj datetime, zhiwu varchar(20))
insert into people select 1 , '张三 ' , '男 ' , '2004-01-01 ' , '士兵 ' union all
select 2 , '李五 ' , '男 ' , '2003-05-05 ' , '连长 ' union all
select 3 , '李九 ' , '男 ' , '2002-05-05 ' , '排长 ' union all
select 4 , '孙四 ' , '男 ' , '2002-02-05 ' , '营长 ' union all
select 5 , '孙红 ' , '女 ' , '2002-02-05 ' , '后勤 '


select * from people where datediff(year,rusj, '2004-01-01 ')> (case zhiwu when '士兵 ' then 3 when '后勤 ' then 2 else 1 end)

------------
3李九男2002-05-05 00:00:00.000排长
4孙四男2002-02-05 00:00:00.000营长
[解决办法]
--try

select * from people where zhiwu= '士兵 ' and rusj between '2003-01-01 ' and '2004-01-01 ' and
datediff(year, rusj, getdate())
-case when dateadd(month, datediff(month, rusj, getdate()), rusj)> getdate()
then 1 else 0 end> 3
union all
select * from people where zhiwu= '后勤 ' and rusj between '2003-01-01 ' and '2004-01-01 ' and
datediff(year, rusj, getdate())
-case when dateadd(month, datediff(month, rusj, getdate()), rusj)> getdate()
then 1 else 0 end> 2
union all
select * from people where zhiwu not in( '后勤 ', '士兵 ') and rusj between '2003-01-01 ' and '2004-01-01 ' and
datediff(year, rusj, getdate())
-case when dateadd(month, datediff(month, rusj, getdate()), rusj)> getdate()
then 1 else 0 end> 0
[解决办法]
create table people(id int, name nvarchar(10), sex nvarchar(10), rusj datetime, zhiwu nvarchar(10))
insert people select 1, '张三 ', '男 ', '2004-01-01 ', '士兵 '


union all select 2, '李五 ', '男 ', '2003-05-05 ', '连长 '
union all select 3, '李九 ', '男 ', '2002-05-05 ', '排长 '
union all select 4, '孙四 ', '男 ', '2002-02-05 ', '营长 '
union all select 5, '孙红 ', '女 ', '2002-02-05 ', '后勤 '

select * from people where zhiwu= '士兵 ' and rusj between '2003-01-01 ' and '2004-01-01 ' and
(datediff(month, rusj, getdate())
-case when dateadd(month, datediff(month, rusj, getdate()), rusj)> getdate()
then 1 else 0 end)/12> =3
union all
select * from people where zhiwu= '后勤 ' and rusj between '2003-01-01 ' and '2004-01-01 ' and
(datediff(month, rusj, getdate())
-case when dateadd(month, datediff(month, rusj, getdate()), rusj)> getdate()
then 1 else 0 end)/12> =2
union all
select * from people where zhiwu not in( '后勤 ', '士兵 ') and rusj between '2003-01-01 ' and '2004-01-01 ' and
(datediff(month, rusj, getdate())
-case when dateadd(month, datediff(month, rusj, getdate()), rusj)> getdate()
then 1 else 0 end)/12> =1

热点排行