dxinchen 发表于 2011-02-19 21:32

在线等,请教一个查询最后一次时间问题

大虾们,请教一个问题:
假如有个表ip是员工号,o_time是请假时间,其他的内容省略。
#table
(
ipint,
o_time        datetime,
......
)

如果要查询在上月时间内,同一个ip(即同一个人)的最后一次请假的请假时间而且请假次数超过4次的人员名单。怎么sql语句或者sybase怎么写?

dxinchen 发表于 2011-02-19 22:02

select ip,count(*) as total into #table from table where 日期段 group by ip having count(*)>4 select table.ip,max(table.o_time) from table,#table where talbe.ip=#table.ip

andkylee 发表于 2011-02-21 09:42

认为楼主想要的结果是: 请假次数超过4次的人员的最后一次请假的请假时间及其它信息。


下面是测试情况:


create table #table
(
ipint,
o_time      datetime)
go
insert into #table values(1,'2011-01-02')
insert into #table values(2,'2011-01-02')
insert into #table values(1,'2011-01-03')
insert into #table values(1,'2011-01-04')
insert into #table values(1,'2011-01-05')
insert into #table values(3,'2011-01-12')
insert into #table values(3,'2011-01-20')
insert into #table values(1,'2011-01-31')
go


select * from #table
where left(convert(varchar,o_time,112),6) = left(convert(varchar,dateadd(mm,-1,getdate()),112),6)
group by ip
having count(*) > 4and o_time = max(o_time)
go



其中: left(convert(varchar,o_time,112),6) = left(convert(varchar,dateadd(mm,-1,getdate()),112),6)表示匹配上个月的日期。
页: [1]
查看完整版本: 在线等,请教一个查询最后一次时间问题