取出分组后每组的第一条记录(不用group by)按时间排序---转

it2024-12-20  7

操作日志表CREATE TABLE JobLog  -- 操作日志表(    JobLogId] int NOT NULL ,       -- 主键    FunctionId nvarchar(20) NULL , -- 功能Id    OperateTime datetime NULL      -- 操作时间) ON PRIMARYGOALTER TABLE JobLog ADD CONSTRAINT PK_JobLog PRIMARY KEY CLUSTERED(JobLogId) ON PRIMARYGO

-- 操作日志表的所有记录SELECT * FROM JobLog查询结果:1    001    2007-11-012    001    2007-11-023    001    2007-11-034    002    2007-11-045    002    2007-11-056    003    2007-11-067    004    2007-11-078    004    2007-11-089    005    2007-11-0910  005    2007-11-10

-- 每个功能最后一次操作记录SELECT * FROM JobLog A WHERE JobLogId in       (SELECT TOP 1 JobLogId FROM JobLog          WHERE A.FunctionId = FunctionId ORDER BY OperateTime DESC       )查询结果:3    001    2007-11-035    002    2007-11-056    003    2007-11-068    004    2007-11-0810  005    2007-11-10

转载于:https://www.cnblogs.com/_zjl/archive/2011/05/30/2063612.html

最新回复(0)