27,579
社区成员
发帖
与我相关
我的任务
分享
if object_id('[huang]') is not null drop table [huang]
go
create table [huang](
[ID] int,
[USER] varchar(1),
[ACCESS_PAGE] varchar(6),
[ACCESS_DATE_TIME] datetime)
insert [huang]
select 1,'A','PAGE-A','2013/10/01' union all
select 2,'A','PAGE-A','2013/10/02' union all
select 3,'A','PAGE-A','2013/10/03' union all
select 4,'A','PAGE-B','2013/10/01' union all
select 5,'A','PAGE-B','2013/10/02' union all
select 6,'B','PAGE-A','2013/10/03' union all
select 7,'C','PAGE-B','2013/10/05' union all
select 8,'D','PAGE-A','2013/10/01' union all
select 9,'D','PAGE-A','2013/10/09' union all
select 10,'D','PAGE-B','2013/10/11'
select distinct
[user],
a as ACCESS_PAGE_A_TIME,
b as ACCESS_PAGE_B_TIME
from
(
select id,
[user],
[ACCESS_PAGE],
[ACCESS_DATE_TIME],
max(case when [ACCESS_PAGE] = 'PAGE-A' then [ACCESS_DATE_TIME] else null end) over(partition by [user]) as a,
max(case when [ACCESS_PAGE] = 'PAGE-B' then [ACCESS_DATE_TIME] else null end) over(partition by [user]) as b
from huang
)t
where a is not null and b is not null
/*
user ACCESS_PAGE_A_TIME ACCESS_PAGE_B_TIME
---- ----------------------- -----------------------
A 2013-10-03 00:00:00.000 2013-10-02 00:00:00.000
D 2013-10-09 00:00:00.000 2013-10-11 00:00:00.000
*/
;with cte(ID,[USER],ACCESS_PAGE,ACCESS_DATE_TIME ) as
(
select 1,'A','PAGE-A','2013/10/01'
union all select 2,'A','PAGE-A','2013/10/02'
union all select 3,'A','PAGE-A','2013/10/03'
union all select 4,'A','PAGE-B','2013/10/01'
union all select 5,'A','PAGE-B','2013/10/02'
union all select 6,'B','PAGE-A','2013/10/03'
union all select 7,'C','PAGE-B','2013/10/05'
union all select 8,'D','PAGE-A','2013/10/01'
union all select 9,'D','PAGE-A','2013/10/09'
union all select 10,'D','PAGE-B','2013/10/11'
)
select *
from
(
select [USER],MAX(case when ACCESS_PAGE='PAGE-A' then ACCESS_DATE_TIME else null end) as ACCESS_PAGE_A_TIME
,MAX(case when ACCESS_PAGE='PAGE-B' then ACCESS_DATE_TIME else null end) as ACCESS_PAGE_B_TIME
from cte
group by [USER]
)t
where ACCESS_PAGE_A_TIME is not null and ACCESS_PAGE_B_TIME is not null
/*
USER ACCESS_PAGE_A_TIME ACCESS_PAGE_B_TIME
A 2013/10/03 2013/10/02
D 2013/10/09 2013/10/11
*/
select *
from
(
select *,rn=ROW_NUMBER() over(order by ACCESS_DATE_TIME desc) from TB_DataPlat
)t
where rn=1
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-17 16:16:52
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int,[USER] varchar(1),[ACCESS_PAGE] varchar(6),[ACCESS_DATE_TIME] datetime)
insert [huang]
select 1,'A','PAGE-A','2013/10/01' union all
select 2,'A','PAGE-A','2013/10/02' union all
select 3,'A','PAGE-A','2013/10/03' union all
select 4,'A','PAGE-B','2013/10/01' union all
select 5,'A','PAGE-B','2013/10/02' union all
select 6,'B','PAGE-A','2013/10/03' union all
select 7,'C','PAGE-B','2013/10/05' union all
select 8,'D','PAGE-A','2013/10/01' union all
select 9,'D','PAGE-A','2013/10/09' union all
select 10,'D','PAGE-B','2013/10/11'
--------------开始查询--------------------------
select [user],MAX(CASE WHEN [ACCESS_PAGE]='PAGE-A' THEN [ACCESS_DATE_TIME] ELSE NULL END ) [ACCESS_PAGE_A_TIME]
,MAX(CASE WHEN [ACCESS_PAGE]='PAGE-B' THEN [ACCESS_DATE_TIME] ELSE NULL END ) [ACCESS_PAGE_B_TIME]
from [huang] a
WHERE EXISTS (SELECT 1 FROM huang b WHERE a.[USER]=b.[USER] AND b.access_page='PAGE-A')
AND EXISTS (SELECT 1 FROM huang c WHERE a.[USER]=c.[USER] AND c.access_page='PAGE-B')
GROUP BY [user]
----------------结果----------------------------
/*
user ACCESS_PAGE_A_TIME ACCESS_PAGE_B_TIME
---- ----------------------- -----------------------
A 2013-10-03 00:00:00.000 2013-10-02 00:00:00.000
D 2013-10-09 00:00:00.000 2013-10-11 00:00:00.000
*/