我在员工表上工作,员工登录并在一天内注销。这些登录和注销都是使用一些代码进行捕获,并说“我是”。并且' O'。在我们的数据库中,员工可以在注销前多次登录,反之亦然。 示例数据如下所示。为了理解数据,我在样本数据中保留了一些空间。
empid terminal prd cdts unique_id logflag 10025 system1 C 20140129025854CS 66D47E9841E1C149947A8D28B32BFF98 I 10025 system1 C 20140129031601CS 85268C4FF5523D4882DE492CC0716F79 O 10025 system1 C 20140129031849CS 501F181C0138C041B25F4BAFFC4FE9B4 I 10025 system1 C 20140129035014CS 209CF069D0A3A242871D186D431 I
10025 system2 C 20140129035214CS 1248A63B50B1C244A8F21D9FD337F839 I 10025 system2 C 20140129040748CS 9A15F65724F0C04B93B42252BB3C50E3 O 10025 system2 C 20140129041636CS 41FB8A9D48B5C4489A4BC95F343613CC O
10025 system2 D 20140129044217CS 917054DE757CAC40A97A3E714BEA9CD1 I 10025 system2 D 20140129035214CS 1248A63B50B1C244A8F21D9FD337F839 O 10025 system2 D 20140129040748CS 9A15F65724F0C04B93B42252BB3C50E3 O 10025 system2 D 20140129041636CS 41FB8A9D48B5C4489A4BC95F343613CC I
empid terminal prd cdts unique_id logflag 10025 system1 C 20140129025854CS 66D47E9841E1C149947A8D28B32BFF98 I 10025 system1 C 20140129031601CS 85268C4FF5523D4882DE492CC0716F79 O 10025 system1 C 20140129031849CS 501F181C0138C041B25F4BAFFC4FE9B4 I
10025 system2 C 20140129035214CS 1248A63B50B1C244A8F21D9FD337F839 I 10025 system2 C 20140129040748CS 9A15F65724F0C04B93B42252BB3C50E3 O
10025 system2 D 20140129044217CS 917054DE757CAC40A97A3E714BEA9CD1 I 10025 system2 D 20140129035214CS 1248A63B50B1C244A8F21D9FD337F839 O 10025 system2 D 20140129041636CS 41FB8A9D48B5C4489A4BC95F343613CC I 10025 system2 D 20140129044217CS 917054DE757CAC40A97A3E714BEA9CD1 O 像智者我有数百万的记录。我的任务是为每个员工我应该捕获第一次登录并按顺序首次注销。对于上面的例子,我应该得到4条记录作为结果集,提到他的登录和注销。
这里前3列(empid,terminal,prd)需要参与分区。用语言员工进入终端并使用不同的 prd 。所以我想抓住每个员工的第一次登录和拳头注销到每个系统并使用每个prd 。
我怎样才能做到这一点。我完全被封锁了。请帮忙
答案 0 :(得分:0)
SELECT cdts,
empid,
logflag,
unique_id
FROM (
SELECT t.*,
ROW_NUMBER() OVER ( PARTITION BY empid, logflag, group_id ORDER BY cdts ) AS rn
FROM (
SELECT t.*
ROW_NUMBER() OVER ( PARTITION BY empid ORDER BY cdts )
- ROW_NUMBER() OVER ( PARTITION BY empid, logflag ORDER BY cdts )
AS group_id
FROM your_table t
) t
)
WHERE rn = 1;
答案 1 :(得分:0)
答案 2 :(得分:0)
非常感谢你的指导。最后,我可以通过此查询获得所需的结果。
select CDTS,empid,cterm,prd,logflag,unique_id,FLAG from
(
select CDTS,empid,cterm,prd,logflag,unique_id, case when logflag = prev_logflag then 1 else 0 end as flag from
(
select CDTS,empid,cterm,prd,logflag,unique_id,lag(logflag) over(partition by empid,cterm order by prd,cdts) as prev_logflag from emp
)T
)T1 where flag =0