Date bound SQL group by












0















I have a dataset that looks like this:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-01 123 Currency GBP
2018-01-02 2018-01-02 123 Currency GBP
2018-01-03 2018-01-03 123 Currency USD
2018-01-04 2018-01-04 123 Currency USD
2018-01-05 2018-01-05 123 Currency GBP
2018-01-06 2018-01-06 123 Currency GBP


What I would like is to transform this dataset into a date bound dataset like below:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-02 123 Currency GBP
2018-01-03 2018-01-04 123 Currency USD
2018-01-05 2018-01-06 123 Currency GBP


I thought about writing the SQL like this:



SELECT
MIN(StartDate) AS StartDate
, MAX(EndDate) AS EndDate
, [InstrumentID]
, Dimension
, DimensionValue

FROM #Worktable
GROUP BY InstrumentID, Dimension, DimensionValue


However this obviously won't work as it will ignore the change in date for GBP and just group one record together with start date of 2018-01-01 and end date of 2018-01-06.



Is there a way in which I can do this and achieve the dates I require?



Thanks










share|improve this question























  • This is a very symmetrical source data set. What happens when it's not so nice? Date ranges overlap, DimensionValues overlap, StartDate <> EndDate, etc.

    – Aaron Bertrand
    Nov 24 '18 at 15:14


















0















I have a dataset that looks like this:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-01 123 Currency GBP
2018-01-02 2018-01-02 123 Currency GBP
2018-01-03 2018-01-03 123 Currency USD
2018-01-04 2018-01-04 123 Currency USD
2018-01-05 2018-01-05 123 Currency GBP
2018-01-06 2018-01-06 123 Currency GBP


What I would like is to transform this dataset into a date bound dataset like below:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-02 123 Currency GBP
2018-01-03 2018-01-04 123 Currency USD
2018-01-05 2018-01-06 123 Currency GBP


I thought about writing the SQL like this:



SELECT
MIN(StartDate) AS StartDate
, MAX(EndDate) AS EndDate
, [InstrumentID]
, Dimension
, DimensionValue

FROM #Worktable
GROUP BY InstrumentID, Dimension, DimensionValue


However this obviously won't work as it will ignore the change in date for GBP and just group one record together with start date of 2018-01-01 and end date of 2018-01-06.



Is there a way in which I can do this and achieve the dates I require?



Thanks










share|improve this question























  • This is a very symmetrical source data set. What happens when it's not so nice? Date ranges overlap, DimensionValues overlap, StartDate <> EndDate, etc.

    – Aaron Bertrand
    Nov 24 '18 at 15:14
















0












0








0








I have a dataset that looks like this:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-01 123 Currency GBP
2018-01-02 2018-01-02 123 Currency GBP
2018-01-03 2018-01-03 123 Currency USD
2018-01-04 2018-01-04 123 Currency USD
2018-01-05 2018-01-05 123 Currency GBP
2018-01-06 2018-01-06 123 Currency GBP


What I would like is to transform this dataset into a date bound dataset like below:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-02 123 Currency GBP
2018-01-03 2018-01-04 123 Currency USD
2018-01-05 2018-01-06 123 Currency GBP


I thought about writing the SQL like this:



SELECT
MIN(StartDate) AS StartDate
, MAX(EndDate) AS EndDate
, [InstrumentID]
, Dimension
, DimensionValue

FROM #Worktable
GROUP BY InstrumentID, Dimension, DimensionValue


However this obviously won't work as it will ignore the change in date for GBP and just group one record together with start date of 2018-01-01 and end date of 2018-01-06.



Is there a way in which I can do this and achieve the dates I require?



Thanks










share|improve this question














I have a dataset that looks like this:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-01 123 Currency GBP
2018-01-02 2018-01-02 123 Currency GBP
2018-01-03 2018-01-03 123 Currency USD
2018-01-04 2018-01-04 123 Currency USD
2018-01-05 2018-01-05 123 Currency GBP
2018-01-06 2018-01-06 123 Currency GBP


What I would like is to transform this dataset into a date bound dataset like below:



StartDate  EndDate    InstrumentID   Dimension   DimensionValue
2018-01-01 2018-01-02 123 Currency GBP
2018-01-03 2018-01-04 123 Currency USD
2018-01-05 2018-01-06 123 Currency GBP


I thought about writing the SQL like this:



SELECT
MIN(StartDate) AS StartDate
, MAX(EndDate) AS EndDate
, [InstrumentID]
, Dimension
, DimensionValue

FROM #Worktable
GROUP BY InstrumentID, Dimension, DimensionValue


However this obviously won't work as it will ignore the change in date for GBP and just group one record together with start date of 2018-01-01 and end date of 2018-01-06.



Is there a way in which I can do this and achieve the dates I require?



Thanks







sql sql-server tsql group-by






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 15:08









chrissy pchrissy p

41021237




41021237













  • This is a very symmetrical source data set. What happens when it's not so nice? Date ranges overlap, DimensionValues overlap, StartDate <> EndDate, etc.

    – Aaron Bertrand
    Nov 24 '18 at 15:14





















  • This is a very symmetrical source data set. What happens when it's not so nice? Date ranges overlap, DimensionValues overlap, StartDate <> EndDate, etc.

    – Aaron Bertrand
    Nov 24 '18 at 15:14



















This is a very symmetrical source data set. What happens when it's not so nice? Date ranges overlap, DimensionValues overlap, StartDate <> EndDate, etc.

– Aaron Bertrand
Nov 24 '18 at 15:14







This is a very symmetrical source data set. What happens when it's not so nice? Date ranges overlap, DimensionValues overlap, StartDate <> EndDate, etc.

– Aaron Bertrand
Nov 24 '18 at 15:14














5 Answers
5






active

oldest

votes


















2














This is a common Gaps and Islands question. There are plenty of examples out there on how to do this; for example:



WITH VTE AS(
SELECT CONVERT(date,StartDate) AS StartDate,
CONVERT(Date,EndDate) AS EndDate,
InstrumentID,
Dimension,
DimensionValue
FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
('20180102','20180102',123,'Currency','GBP'),
('20180103','20180103',123,'Currency','USD'),
('20180104','20180104',123,'Currency','USD'),
('20180105','20180105',123,'Currency','GBP'),
('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue)),
Grps AS (
SELECT StartDate,
EndDate,
InstrumentID,
Dimension,
DimensionValue,
ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension ORDER BY StartDate) -
ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension, DimensionValue ORDER BY StartDate) AS Grp
FROM VTE)
SELECT MIN(StartDate) AS StartDate,
MAX(EndDate) AS EndDate,
InstrumentID,
Dimension,
DimensionValue
FROM Grps
GROUP BY InstrumentID,
Dimension,
DimensionValue,
Grp
ORDER BY StartDate;





share|improve this answer

































    1














    This is a form of gaps-and-islands. But because there are start date and end dates, you need to be careful. I recommend lag() and cumulative sum:



    select InstrumentID, Dimension, DimensionValue,
    min(startdate) as startdate, max(enddate) as enddate
    from (select w.*,
    sum(case when prev_enddate = startdate then 0 else 1 end)
    over (partition by InstrumentID, Dimension,
    DimensionValue order by startdate) as grp
    from (select w.*,
    lag(enddate) over (partition by InstrumentID, Dimension, DimensionValue
    order by startdate) as prev_enddate
    from #worktable w
    ) w
    group by InstrumentID, Dimension, DimensionValue, grp
    order by InstrumentID, Dimension, DimensionValue, min(startdate);





    share|improve this answer

































      0














      You need to use dense rank like:



      with x as(
      select DENSE_RANK() OVER
      (PARTITION BY DimensionValue) AS Rank , *
      from Worktable
      ) select StartDate AS StartDate
      , EndDate AS EndDate
      , [InstrumentID]
      , Max(Dimension) AS Dimension
      , DimensionValue, Rank
      FROM x
      GROUP BY InstrumentID, StartDate, EndDate, DimensionValue,Rank





      share|improve this answer































        0














        Update, I just thought of this, I couldn't test it yet, I think it will work the way you want it to.



        Select StartDate, EndDate, InstrumentID, Dimension, DimensionValue From (
        SELECT
        StartDate AS StartDate
        , EndDate AS EndDate
        , [InstrumentID]
        , Dimension
        , DimensionValue
        , Count(*)
        FROM #Worktable
        GROUP BY InstrumentID, StartDate, EndDate, Dimension, DimensionValue) x


        Hope this helps!






        share|improve this answer


























        • This doesn't give the result the OP wants.

          – Larnu
          Nov 24 '18 at 15:19



















        0














        Try something like the following:



        WITH CTE AS(
        SELECT StartDate::DATE AS StartDate,
        EndDate::DATE AS EndDate,
        InstrumentID,
        Dimension,
        DimensionValue
        FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
        ('20180102','20180102',123,'Currency','GBP'),
        ('20180103','20180103',123,'Currency','USD'),
        ('20180104','20180104',123,'Currency','USD'),
        ('20180105','20180105',123,'Currency','GBP'),
        ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue))

        SELECT startdate
        , enddate
        , instrumentid
        , dimension
        , dimensionvalue
        FROM (
        SELECT *
        , CASE WHEN (LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) IS NULL) OR (enddate - LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) <> 1) THEN 0
        ELSE 1 END is_valid
        FROM CTE
        ) a
        WHERE is_valid = 1
        ORDER BY startdate;


        Credit to @Lamu for creating the temp table.






        share|improve this answer























          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%2f53459480%2fdate-bound-sql-group-by%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          This is a common Gaps and Islands question. There are plenty of examples out there on how to do this; for example:



          WITH VTE AS(
          SELECT CONVERT(date,StartDate) AS StartDate,
          CONVERT(Date,EndDate) AS EndDate,
          InstrumentID,
          Dimension,
          DimensionValue
          FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
          ('20180102','20180102',123,'Currency','GBP'),
          ('20180103','20180103',123,'Currency','USD'),
          ('20180104','20180104',123,'Currency','USD'),
          ('20180105','20180105',123,'Currency','GBP'),
          ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue)),
          Grps AS (
          SELECT StartDate,
          EndDate,
          InstrumentID,
          Dimension,
          DimensionValue,
          ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension ORDER BY StartDate) -
          ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension, DimensionValue ORDER BY StartDate) AS Grp
          FROM VTE)
          SELECT MIN(StartDate) AS StartDate,
          MAX(EndDate) AS EndDate,
          InstrumentID,
          Dimension,
          DimensionValue
          FROM Grps
          GROUP BY InstrumentID,
          Dimension,
          DimensionValue,
          Grp
          ORDER BY StartDate;





          share|improve this answer






























            2














            This is a common Gaps and Islands question. There are plenty of examples out there on how to do this; for example:



            WITH VTE AS(
            SELECT CONVERT(date,StartDate) AS StartDate,
            CONVERT(Date,EndDate) AS EndDate,
            InstrumentID,
            Dimension,
            DimensionValue
            FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
            ('20180102','20180102',123,'Currency','GBP'),
            ('20180103','20180103',123,'Currency','USD'),
            ('20180104','20180104',123,'Currency','USD'),
            ('20180105','20180105',123,'Currency','GBP'),
            ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue)),
            Grps AS (
            SELECT StartDate,
            EndDate,
            InstrumentID,
            Dimension,
            DimensionValue,
            ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension ORDER BY StartDate) -
            ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension, DimensionValue ORDER BY StartDate) AS Grp
            FROM VTE)
            SELECT MIN(StartDate) AS StartDate,
            MAX(EndDate) AS EndDate,
            InstrumentID,
            Dimension,
            DimensionValue
            FROM Grps
            GROUP BY InstrumentID,
            Dimension,
            DimensionValue,
            Grp
            ORDER BY StartDate;





            share|improve this answer




























              2












              2








              2







              This is a common Gaps and Islands question. There are plenty of examples out there on how to do this; for example:



              WITH VTE AS(
              SELECT CONVERT(date,StartDate) AS StartDate,
              CONVERT(Date,EndDate) AS EndDate,
              InstrumentID,
              Dimension,
              DimensionValue
              FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
              ('20180102','20180102',123,'Currency','GBP'),
              ('20180103','20180103',123,'Currency','USD'),
              ('20180104','20180104',123,'Currency','USD'),
              ('20180105','20180105',123,'Currency','GBP'),
              ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue)),
              Grps AS (
              SELECT StartDate,
              EndDate,
              InstrumentID,
              Dimension,
              DimensionValue,
              ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension ORDER BY StartDate) -
              ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension, DimensionValue ORDER BY StartDate) AS Grp
              FROM VTE)
              SELECT MIN(StartDate) AS StartDate,
              MAX(EndDate) AS EndDate,
              InstrumentID,
              Dimension,
              DimensionValue
              FROM Grps
              GROUP BY InstrumentID,
              Dimension,
              DimensionValue,
              Grp
              ORDER BY StartDate;





              share|improve this answer















              This is a common Gaps and Islands question. There are plenty of examples out there on how to do this; for example:



              WITH VTE AS(
              SELECT CONVERT(date,StartDate) AS StartDate,
              CONVERT(Date,EndDate) AS EndDate,
              InstrumentID,
              Dimension,
              DimensionValue
              FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
              ('20180102','20180102',123,'Currency','GBP'),
              ('20180103','20180103',123,'Currency','USD'),
              ('20180104','20180104',123,'Currency','USD'),
              ('20180105','20180105',123,'Currency','GBP'),
              ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue)),
              Grps AS (
              SELECT StartDate,
              EndDate,
              InstrumentID,
              Dimension,
              DimensionValue,
              ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension ORDER BY StartDate) -
              ROW_NUMBER() OVER (PARTITION BY InstrumentID, Dimension, DimensionValue ORDER BY StartDate) AS Grp
              FROM VTE)
              SELECT MIN(StartDate) AS StartDate,
              MAX(EndDate) AS EndDate,
              InstrumentID,
              Dimension,
              DimensionValue
              FROM Grps
              GROUP BY InstrumentID,
              Dimension,
              DimensionValue,
              Grp
              ORDER BY StartDate;






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 24 '18 at 18:50









              StepUp

              7,87784473




              7,87784473










              answered Nov 24 '18 at 15:18









              LarnuLarnu

              18.2k51731




              18.2k51731

























                  1














                  This is a form of gaps-and-islands. But because there are start date and end dates, you need to be careful. I recommend lag() and cumulative sum:



                  select InstrumentID, Dimension, DimensionValue,
                  min(startdate) as startdate, max(enddate) as enddate
                  from (select w.*,
                  sum(case when prev_enddate = startdate then 0 else 1 end)
                  over (partition by InstrumentID, Dimension,
                  DimensionValue order by startdate) as grp
                  from (select w.*,
                  lag(enddate) over (partition by InstrumentID, Dimension, DimensionValue
                  order by startdate) as prev_enddate
                  from #worktable w
                  ) w
                  group by InstrumentID, Dimension, DimensionValue, grp
                  order by InstrumentID, Dimension, DimensionValue, min(startdate);





                  share|improve this answer






























                    1














                    This is a form of gaps-and-islands. But because there are start date and end dates, you need to be careful. I recommend lag() and cumulative sum:



                    select InstrumentID, Dimension, DimensionValue,
                    min(startdate) as startdate, max(enddate) as enddate
                    from (select w.*,
                    sum(case when prev_enddate = startdate then 0 else 1 end)
                    over (partition by InstrumentID, Dimension,
                    DimensionValue order by startdate) as grp
                    from (select w.*,
                    lag(enddate) over (partition by InstrumentID, Dimension, DimensionValue
                    order by startdate) as prev_enddate
                    from #worktable w
                    ) w
                    group by InstrumentID, Dimension, DimensionValue, grp
                    order by InstrumentID, Dimension, DimensionValue, min(startdate);





                    share|improve this answer




























                      1












                      1








                      1







                      This is a form of gaps-and-islands. But because there are start date and end dates, you need to be careful. I recommend lag() and cumulative sum:



                      select InstrumentID, Dimension, DimensionValue,
                      min(startdate) as startdate, max(enddate) as enddate
                      from (select w.*,
                      sum(case when prev_enddate = startdate then 0 else 1 end)
                      over (partition by InstrumentID, Dimension,
                      DimensionValue order by startdate) as grp
                      from (select w.*,
                      lag(enddate) over (partition by InstrumentID, Dimension, DimensionValue
                      order by startdate) as prev_enddate
                      from #worktable w
                      ) w
                      group by InstrumentID, Dimension, DimensionValue, grp
                      order by InstrumentID, Dimension, DimensionValue, min(startdate);





                      share|improve this answer















                      This is a form of gaps-and-islands. But because there are start date and end dates, you need to be careful. I recommend lag() and cumulative sum:



                      select InstrumentID, Dimension, DimensionValue,
                      min(startdate) as startdate, max(enddate) as enddate
                      from (select w.*,
                      sum(case when prev_enddate = startdate then 0 else 1 end)
                      over (partition by InstrumentID, Dimension,
                      DimensionValue order by startdate) as grp
                      from (select w.*,
                      lag(enddate) over (partition by InstrumentID, Dimension, DimensionValue
                      order by startdate) as prev_enddate
                      from #worktable w
                      ) w
                      group by InstrumentID, Dimension, DimensionValue, grp
                      order by InstrumentID, Dimension, DimensionValue, min(startdate);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 24 '18 at 18:39









                      StepUp

                      7,87784473




                      7,87784473










                      answered Nov 24 '18 at 15:24









                      Gordon LinoffGordon Linoff

                      776k35306409




                      776k35306409























                          0














                          You need to use dense rank like:



                          with x as(
                          select DENSE_RANK() OVER
                          (PARTITION BY DimensionValue) AS Rank , *
                          from Worktable
                          ) select StartDate AS StartDate
                          , EndDate AS EndDate
                          , [InstrumentID]
                          , Max(Dimension) AS Dimension
                          , DimensionValue, Rank
                          FROM x
                          GROUP BY InstrumentID, StartDate, EndDate, DimensionValue,Rank





                          share|improve this answer




























                            0














                            You need to use dense rank like:



                            with x as(
                            select DENSE_RANK() OVER
                            (PARTITION BY DimensionValue) AS Rank , *
                            from Worktable
                            ) select StartDate AS StartDate
                            , EndDate AS EndDate
                            , [InstrumentID]
                            , Max(Dimension) AS Dimension
                            , DimensionValue, Rank
                            FROM x
                            GROUP BY InstrumentID, StartDate, EndDate, DimensionValue,Rank





                            share|improve this answer


























                              0












                              0








                              0







                              You need to use dense rank like:



                              with x as(
                              select DENSE_RANK() OVER
                              (PARTITION BY DimensionValue) AS Rank , *
                              from Worktable
                              ) select StartDate AS StartDate
                              , EndDate AS EndDate
                              , [InstrumentID]
                              , Max(Dimension) AS Dimension
                              , DimensionValue, Rank
                              FROM x
                              GROUP BY InstrumentID, StartDate, EndDate, DimensionValue,Rank





                              share|improve this answer













                              You need to use dense rank like:



                              with x as(
                              select DENSE_RANK() OVER
                              (PARTITION BY DimensionValue) AS Rank , *
                              from Worktable
                              ) select StartDate AS StartDate
                              , EndDate AS EndDate
                              , [InstrumentID]
                              , Max(Dimension) AS Dimension
                              , DimensionValue, Rank
                              FROM x
                              GROUP BY InstrumentID, StartDate, EndDate, DimensionValue,Rank






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 24 '18 at 15:18









                              picklerickpicklerick

                              32918




                              32918























                                  0














                                  Update, I just thought of this, I couldn't test it yet, I think it will work the way you want it to.



                                  Select StartDate, EndDate, InstrumentID, Dimension, DimensionValue From (
                                  SELECT
                                  StartDate AS StartDate
                                  , EndDate AS EndDate
                                  , [InstrumentID]
                                  , Dimension
                                  , DimensionValue
                                  , Count(*)
                                  FROM #Worktable
                                  GROUP BY InstrumentID, StartDate, EndDate, Dimension, DimensionValue) x


                                  Hope this helps!






                                  share|improve this answer


























                                  • This doesn't give the result the OP wants.

                                    – Larnu
                                    Nov 24 '18 at 15:19
















                                  0














                                  Update, I just thought of this, I couldn't test it yet, I think it will work the way you want it to.



                                  Select StartDate, EndDate, InstrumentID, Dimension, DimensionValue From (
                                  SELECT
                                  StartDate AS StartDate
                                  , EndDate AS EndDate
                                  , [InstrumentID]
                                  , Dimension
                                  , DimensionValue
                                  , Count(*)
                                  FROM #Worktable
                                  GROUP BY InstrumentID, StartDate, EndDate, Dimension, DimensionValue) x


                                  Hope this helps!






                                  share|improve this answer


























                                  • This doesn't give the result the OP wants.

                                    – Larnu
                                    Nov 24 '18 at 15:19














                                  0












                                  0








                                  0







                                  Update, I just thought of this, I couldn't test it yet, I think it will work the way you want it to.



                                  Select StartDate, EndDate, InstrumentID, Dimension, DimensionValue From (
                                  SELECT
                                  StartDate AS StartDate
                                  , EndDate AS EndDate
                                  , [InstrumentID]
                                  , Dimension
                                  , DimensionValue
                                  , Count(*)
                                  FROM #Worktable
                                  GROUP BY InstrumentID, StartDate, EndDate, Dimension, DimensionValue) x


                                  Hope this helps!






                                  share|improve this answer















                                  Update, I just thought of this, I couldn't test it yet, I think it will work the way you want it to.



                                  Select StartDate, EndDate, InstrumentID, Dimension, DimensionValue From (
                                  SELECT
                                  StartDate AS StartDate
                                  , EndDate AS EndDate
                                  , [InstrumentID]
                                  , Dimension
                                  , DimensionValue
                                  , Count(*)
                                  FROM #Worktable
                                  GROUP BY InstrumentID, StartDate, EndDate, Dimension, DimensionValue) x


                                  Hope this helps!







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 24 '18 at 15:31

























                                  answered Nov 24 '18 at 15:16









                                  Boris JovanovicBoris Jovanovic

                                  437




                                  437













                                  • This doesn't give the result the OP wants.

                                    – Larnu
                                    Nov 24 '18 at 15:19



















                                  • This doesn't give the result the OP wants.

                                    – Larnu
                                    Nov 24 '18 at 15:19

















                                  This doesn't give the result the OP wants.

                                  – Larnu
                                  Nov 24 '18 at 15:19





                                  This doesn't give the result the OP wants.

                                  – Larnu
                                  Nov 24 '18 at 15:19











                                  0














                                  Try something like the following:



                                  WITH CTE AS(
                                  SELECT StartDate::DATE AS StartDate,
                                  EndDate::DATE AS EndDate,
                                  InstrumentID,
                                  Dimension,
                                  DimensionValue
                                  FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
                                  ('20180102','20180102',123,'Currency','GBP'),
                                  ('20180103','20180103',123,'Currency','USD'),
                                  ('20180104','20180104',123,'Currency','USD'),
                                  ('20180105','20180105',123,'Currency','GBP'),
                                  ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue))

                                  SELECT startdate
                                  , enddate
                                  , instrumentid
                                  , dimension
                                  , dimensionvalue
                                  FROM (
                                  SELECT *
                                  , CASE WHEN (LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) IS NULL) OR (enddate - LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) <> 1) THEN 0
                                  ELSE 1 END is_valid
                                  FROM CTE
                                  ) a
                                  WHERE is_valid = 1
                                  ORDER BY startdate;


                                  Credit to @Lamu for creating the temp table.






                                  share|improve this answer




























                                    0














                                    Try something like the following:



                                    WITH CTE AS(
                                    SELECT StartDate::DATE AS StartDate,
                                    EndDate::DATE AS EndDate,
                                    InstrumentID,
                                    Dimension,
                                    DimensionValue
                                    FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
                                    ('20180102','20180102',123,'Currency','GBP'),
                                    ('20180103','20180103',123,'Currency','USD'),
                                    ('20180104','20180104',123,'Currency','USD'),
                                    ('20180105','20180105',123,'Currency','GBP'),
                                    ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue))

                                    SELECT startdate
                                    , enddate
                                    , instrumentid
                                    , dimension
                                    , dimensionvalue
                                    FROM (
                                    SELECT *
                                    , CASE WHEN (LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) IS NULL) OR (enddate - LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) <> 1) THEN 0
                                    ELSE 1 END is_valid
                                    FROM CTE
                                    ) a
                                    WHERE is_valid = 1
                                    ORDER BY startdate;


                                    Credit to @Lamu for creating the temp table.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Try something like the following:



                                      WITH CTE AS(
                                      SELECT StartDate::DATE AS StartDate,
                                      EndDate::DATE AS EndDate,
                                      InstrumentID,
                                      Dimension,
                                      DimensionValue
                                      FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
                                      ('20180102','20180102',123,'Currency','GBP'),
                                      ('20180103','20180103',123,'Currency','USD'),
                                      ('20180104','20180104',123,'Currency','USD'),
                                      ('20180105','20180105',123,'Currency','GBP'),
                                      ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue))

                                      SELECT startdate
                                      , enddate
                                      , instrumentid
                                      , dimension
                                      , dimensionvalue
                                      FROM (
                                      SELECT *
                                      , CASE WHEN (LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) IS NULL) OR (enddate - LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) <> 1) THEN 0
                                      ELSE 1 END is_valid
                                      FROM CTE
                                      ) a
                                      WHERE is_valid = 1
                                      ORDER BY startdate;


                                      Credit to @Lamu for creating the temp table.






                                      share|improve this answer













                                      Try something like the following:



                                      WITH CTE AS(
                                      SELECT StartDate::DATE AS StartDate,
                                      EndDate::DATE AS EndDate,
                                      InstrumentID,
                                      Dimension,
                                      DimensionValue
                                      FROM (VALUES('20180101','20180101',123,'Currency','GBP'),
                                      ('20180102','20180102',123,'Currency','GBP'),
                                      ('20180103','20180103',123,'Currency','USD'),
                                      ('20180104','20180104',123,'Currency','USD'),
                                      ('20180105','20180105',123,'Currency','GBP'),
                                      ('20180106','20180106',123,'Currency','GBP')) V(StartDate,EndDate,InstrumentID,Dimension,DimensionValue))

                                      SELECT startdate
                                      , enddate
                                      , instrumentid
                                      , dimension
                                      , dimensionvalue
                                      FROM (
                                      SELECT *
                                      , CASE WHEN (LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) IS NULL) OR (enddate - LAG(enddate, 1) OVER(PARTITION BY dimensionvalue ORDER BY startdate) <> 1) THEN 0
                                      ELSE 1 END is_valid
                                      FROM CTE
                                      ) a
                                      WHERE is_valid = 1
                                      ORDER BY startdate;


                                      Credit to @Lamu for creating the temp table.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 24 '18 at 20:14









                                      Newbie92Newbie92

                                      83




                                      83






























                                          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%2f53459480%2fdate-bound-sql-group-by%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

                                          404 Error Contact Form 7 ajax form submitting

                                          How to know if a Active Directory user can login interactively

                                          Refactoring coordinates for Minecraft Pi buildings written in Python