34,576
社区成员
发帖
与我相关
我的任务
分享
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[ATTTIME] datetime,[UserID] int)
insert [tb]
select 1,'2010-01-01 8:10:00',1 union all
select 2,'2010-01-01 8:15:00',1 union all
select 3,'2010-01-01 17:10:00',2 union all
select 4,'2010-01-02 8:05:00',1 union all
select 5,'2010-01-02 17:00:00',1
select CONVERT(char(18),[ATTTIME],100) like
from tb
with cte as(
select [UserID],[ATTTIME],CONVERT(char(8),[ATTTIME],112) as date1
,case when CONVERT(char(18),[ATTTIME],100) like '%AM' then 'montime' else 'aftertime' end as AMORPM
from tb)
select userid,date1,[montime],[aftertime]
from ( select userid,date1,AMORPM,case when AMORPM='montime' then MIN(atttime) else MAX(atttime) end as atttime
from cte
group by userid,date1,AMORPM
) a
PIVOT
( min([ATTTIME])
for AMORPM in([montime],[aftertime])) as pvt
order by userid
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-02-04 20:11:42
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[ATTTIME] datetime,[UserID] int)
insert [tb]
select 1,'2010-01-01 8:10:00',1 union all
select 2,'2010-01-01 8:15:00',1 union all
select 3,'2010-01-01 17:10:00',2 union all
select 4,'2010-01-02 8:05:00',1 union all
select 5,'2010-01-02 17:00:00',1
--------------开始查询--------------------------
select
userid,
convert(varchar(10),[ATTTIME],120) as date,
case datepart(hour,min([ATTTIME])) when 8 then convert(varchar(8),min([ATTTIME]),108) else null end as montime,
case datepart(hour,max([ATTTIME])) when 17 then convert(varchar(8),max([ATTTIME]),108) else null end as aftertime
from
[tb]
group by
userid,convert(varchar(10),[ATTTIME],120)
order by
userid
----------------结果----------------------------
/* userid date montime aftertime
----------- ---------- -------- ---------
1 2010-01-01 08:10:00 NULL
1 2010-01-02 08:05:00 17:00:00
2 2010-01-01 NULL 17:10:00
(3 行受影响)
*/
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[ATTTIME] datetime,[UserID] int)
insert [tb]
select 1,'2010-01-01 8:10:00',1 union all
select 2,'2010-01-01 8:15:00',1 union all
select 3,'2010-01-01 17:10:00',2 union all
select 4,'2010-01-02 8:05:00',1 union all
select 5,'2010-01-02 17:00:00',1
select * from [tb]
/*
userid date montime aftertime
1 2010-01-01 8:10:00 null
1 2010-01-02 8:05:00 17:00:00
2 2010-01-01 null 17:10:00
*/
select userid,
convert(varchar(10),[ATTTIME],120) as date,
case datepart(hour,min([ATTTIME])) when 8 then convert(varchar(8),min([ATTTIME]),108) else null end as montime,
case datepart(hour,max([ATTTIME])) when 17 then convert(varchar(8),max([ATTTIME]),108) else null end as aftertime
from [tb]
group by userid,convert(varchar(10),[ATTTIME],120)
order by userid
--------------------------------
1 2010-01-01 08:10:00 NULL
1 2010-01-02 08:05:00 17:00:00
2 2010-01-01 NULL 17:10:00