我有一张表TBL_Student
:
RollNo Name Subject Month Year Marks Start_Date End_Date
---------------------------------------------------------------------
1 A MATH JAN 2016 65 01-Jan-16 31-Jan-16
1 A MATH FEB 2016 65 01-Feb-16 29-Feb-16
1 A MATH MAR 2016 70 01-Mar-16 31-Mar-16
1 A MATH APR 2016 65 01-Apr-16 30-Apr-16
1 A MATH MAY 2016 65 01-May-16 31-May-16
1 A MATH JUN 2016 65 01-Jun-16 30-Jun-16
1 A MATH JUL 2016 82 01-Jul-16 31-Jul-16
1 A MATH AUG 2016 70 01-Aug-16 31-Aug-16
1 A MATH SEP 2016 70 01-Sep-16 30-Sep-16
1 A MATH OCT 2016 56 01-Oct-16 31-Oct-16
1 A MATH NOV 2016 65 01-Nov-16 30-Nov-16
1 A MATH DEC 2016 65 01-Dec-16 31-Dec-16
1 A MATH JAN 2017 86 01-Jan-17 31-Jan-17
我需要这样的输出
RollNo Name Subject Marks Start_Date End_Date
-----------------------------------------------------
1 A MATH 65 01-Jan-16 29-Feb-16
1 A MATH 70 01-Mar-16 31-Mar-16
1 A MATH 65 01-Apr-16 30-Jun-16
1 A MATH 82 01-Jul-16 31-Jul-16
1 A MATH 70 01-Aug-16 30-Sep-16
1 A MATH 56 01-Oct-16 31-Oct-16
1 A MATH 65 01-Nov-16 31-Dec-16
1 A MATH 86 01-Jan-17 31-Jan-17
如果连续标记相同,则它将出现一次,但是第一个标记的开始日期和最后一个标记结束。
到目前为止我尝试过的,但是没有工作:
SELECT
RollNO, Name, Subject, Marks,
Start_Date, End_Date
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY Start_Date ASC) RNUM,
RollNO, Name, Subject, Marks,
Start_Date, End_Date
FROM
TBL_Student
WHERE
RollNo = 1
AND Subject = 'MATH') A
答案 0 :(得分:0)
试试这个:
select
rollNo, Name, subject, marks,
min(start_date) start_date, max(end_date) end_date
from (
select
t.*, sum(x) over (partition by rollNo, subject order by start_date) grp
from (
select
t.*, case when marks = lag(marks,1,marks) over
(partition by rollNo, subject order by start_date)
then 0 else 1 end x
from your_table t
) t
) t
group by rollNo, Name, subject, marks, grp;
它根据标记和start_date创建组,然后找到所需的聚合。