查询每个人最晚的纪录!!求搭救
表
index userid workid time
1 1 1 2013-07-23 12:38:10
2 2 2 2013-07-23 12:38:11
3 3 3 2013-07-23 12:38:12
4 1 4 2013-07-23 12:38:13
5 2 5 2013-07-23 12:38:14
6 3 6 2013-07-23 12:38:15
想要查出每个人最晚那条记录 如
4 1 4 2013-07-23 12:38:13
5 2 5 2013-07-23 12:38:14
6 3 6 2013-07-23 12:38:15
求搭救 sql?查询?
[解决办法]
select * from 表 a
where time=(select max(time) from 表 where a.userid=userid)
[解决办法]
select a.*
from tb a
inner join (select userid,max(time) as time from tb group by userid)b
on a.userid=b.userid and a.time=b.time
[解决办法]
create table tb([index] int, userid int,workid int,[time] datetime)
insert into tb
select 1,1,1,'2013-07-23 12:38:10'
union all select 2,2,2,'2013-07-23 12:38:11'
union all select 3,3,3,'2013-07-23 12:38:12'
union all select 4,1,4,'2013-07-23 12:38:13'
union all select 5,2,5,'2013-07-23 12:38:14'
union all select 6,3,6,'2013-07-23 12:38:15'
select * from tb
select a.*
from tb a
inner join (select userid,max(time) as time from tb group by userid)b
on a.userid=b.userid and a.time=b.time
order by userid
drop table tb
/*
4142013-07-23 12:38:13.000
5252013-07-23 12:38:14.000
6362013-07-23 12:38:15.000
*/
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000
(6 行受影响)
*/
[解决办法]
create table tb([index] int, userid int,workid int,[time] datetime)
insert into tb
select 1,1,1,'2013-07-23 12:38:10'
union all select 2,2,2,'2013-07-23 12:38:11'
union all select 3,3,3,'2013-07-23 12:38:12'
union all select 4,1,4,'2013-07-23 12:38:13'
union all select 5,2,5,'2013-07-23 12:38:14'
union all select 6,3,6,'2013-07-23 12:38:15'
WITH CTE AS
(SELECT ROW_NUMBER() OVER(PARTITION BY userid ORDER BY [time] DESC) AS [rank],* FROM tb
)
SELECT * FROM CTE WHERE [rank] =1
drop table tb
/*
rankindexuseridworkidtime
14142013-07-23 12:38:13.000
15252013-07-23 12:38:14.000
16362013-07-23 12:38:15.000
*/
[解决办法]
二次循环
第一循环:人员
——可以用user表
——也可以group by一下
第二循环:top 1 where 姓名=第一循环的姓名 order by time desc
[解决办法]
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] ([index] int,userid int,workid int,time datetime)
insert into [TB]
select 1,1,1,'2013-07-23 12:38:10' union all
select 2,2,2,'2013-07-23 12:38:11' union all
select 3,3,3,'2013-07-23 12:38:12' union all
select 4,1,4,'2013-07-23 12:38:13' union all
select 5,2,5,'2013-07-23 12:38:14' union all
select 6,3,6,'2013-07-23 12:38:15'
select * from [TB]
SELECT *
FROM dbo.TB
WHERE NOT EXISTS ( SELECT 1 --注意使用not exists,最好匹配对应的索引,参考:http://blog.csdn.net/orchidcat/article/details/6267552
FROM TB B
WHERE TB.userid = B.userid
AND B.TIME > TB.TIME )
/*
indexuseridworkidtime
4142013-07-23 12:38:13.000
5252013-07-23 12:38:14.000
6362013-07-23 12:38:15.000*/
[解决办法]
try this,
-- 建索引
create index idx_[表名]_userid_time on [表名](userid,[time])
-- 查询
select a.*
from [表名] a
inner join
(select userid,
max([time]) 'maxtime'
from [表名]
group by userid) b on a.userid=b.userid and a.[time]=b.maxtime
IF OBJECT_ID('Ta') IS NOT NULL
DROP TABLE Ta
GO
CREATE TABLE Ta
( [index] INT,
userid INT,
workid INT,
[time] DATETIME
)
GO
INSERT Ta
SELECT 1,1,1,'2013-07-23 12:38:10' UNION
SELECT 2,2,2,'2013-07-23 12:38:11' UNION
SELECT 3,3,3,'2013-07-23 12:38:12' UNION
SELECT 4,1,4,'2013-07-23 12:38:13' UNION
SELECT 5,2,5,'2013-07-23 12:38:14' UNION
SELECT 6,3,6,'2013-07-23 12:38:15'
SELECT *
FROM TA a
WHERE EXISTS (
SELECT 1
FROM TA b
WHERE A.userid=b.userid and a.[time]>b.[time]
)
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] ([index] int,userid int,workid int,time datetime)
insert into [TB]
select 1,1,1,'2013-07-23 12:38:10' union all
select 2,2,2,'2013-07-23 12:38:11' union all
select 3,3,3,'2013-07-23 12:38:12' union all
select 4,1,4,'2013-07-23 12:38:13' union all
select 5,2,5,'2013-07-23 12:38:14' union all
select 6,3,6,'2013-07-23 12:38:15'
--应该创建userid asc,time desc 复合索引
CREATE INDEX IX_TB_fieldlist ON dbo.[TB]
(
userid,
[time] DESC
)
--50W数据,一般的服务器级别的硬件问题不大
SELECT b.*
FROM(SELECT DISTINCT userid FROM TB) A--不过你应该有现在的user表,用user表代替这行代码,效率会更好
CROSS APPLY
(SELECT TOP(1) * FROM TB M WHERE M.userid = A.userid ORDER BY [time] DESC) B
/*
indexuseridworkidtime
4142013-07-23 12:38:13.000
5252013-07-23 12:38:14.000
6362013-07-23 12:38:15.000
*/