How collpase all subgroups into one line and keep the same ordersql union with an aggregation componentHow do I limit the number of rows returned by an Oracle query after ordering?Why does this left join evaluate to a cross join?ORDER BY 2 values that might be NULLHow to import an SQL file using the command line in MySQL?How to order an already ordered subquerySQL - Update Record Based on Entity's Previous RecordOracle Find and Rewrite Consecutive RowsDay Number In a PatternHow to calculate number of Orders for given date range

What does "frozen" mean (e.g. for catcodes)?

What factors could lead to bishops establishing monastic armies?

What does "spinning upon the shoals" mean?

What is the shape of the upper boundary of water hitting a screen?

This LM317 diagram doesn't make any sense to me

Diagram with cylinder shapes and rectangles

Why is a mixture of two normally distributed variables only bimodal if their means differ by at least two times the common standard deviation?

Why am I getting unevenly-spread results when using $RANDOM?

Why do airports remove/realign runways?

Four ships at the ocean with the same distance

When is one 'Ready' to make Original Contributions to Mathematics?

Does the Wild Magic sorcerer's Tides of Chaos feature grant advantage on all attacks, or just the first one?

How to have a filled pattern

Possibility to correct pitch from digital versions of records with the hole not centered

Why do people prefer metropolitan areas, considering monsters and villains?

As a supervisor, what feedback would you expect from a PhD who quits?

Intern not wearing safety equipment; how could I have handled this differently?

Shipped package arrived - didn't order, possible scam?

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant

Uniform initialization by tuple

How to deal with account scam and fraud?

The Apéry's constant and the Airy function

Array or vector? Two dimensional array or matrix?



How collpase all subgroups into one line and keep the same order


sql union with an aggregation componentHow do I limit the number of rows returned by an Oracle query after ordering?Why does this left join evaluate to a cross join?ORDER BY 2 values that might be NULLHow to import an SQL file using the command line in MySQL?How to order an already ordered subquerySQL - Update Record Based on Entity's Previous RecordOracle Find and Rewrite Consecutive RowsDay Number In a PatternHow to calculate number of Orders for given date range






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















This is a simplified version of my table



+----+----------+------------+------------+
| ID | Category | Start Date | End Date |
+----+----------+------------+------------+
| 1 | 'Alpha' | 2018/04/12 | 2018/04/15 |
| 2 | null | 2018/04/17 | 2018/04/21 |
| 3 | 'Gamma' | 2018/05/02 | 2018/05/07 |
| 4 | 'Gamma' | 2018/05/09 | 2018/05/11 |
| 5 | 'Gamma' | 2018/05/11 | 2018/05/17 |
| 6 | 'Alpha' | 2018/05/17 | 2018/05/23 |
| 7 | 'Alpha' | 2018/05/23 | 2018/05/24 |
| 8 | null | 2018/05/24 | 2018/06/02 |
| 9 | 'Beta' | 2018/06/12 | 2018/06/16 |
| 10 | 'Beta' | 2018/06/16 | 2018/06/20 |
+----+----------+------------+------------+


All Start Date are unique, not nullable and they have the same order as the IDs (if a and b are IDs and a < b then StartDate[a] < StartDate[b]). The Start Date is not always equal to the End Date of the previous row for the same Category (look at id 3 and 4).



I'm looking for a query that will give me the following result



+----------+------------+------------+
| Category | Start Date | End Date |
+----------+------------+------------+
| 'Alpha' | 2018/04/12 | 2018/04/15 |
| null | 2018/04/17 | 2018/04/21 |
| 'Gamma' | 2018/05/02 | 2018/05/17 |
| 'Alpha' | 2018/05/17 | 2018/05/24 |
| null | 2018/05/24 | 2018/06/02 |
| 'Beta' | 2018/06/12 | 2018/06/20 |
+----------+------------+------------+


Note: The End Date will be equal to End Date of the last row in the subgroup (same continuous Category).










share|improve this question






























    1















    This is a simplified version of my table



    +----+----------+------------+------------+
    | ID | Category | Start Date | End Date |
    +----+----------+------------+------------+
    | 1 | 'Alpha' | 2018/04/12 | 2018/04/15 |
    | 2 | null | 2018/04/17 | 2018/04/21 |
    | 3 | 'Gamma' | 2018/05/02 | 2018/05/07 |
    | 4 | 'Gamma' | 2018/05/09 | 2018/05/11 |
    | 5 | 'Gamma' | 2018/05/11 | 2018/05/17 |
    | 6 | 'Alpha' | 2018/05/17 | 2018/05/23 |
    | 7 | 'Alpha' | 2018/05/23 | 2018/05/24 |
    | 8 | null | 2018/05/24 | 2018/06/02 |
    | 9 | 'Beta' | 2018/06/12 | 2018/06/16 |
    | 10 | 'Beta' | 2018/06/16 | 2018/06/20 |
    +----+----------+------------+------------+


    All Start Date are unique, not nullable and they have the same order as the IDs (if a and b are IDs and a < b then StartDate[a] < StartDate[b]). The Start Date is not always equal to the End Date of the previous row for the same Category (look at id 3 and 4).



    I'm looking for a query that will give me the following result



    +----------+------------+------------+
    | Category | Start Date | End Date |
    +----------+------------+------------+
    | 'Alpha' | 2018/04/12 | 2018/04/15 |
    | null | 2018/04/17 | 2018/04/21 |
    | 'Gamma' | 2018/05/02 | 2018/05/17 |
    | 'Alpha' | 2018/05/17 | 2018/05/24 |
    | null | 2018/05/24 | 2018/06/02 |
    | 'Beta' | 2018/06/12 | 2018/06/20 |
    +----------+------------+------------+


    Note: The End Date will be equal to End Date of the last row in the subgroup (same continuous Category).










    share|improve this question


























      1












      1








      1








      This is a simplified version of my table



      +----+----------+------------+------------+
      | ID | Category | Start Date | End Date |
      +----+----------+------------+------------+
      | 1 | 'Alpha' | 2018/04/12 | 2018/04/15 |
      | 2 | null | 2018/04/17 | 2018/04/21 |
      | 3 | 'Gamma' | 2018/05/02 | 2018/05/07 |
      | 4 | 'Gamma' | 2018/05/09 | 2018/05/11 |
      | 5 | 'Gamma' | 2018/05/11 | 2018/05/17 |
      | 6 | 'Alpha' | 2018/05/17 | 2018/05/23 |
      | 7 | 'Alpha' | 2018/05/23 | 2018/05/24 |
      | 8 | null | 2018/05/24 | 2018/06/02 |
      | 9 | 'Beta' | 2018/06/12 | 2018/06/16 |
      | 10 | 'Beta' | 2018/06/16 | 2018/06/20 |
      +----+----------+------------+------------+


      All Start Date are unique, not nullable and they have the same order as the IDs (if a and b are IDs and a < b then StartDate[a] < StartDate[b]). The Start Date is not always equal to the End Date of the previous row for the same Category (look at id 3 and 4).



      I'm looking for a query that will give me the following result



      +----------+------------+------------+
      | Category | Start Date | End Date |
      +----------+------------+------------+
      | 'Alpha' | 2018/04/12 | 2018/04/15 |
      | null | 2018/04/17 | 2018/04/21 |
      | 'Gamma' | 2018/05/02 | 2018/05/17 |
      | 'Alpha' | 2018/05/17 | 2018/05/24 |
      | null | 2018/05/24 | 2018/06/02 |
      | 'Beta' | 2018/06/12 | 2018/06/20 |
      +----------+------------+------------+


      Note: The End Date will be equal to End Date of the last row in the subgroup (same continuous Category).










      share|improve this question
















      This is a simplified version of my table



      +----+----------+------------+------------+
      | ID | Category | Start Date | End Date |
      +----+----------+------------+------------+
      | 1 | 'Alpha' | 2018/04/12 | 2018/04/15 |
      | 2 | null | 2018/04/17 | 2018/04/21 |
      | 3 | 'Gamma' | 2018/05/02 | 2018/05/07 |
      | 4 | 'Gamma' | 2018/05/09 | 2018/05/11 |
      | 5 | 'Gamma' | 2018/05/11 | 2018/05/17 |
      | 6 | 'Alpha' | 2018/05/17 | 2018/05/23 |
      | 7 | 'Alpha' | 2018/05/23 | 2018/05/24 |
      | 8 | null | 2018/05/24 | 2018/06/02 |
      | 9 | 'Beta' | 2018/06/12 | 2018/06/16 |
      | 10 | 'Beta' | 2018/06/16 | 2018/06/20 |
      +----+----------+------------+------------+


      All Start Date are unique, not nullable and they have the same order as the IDs (if a and b are IDs and a < b then StartDate[a] < StartDate[b]). The Start Date is not always equal to the End Date of the previous row for the same Category (look at id 3 and 4).



      I'm looking for a query that will give me the following result



      +----------+------------+------------+
      | Category | Start Date | End Date |
      +----------+------------+------------+
      | 'Alpha' | 2018/04/12 | 2018/04/15 |
      | null | 2018/04/17 | 2018/04/21 |
      | 'Gamma' | 2018/05/02 | 2018/05/17 |
      | 'Alpha' | 2018/05/17 | 2018/05/24 |
      | null | 2018/05/24 | 2018/06/02 |
      | 'Beta' | 2018/06/12 | 2018/06/20 |
      +----------+------------+------------+


      Note: The End Date will be equal to End Date of the last row in the subgroup (same continuous Category).







      sql oracle gaps-and-islands






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 21:45









      Barbaros Özhan

      18.6k7 gold badges16 silver badges35 bronze badges




      18.6k7 gold badges16 silver badges35 bronze badges










      asked Mar 25 at 21:37









      Dominique FortinDominique Fortin

      1,6658 silver badges17 bronze badges




      1,6658 silver badges17 bronze badges






















          2 Answers
          2






          active

          oldest

          votes


















          1














          This is a gaps-and-islands problem. I think you can use the difference of row numbers:



          select category, min(startdate), max(enddate)
          from (select t.*,
          row_number() over (order by id) as seqnum,
          row_number() over (partition by category order by id) as seqnum_c
          from t
          ) t
          group by category, (seqnum - seqnum_c)
          order by min(startdate);





          share|improve this answer























          • I'm surprised how simple it is.

            – Dominique Fortin
            Mar 25 at 22:28


















          0














          This is a gaps and islands question, you can use such a logic below



          select category, min(start_date) as start_date, max(end_date) as end_date
          from
          (
          select tt.*, sum(grp) over (order by id, start_date) sm
          from
          (
          with t( ID, Category, Start_Date, End_Date) as
          (
          select 1 , 'Alpha' , date'2018-04-12',date'2018-04-15' from dual union all
          select 2 , null , date'2018-04-17',date'2018-04-21' from dual union all
          select 3 , 'Gamma' , date'2018-05-02',date'2018-05-07' from dual union all
          select 4 , 'Gamma' , date'2018-05-09',date'2018-05-11' from dual union all
          select 5 , 'Gamma' , date'2018-05-11',date'2018-05-17' from dual union all
          select 6 , 'Alpha' , date'2018-05-17',date'2018-05-23' from dual union all
          select 7 , 'Alpha' , date'2018-05-23',date'2018-05-24' from dual union all
          select 8 , null , date'2018-05-24',date'2018-06-02' from dual union all
          select 9 , 'Beta' , date'2018-06-12',date'2018-06-16' from dual union all
          select 10 , 'Beta' , date'2018-06-16',date'2018-06-20' from dual
          )
          select id, Category,
          decode(nvl(lag(end_date) over
          (order by end_date),start_date),start_date,0,1)
          as grp, --> means prev. value equals or not
          row_number() over (order by id, end_date) as rn, start_date, end_date
          from t
          ) tt
          order by rn
          )
          group by Category, sm
          order by end_date;

          CATEGORY START_DATE END_DATE
          Alpha 12.04.2018 15.04.2018
          NULL 17.04.2018 21.04.2018
          Gamma 02.05.2018 07.05.2018
          Gamma 09.05.2018 17.05.2018
          Alpha 17.05.2018 24.05.2018
          NULL 24.05.2018 02.06.2018
          Beta 12.06.2018 20.06.2018





          share|improve this answer

























          • How will you get the second Alpha row?

            – Dominique Fortin
            Mar 25 at 21:43











          • @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

            – Barbaros Özhan
            Mar 25 at 21:44











          • There shouldn't be 2 Gamma rows in the result.

            – Dominique Fortin
            Mar 25 at 22:14












          • @DominiqueFortin I know, but couldn't figure out yet :)

            – Barbaros Özhan
            Mar 25 at 22:15













          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55346785%2fhow-collpase-all-subgroups-into-one-line-and-keep-the-same-order%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This is a gaps-and-islands problem. I think you can use the difference of row numbers:



          select category, min(startdate), max(enddate)
          from (select t.*,
          row_number() over (order by id) as seqnum,
          row_number() over (partition by category order by id) as seqnum_c
          from t
          ) t
          group by category, (seqnum - seqnum_c)
          order by min(startdate);





          share|improve this answer























          • I'm surprised how simple it is.

            – Dominique Fortin
            Mar 25 at 22:28















          1














          This is a gaps-and-islands problem. I think you can use the difference of row numbers:



          select category, min(startdate), max(enddate)
          from (select t.*,
          row_number() over (order by id) as seqnum,
          row_number() over (partition by category order by id) as seqnum_c
          from t
          ) t
          group by category, (seqnum - seqnum_c)
          order by min(startdate);





          share|improve this answer























          • I'm surprised how simple it is.

            – Dominique Fortin
            Mar 25 at 22:28













          1












          1








          1







          This is a gaps-and-islands problem. I think you can use the difference of row numbers:



          select category, min(startdate), max(enddate)
          from (select t.*,
          row_number() over (order by id) as seqnum,
          row_number() over (partition by category order by id) as seqnum_c
          from t
          ) t
          group by category, (seqnum - seqnum_c)
          order by min(startdate);





          share|improve this answer













          This is a gaps-and-islands problem. I think you can use the difference of row numbers:



          select category, min(startdate), max(enddate)
          from (select t.*,
          row_number() over (order by id) as seqnum,
          row_number() over (partition by category order by id) as seqnum_c
          from t
          ) t
          group by category, (seqnum - seqnum_c)
          order by min(startdate);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 22:06









          Gordon LinoffGordon Linoff

          829k38 gold badges339 silver badges445 bronze badges




          829k38 gold badges339 silver badges445 bronze badges












          • I'm surprised how simple it is.

            – Dominique Fortin
            Mar 25 at 22:28

















          • I'm surprised how simple it is.

            – Dominique Fortin
            Mar 25 at 22:28
















          I'm surprised how simple it is.

          – Dominique Fortin
          Mar 25 at 22:28





          I'm surprised how simple it is.

          – Dominique Fortin
          Mar 25 at 22:28













          0














          This is a gaps and islands question, you can use such a logic below



          select category, min(start_date) as start_date, max(end_date) as end_date
          from
          (
          select tt.*, sum(grp) over (order by id, start_date) sm
          from
          (
          with t( ID, Category, Start_Date, End_Date) as
          (
          select 1 , 'Alpha' , date'2018-04-12',date'2018-04-15' from dual union all
          select 2 , null , date'2018-04-17',date'2018-04-21' from dual union all
          select 3 , 'Gamma' , date'2018-05-02',date'2018-05-07' from dual union all
          select 4 , 'Gamma' , date'2018-05-09',date'2018-05-11' from dual union all
          select 5 , 'Gamma' , date'2018-05-11',date'2018-05-17' from dual union all
          select 6 , 'Alpha' , date'2018-05-17',date'2018-05-23' from dual union all
          select 7 , 'Alpha' , date'2018-05-23',date'2018-05-24' from dual union all
          select 8 , null , date'2018-05-24',date'2018-06-02' from dual union all
          select 9 , 'Beta' , date'2018-06-12',date'2018-06-16' from dual union all
          select 10 , 'Beta' , date'2018-06-16',date'2018-06-20' from dual
          )
          select id, Category,
          decode(nvl(lag(end_date) over
          (order by end_date),start_date),start_date,0,1)
          as grp, --> means prev. value equals or not
          row_number() over (order by id, end_date) as rn, start_date, end_date
          from t
          ) tt
          order by rn
          )
          group by Category, sm
          order by end_date;

          CATEGORY START_DATE END_DATE
          Alpha 12.04.2018 15.04.2018
          NULL 17.04.2018 21.04.2018
          Gamma 02.05.2018 07.05.2018
          Gamma 09.05.2018 17.05.2018
          Alpha 17.05.2018 24.05.2018
          NULL 24.05.2018 02.06.2018
          Beta 12.06.2018 20.06.2018





          share|improve this answer

























          • How will you get the second Alpha row?

            – Dominique Fortin
            Mar 25 at 21:43











          • @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

            – Barbaros Özhan
            Mar 25 at 21:44











          • There shouldn't be 2 Gamma rows in the result.

            – Dominique Fortin
            Mar 25 at 22:14












          • @DominiqueFortin I know, but couldn't figure out yet :)

            – Barbaros Özhan
            Mar 25 at 22:15















          0














          This is a gaps and islands question, you can use such a logic below



          select category, min(start_date) as start_date, max(end_date) as end_date
          from
          (
          select tt.*, sum(grp) over (order by id, start_date) sm
          from
          (
          with t( ID, Category, Start_Date, End_Date) as
          (
          select 1 , 'Alpha' , date'2018-04-12',date'2018-04-15' from dual union all
          select 2 , null , date'2018-04-17',date'2018-04-21' from dual union all
          select 3 , 'Gamma' , date'2018-05-02',date'2018-05-07' from dual union all
          select 4 , 'Gamma' , date'2018-05-09',date'2018-05-11' from dual union all
          select 5 , 'Gamma' , date'2018-05-11',date'2018-05-17' from dual union all
          select 6 , 'Alpha' , date'2018-05-17',date'2018-05-23' from dual union all
          select 7 , 'Alpha' , date'2018-05-23',date'2018-05-24' from dual union all
          select 8 , null , date'2018-05-24',date'2018-06-02' from dual union all
          select 9 , 'Beta' , date'2018-06-12',date'2018-06-16' from dual union all
          select 10 , 'Beta' , date'2018-06-16',date'2018-06-20' from dual
          )
          select id, Category,
          decode(nvl(lag(end_date) over
          (order by end_date),start_date),start_date,0,1)
          as grp, --> means prev. value equals or not
          row_number() over (order by id, end_date) as rn, start_date, end_date
          from t
          ) tt
          order by rn
          )
          group by Category, sm
          order by end_date;

          CATEGORY START_DATE END_DATE
          Alpha 12.04.2018 15.04.2018
          NULL 17.04.2018 21.04.2018
          Gamma 02.05.2018 07.05.2018
          Gamma 09.05.2018 17.05.2018
          Alpha 17.05.2018 24.05.2018
          NULL 24.05.2018 02.06.2018
          Beta 12.06.2018 20.06.2018





          share|improve this answer

























          • How will you get the second Alpha row?

            – Dominique Fortin
            Mar 25 at 21:43











          • @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

            – Barbaros Özhan
            Mar 25 at 21:44











          • There shouldn't be 2 Gamma rows in the result.

            – Dominique Fortin
            Mar 25 at 22:14












          • @DominiqueFortin I know, but couldn't figure out yet :)

            – Barbaros Özhan
            Mar 25 at 22:15













          0












          0








          0







          This is a gaps and islands question, you can use such a logic below



          select category, min(start_date) as start_date, max(end_date) as end_date
          from
          (
          select tt.*, sum(grp) over (order by id, start_date) sm
          from
          (
          with t( ID, Category, Start_Date, End_Date) as
          (
          select 1 , 'Alpha' , date'2018-04-12',date'2018-04-15' from dual union all
          select 2 , null , date'2018-04-17',date'2018-04-21' from dual union all
          select 3 , 'Gamma' , date'2018-05-02',date'2018-05-07' from dual union all
          select 4 , 'Gamma' , date'2018-05-09',date'2018-05-11' from dual union all
          select 5 , 'Gamma' , date'2018-05-11',date'2018-05-17' from dual union all
          select 6 , 'Alpha' , date'2018-05-17',date'2018-05-23' from dual union all
          select 7 , 'Alpha' , date'2018-05-23',date'2018-05-24' from dual union all
          select 8 , null , date'2018-05-24',date'2018-06-02' from dual union all
          select 9 , 'Beta' , date'2018-06-12',date'2018-06-16' from dual union all
          select 10 , 'Beta' , date'2018-06-16',date'2018-06-20' from dual
          )
          select id, Category,
          decode(nvl(lag(end_date) over
          (order by end_date),start_date),start_date,0,1)
          as grp, --> means prev. value equals or not
          row_number() over (order by id, end_date) as rn, start_date, end_date
          from t
          ) tt
          order by rn
          )
          group by Category, sm
          order by end_date;

          CATEGORY START_DATE END_DATE
          Alpha 12.04.2018 15.04.2018
          NULL 17.04.2018 21.04.2018
          Gamma 02.05.2018 07.05.2018
          Gamma 09.05.2018 17.05.2018
          Alpha 17.05.2018 24.05.2018
          NULL 24.05.2018 02.06.2018
          Beta 12.06.2018 20.06.2018





          share|improve this answer















          This is a gaps and islands question, you can use such a logic below



          select category, min(start_date) as start_date, max(end_date) as end_date
          from
          (
          select tt.*, sum(grp) over (order by id, start_date) sm
          from
          (
          with t( ID, Category, Start_Date, End_Date) as
          (
          select 1 , 'Alpha' , date'2018-04-12',date'2018-04-15' from dual union all
          select 2 , null , date'2018-04-17',date'2018-04-21' from dual union all
          select 3 , 'Gamma' , date'2018-05-02',date'2018-05-07' from dual union all
          select 4 , 'Gamma' , date'2018-05-09',date'2018-05-11' from dual union all
          select 5 , 'Gamma' , date'2018-05-11',date'2018-05-17' from dual union all
          select 6 , 'Alpha' , date'2018-05-17',date'2018-05-23' from dual union all
          select 7 , 'Alpha' , date'2018-05-23',date'2018-05-24' from dual union all
          select 8 , null , date'2018-05-24',date'2018-06-02' from dual union all
          select 9 , 'Beta' , date'2018-06-12',date'2018-06-16' from dual union all
          select 10 , 'Beta' , date'2018-06-16',date'2018-06-20' from dual
          )
          select id, Category,
          decode(nvl(lag(end_date) over
          (order by end_date),start_date),start_date,0,1)
          as grp, --> means prev. value equals or not
          row_number() over (order by id, end_date) as rn, start_date, end_date
          from t
          ) tt
          order by rn
          )
          group by Category, sm
          order by end_date;

          CATEGORY START_DATE END_DATE
          Alpha 12.04.2018 15.04.2018
          NULL 17.04.2018 21.04.2018
          Gamma 02.05.2018 07.05.2018
          Gamma 09.05.2018 17.05.2018
          Alpha 17.05.2018 24.05.2018
          NULL 24.05.2018 02.06.2018
          Beta 12.06.2018 20.06.2018






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 22:17

























          answered Mar 25 at 21:39









          Barbaros ÖzhanBarbaros Özhan

          18.6k7 gold badges16 silver badges35 bronze badges




          18.6k7 gold badges16 silver badges35 bronze badges












          • How will you get the second Alpha row?

            – Dominique Fortin
            Mar 25 at 21:43











          • @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

            – Barbaros Özhan
            Mar 25 at 21:44











          • There shouldn't be 2 Gamma rows in the result.

            – Dominique Fortin
            Mar 25 at 22:14












          • @DominiqueFortin I know, but couldn't figure out yet :)

            – Barbaros Özhan
            Mar 25 at 22:15

















          • How will you get the second Alpha row?

            – Dominique Fortin
            Mar 25 at 21:43











          • @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

            – Barbaros Özhan
            Mar 25 at 21:44











          • There shouldn't be 2 Gamma rows in the result.

            – Dominique Fortin
            Mar 25 at 22:14












          • @DominiqueFortin I know, but couldn't figure out yet :)

            – Barbaros Özhan
            Mar 25 at 22:15
















          How will you get the second Alpha row?

          – Dominique Fortin
          Mar 25 at 21:43





          How will you get the second Alpha row?

          – Dominique Fortin
          Mar 25 at 21:43













          @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

          – Barbaros Özhan
          Mar 25 at 21:44





          @DominiqueFortin oh ok, I didn't notice that this is a gaps-and-islands question

          – Barbaros Özhan
          Mar 25 at 21:44













          There shouldn't be 2 Gamma rows in the result.

          – Dominique Fortin
          Mar 25 at 22:14






          There shouldn't be 2 Gamma rows in the result.

          – Dominique Fortin
          Mar 25 at 22:14














          @DominiqueFortin I know, but couldn't figure out yet :)

          – Barbaros Özhan
          Mar 25 at 22:15





          @DominiqueFortin I know, but couldn't figure out yet :)

          – Barbaros Özhan
          Mar 25 at 22:15

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55346785%2fhow-collpase-all-subgroups-into-one-line-and-keep-the-same-order%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript