我有一个包含4行特殊情况的表...并且他们有出席
like : formate(mm/dd/rrrr)
2列名称
atten date : | strtdate
1) 4/02/2016 | 1) 4/02/2016
2) 4/03/2016 | 2) 4/02/2016
3) 4/05/2016 | 3) 4/02/2016
4) 4/ 07/2016 | 4) 4/02/2016
5) 4/08/2016 | 5) 4/02/2016
这里我需要这样的o / p栏
fromdt | todate
1) 4/02/2016 | 4/03/2016
2) 4/03/2016 | 4/05/2016
3) 4/05/2016 | 4/05/2016
4) 4/07/2016 | 4/08/2016
答案 0 :(得分:0)
您可以使用LEAD()
分析函数
with sample_data as (select 1 emp_id, to_date('02/04/2016', 'dd/mm/yyyy') atten_date, to_date('02/04/2016', 'dd/mm/yyyy') strtdate from dual union all
select 1 emp_id, to_date('03/04/2016', 'dd/mm/yyyy') atten_date, to_date('02/04/2016', 'dd/mm/yyyy') strtdate from dual union all
select 1 emp_id, to_date('05/04/2016', 'dd/mm/yyyy') atten_date, to_date('02/04/2016', 'dd/mm/yyyy') strtdate from dual union all
select 1 emp_id, to_date('07/04/2016', 'dd/mm/yyyy') atten_date, to_date('02/04/2016', 'dd/mm/yyyy') strtdate from dual union all
select 1 emp_id, to_date('08/04/2016', 'dd/mm/yyyy') atten_date, to_date('02/04/2016', 'dd/mm/yyyy') strtdate from dual)
-- end of subquery mimicking a table called "sample_data" with data in it. See SQL below:
select emp_id,
fromdt,
todate
from (select emp_id,
atten_date fromdt,
lead(atten_date) over (partition by emp_id order by atten_date) todate
from sample_data)
where todate is not null;
EMP_ID FROMDT TODATE
---------- ---------- ----------
1 04/02/2016 04/03/2016
1 04/03/2016 04/05/2016
1 04/05/2016 04/07/2016
1 04/07/2016 04/08/2016
如果您需要显示行号,那么您可以使用ROW_NUMBER()
分析函数来执行此操作,如果该信息尚未作为源数据的一部分提供。
答案 1 :(得分:0)
如果完全理解您的要求,此解决方案可能适合您:
with sample_data as
(
select 10 emp_id, to_date('4/02/2016','mm/dd/yyyy') attendate, to_date('4/02/2016','mm/dd/yyyy') strtdate from dual union all
select 10 emp_id, to_date('4/03/2016','mm/dd/yyyy') attendate, to_date('4/02/2016','mm/dd/yyyy') strtdate from dual union all
select 10 emp_id, to_date('4/05/2016','mm/dd/yyyy') attendate, to_date('4/02/2016','mm/dd/yyyy') strtdate from dual union all
select 10 emp_id, to_date('4/07/2016','mm/dd/yyyy') attendate, to_date('4/02/2016','mm/dd/yyyy') strtdate from dual union all
select 10 emp_id, to_date('4/08/2016','mm/dd/yyyy') attendate, to_date('4/02/2016','mm/dd/yyyy') strtdate from dual
)
, marked_data as
(
select emp_id
, attendate
, strtdate
, row_number() over (partition by emp_id, strtdate order by attendate) as is_from
, row_number() over (partition by emp_id, strtdate order by attendate) - 1 as is_to
from sample_data
)
select md1.emp_id
, md1.attendate as fromdt
, md2.attendate as todt
, md1.strtdate
from marked_data md1
join marked_data md2
on md1.emp_id = md2.emp_id
and md1.strtdate = md2.strtdate
and md1.is_from = md2.is_to
你将得到结果:
EMP_ID FROMDT TODT STRTDATE
1 10 4/2/2016 4/3/2016 4/2/2016
2 10 4/3/2016 4/5/2016 4/2/2016
3 10 4/5/2016 4/7/2016 4/2/2016
4 10 4/7/2016 4/8/2016 4/2/2016