Min Max (Date Time) And Count Data










0















Using SQL Server 2014



Example Table:



| ID | Date_Time | ID_Plan |
| 1 | 2018-12-19 21:21:00.000 | A1 |
| 2 | 2018-12-19 21:22:00.000 | A1 |
| 3 | 2018-12-19 21:23:00.000 | NULL |
| 4 | 2018-12-19 21:24:00.000 | NULL |
| 5 | 2018-12-19 21:25:00.000 | A2 |
| 6 | 2018-12-19 21:26:00.000 | A2 |
| 7 | 2018-12-19 21:27:00.000 | A2 |
| 8 | 2018-12-19 21:28:00.000 | A3 |
| 9 | 2018-12-19 21:29:00.000 | A3 |
| 10 | 2018-12-19 21:30:00.000 | NULL |
| 11 | 2018-12-19 21:31:00.000 | NULL |
| 12 | 2018-12-19 21:32:00.000 | NULL |
| 13 | 2018-12-19 21:33:00.000 | A4 |
| 14 | 2018-12-19 21:34:00.000 | A4 |
| 15 | 2018-12-19 21:35:00.000 | A4 |
| 16 | 2018-12-20 21:36:00.000 | NULL |

IF OBJECT_ID('tempdb..#tb_CountFinal') IS NOT NULL
DROP TABLE #tb_CountFinal

CREATE TABLE #tb_CountFinal
(
[ID] [int] IDENTITY(1,1) NOT NULL,
Date_Time DATETIME,
ID_Plan NVARCHAR(100)
)


INSERT INTO #tb_CountFinal
SELECT '12/19/2018 21:21','A1'union all
SELECT '12/19/2018 21:22','A1'union all
SELECT '12/19/2018 21:23',NULL union all
SELECT '12/19/2018 21:24',NULL union all
SELECT '12/19/2018 21:25','A2'union all
SELECT '12/19/2018 21:26','A2'union all
SELECT '12/19/2018 21:27','A2'union all
SELECT '12/19/2018 21:28','A3'union all
SELECT '12/19/2018 21:29','A3'union all
SELECT '12/19/2018 21:30',NULL union all
SELECT '12/19/2018 21:31',NULL union all
SELECT '12/19/2018 21:32',NULL union all
SELECT '12/19/2018 21:33','A4'union all
SELECT '12/19/2018 21:34','A4'union all
SELECT '12/19/2018 21:35','A4'union all
SELECT '12/20/2018 21:36',NULL


I'm interested in a query which would output rows similar to the following:



| Start_Date | End_Date | Plan_ID | Count_PlantID |
| 12/19/2018 21:21 | 12/19/2018 21:22 | A1 | 2 |
| 12/19/2018 21:23 | 12/19/2018 21:24 | NULL | 2 |
| 12/19/2018 21:25 | 12/19/2018 21:27 | A2 | 3 |
| 12/19/2018 21:28 | 12/19/2018 21:29 | A3 | 2 |
| 12/19/2018 21:30 | 12/19/2018 21:32 | NULL | 3 |
| 12/19/2018 21:33 | 12/19/2018 21:35 | A4 | 3 |
| 12/20/2018 21:36 | 12/20/2018 21:36 | NULL | 1 |









share|improve this question



















  • 1





    Have you tried anything? I think it's as simple as doing Group By then doing Min, Max and Count

    – sallushan
    Nov 15 '18 at 4:16











  • Thank, But I need separate Null data "Plan_ID"

    – taotechnocom
    Nov 15 '18 at 4:23












  • what do you mean by separate Null data "Plan_ID" ?

    – Squirrel
    Nov 15 '18 at 4:34











  • I need to finding match or best of avaliable of time slot

    – taotechnocom
    Nov 15 '18 at 4:37















0















Using SQL Server 2014



Example Table:



| ID | Date_Time | ID_Plan |
| 1 | 2018-12-19 21:21:00.000 | A1 |
| 2 | 2018-12-19 21:22:00.000 | A1 |
| 3 | 2018-12-19 21:23:00.000 | NULL |
| 4 | 2018-12-19 21:24:00.000 | NULL |
| 5 | 2018-12-19 21:25:00.000 | A2 |
| 6 | 2018-12-19 21:26:00.000 | A2 |
| 7 | 2018-12-19 21:27:00.000 | A2 |
| 8 | 2018-12-19 21:28:00.000 | A3 |
| 9 | 2018-12-19 21:29:00.000 | A3 |
| 10 | 2018-12-19 21:30:00.000 | NULL |
| 11 | 2018-12-19 21:31:00.000 | NULL |
| 12 | 2018-12-19 21:32:00.000 | NULL |
| 13 | 2018-12-19 21:33:00.000 | A4 |
| 14 | 2018-12-19 21:34:00.000 | A4 |
| 15 | 2018-12-19 21:35:00.000 | A4 |
| 16 | 2018-12-20 21:36:00.000 | NULL |

IF OBJECT_ID('tempdb..#tb_CountFinal') IS NOT NULL
DROP TABLE #tb_CountFinal

CREATE TABLE #tb_CountFinal
(
[ID] [int] IDENTITY(1,1) NOT NULL,
Date_Time DATETIME,
ID_Plan NVARCHAR(100)
)


INSERT INTO #tb_CountFinal
SELECT '12/19/2018 21:21','A1'union all
SELECT '12/19/2018 21:22','A1'union all
SELECT '12/19/2018 21:23',NULL union all
SELECT '12/19/2018 21:24',NULL union all
SELECT '12/19/2018 21:25','A2'union all
SELECT '12/19/2018 21:26','A2'union all
SELECT '12/19/2018 21:27','A2'union all
SELECT '12/19/2018 21:28','A3'union all
SELECT '12/19/2018 21:29','A3'union all
SELECT '12/19/2018 21:30',NULL union all
SELECT '12/19/2018 21:31',NULL union all
SELECT '12/19/2018 21:32',NULL union all
SELECT '12/19/2018 21:33','A4'union all
SELECT '12/19/2018 21:34','A4'union all
SELECT '12/19/2018 21:35','A4'union all
SELECT '12/20/2018 21:36',NULL


I'm interested in a query which would output rows similar to the following:



| Start_Date | End_Date | Plan_ID | Count_PlantID |
| 12/19/2018 21:21 | 12/19/2018 21:22 | A1 | 2 |
| 12/19/2018 21:23 | 12/19/2018 21:24 | NULL | 2 |
| 12/19/2018 21:25 | 12/19/2018 21:27 | A2 | 3 |
| 12/19/2018 21:28 | 12/19/2018 21:29 | A3 | 2 |
| 12/19/2018 21:30 | 12/19/2018 21:32 | NULL | 3 |
| 12/19/2018 21:33 | 12/19/2018 21:35 | A4 | 3 |
| 12/20/2018 21:36 | 12/20/2018 21:36 | NULL | 1 |









share|improve this question



















  • 1





    Have you tried anything? I think it's as simple as doing Group By then doing Min, Max and Count

    – sallushan
    Nov 15 '18 at 4:16











  • Thank, But I need separate Null data "Plan_ID"

    – taotechnocom
    Nov 15 '18 at 4:23












  • what do you mean by separate Null data "Plan_ID" ?

    – Squirrel
    Nov 15 '18 at 4:34











  • I need to finding match or best of avaliable of time slot

    – taotechnocom
    Nov 15 '18 at 4:37













0












0








0








Using SQL Server 2014



Example Table:



| ID | Date_Time | ID_Plan |
| 1 | 2018-12-19 21:21:00.000 | A1 |
| 2 | 2018-12-19 21:22:00.000 | A1 |
| 3 | 2018-12-19 21:23:00.000 | NULL |
| 4 | 2018-12-19 21:24:00.000 | NULL |
| 5 | 2018-12-19 21:25:00.000 | A2 |
| 6 | 2018-12-19 21:26:00.000 | A2 |
| 7 | 2018-12-19 21:27:00.000 | A2 |
| 8 | 2018-12-19 21:28:00.000 | A3 |
| 9 | 2018-12-19 21:29:00.000 | A3 |
| 10 | 2018-12-19 21:30:00.000 | NULL |
| 11 | 2018-12-19 21:31:00.000 | NULL |
| 12 | 2018-12-19 21:32:00.000 | NULL |
| 13 | 2018-12-19 21:33:00.000 | A4 |
| 14 | 2018-12-19 21:34:00.000 | A4 |
| 15 | 2018-12-19 21:35:00.000 | A4 |
| 16 | 2018-12-20 21:36:00.000 | NULL |

IF OBJECT_ID('tempdb..#tb_CountFinal') IS NOT NULL
DROP TABLE #tb_CountFinal

CREATE TABLE #tb_CountFinal
(
[ID] [int] IDENTITY(1,1) NOT NULL,
Date_Time DATETIME,
ID_Plan NVARCHAR(100)
)


INSERT INTO #tb_CountFinal
SELECT '12/19/2018 21:21','A1'union all
SELECT '12/19/2018 21:22','A1'union all
SELECT '12/19/2018 21:23',NULL union all
SELECT '12/19/2018 21:24',NULL union all
SELECT '12/19/2018 21:25','A2'union all
SELECT '12/19/2018 21:26','A2'union all
SELECT '12/19/2018 21:27','A2'union all
SELECT '12/19/2018 21:28','A3'union all
SELECT '12/19/2018 21:29','A3'union all
SELECT '12/19/2018 21:30',NULL union all
SELECT '12/19/2018 21:31',NULL union all
SELECT '12/19/2018 21:32',NULL union all
SELECT '12/19/2018 21:33','A4'union all
SELECT '12/19/2018 21:34','A4'union all
SELECT '12/19/2018 21:35','A4'union all
SELECT '12/20/2018 21:36',NULL


I'm interested in a query which would output rows similar to the following:



| Start_Date | End_Date | Plan_ID | Count_PlantID |
| 12/19/2018 21:21 | 12/19/2018 21:22 | A1 | 2 |
| 12/19/2018 21:23 | 12/19/2018 21:24 | NULL | 2 |
| 12/19/2018 21:25 | 12/19/2018 21:27 | A2 | 3 |
| 12/19/2018 21:28 | 12/19/2018 21:29 | A3 | 2 |
| 12/19/2018 21:30 | 12/19/2018 21:32 | NULL | 3 |
| 12/19/2018 21:33 | 12/19/2018 21:35 | A4 | 3 |
| 12/20/2018 21:36 | 12/20/2018 21:36 | NULL | 1 |









share|improve this question
















Using SQL Server 2014



Example Table:



| ID | Date_Time | ID_Plan |
| 1 | 2018-12-19 21:21:00.000 | A1 |
| 2 | 2018-12-19 21:22:00.000 | A1 |
| 3 | 2018-12-19 21:23:00.000 | NULL |
| 4 | 2018-12-19 21:24:00.000 | NULL |
| 5 | 2018-12-19 21:25:00.000 | A2 |
| 6 | 2018-12-19 21:26:00.000 | A2 |
| 7 | 2018-12-19 21:27:00.000 | A2 |
| 8 | 2018-12-19 21:28:00.000 | A3 |
| 9 | 2018-12-19 21:29:00.000 | A3 |
| 10 | 2018-12-19 21:30:00.000 | NULL |
| 11 | 2018-12-19 21:31:00.000 | NULL |
| 12 | 2018-12-19 21:32:00.000 | NULL |
| 13 | 2018-12-19 21:33:00.000 | A4 |
| 14 | 2018-12-19 21:34:00.000 | A4 |
| 15 | 2018-12-19 21:35:00.000 | A4 |
| 16 | 2018-12-20 21:36:00.000 | NULL |

IF OBJECT_ID('tempdb..#tb_CountFinal') IS NOT NULL
DROP TABLE #tb_CountFinal

CREATE TABLE #tb_CountFinal
(
[ID] [int] IDENTITY(1,1) NOT NULL,
Date_Time DATETIME,
ID_Plan NVARCHAR(100)
)


INSERT INTO #tb_CountFinal
SELECT '12/19/2018 21:21','A1'union all
SELECT '12/19/2018 21:22','A1'union all
SELECT '12/19/2018 21:23',NULL union all
SELECT '12/19/2018 21:24',NULL union all
SELECT '12/19/2018 21:25','A2'union all
SELECT '12/19/2018 21:26','A2'union all
SELECT '12/19/2018 21:27','A2'union all
SELECT '12/19/2018 21:28','A3'union all
SELECT '12/19/2018 21:29','A3'union all
SELECT '12/19/2018 21:30',NULL union all
SELECT '12/19/2018 21:31',NULL union all
SELECT '12/19/2018 21:32',NULL union all
SELECT '12/19/2018 21:33','A4'union all
SELECT '12/19/2018 21:34','A4'union all
SELECT '12/19/2018 21:35','A4'union all
SELECT '12/20/2018 21:36',NULL


I'm interested in a query which would output rows similar to the following:



| Start_Date | End_Date | Plan_ID | Count_PlantID |
| 12/19/2018 21:21 | 12/19/2018 21:22 | A1 | 2 |
| 12/19/2018 21:23 | 12/19/2018 21:24 | NULL | 2 |
| 12/19/2018 21:25 | 12/19/2018 21:27 | A2 | 3 |
| 12/19/2018 21:28 | 12/19/2018 21:29 | A3 | 2 |
| 12/19/2018 21:30 | 12/19/2018 21:32 | NULL | 3 |
| 12/19/2018 21:33 | 12/19/2018 21:35 | A4 | 3 |
| 12/20/2018 21:36 | 12/20/2018 21:36 | NULL | 1 |






sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:21









Squirrel

11.9k22128




11.9k22128










asked Nov 15 '18 at 4:08









taotechnocomtaotechnocom

1585




1585







  • 1





    Have you tried anything? I think it's as simple as doing Group By then doing Min, Max and Count

    – sallushan
    Nov 15 '18 at 4:16











  • Thank, But I need separate Null data "Plan_ID"

    – taotechnocom
    Nov 15 '18 at 4:23












  • what do you mean by separate Null data "Plan_ID" ?

    – Squirrel
    Nov 15 '18 at 4:34











  • I need to finding match or best of avaliable of time slot

    – taotechnocom
    Nov 15 '18 at 4:37












  • 1





    Have you tried anything? I think it's as simple as doing Group By then doing Min, Max and Count

    – sallushan
    Nov 15 '18 at 4:16











  • Thank, But I need separate Null data "Plan_ID"

    – taotechnocom
    Nov 15 '18 at 4:23












  • what do you mean by separate Null data "Plan_ID" ?

    – Squirrel
    Nov 15 '18 at 4:34











  • I need to finding match or best of avaliable of time slot

    – taotechnocom
    Nov 15 '18 at 4:37







1




1





Have you tried anything? I think it's as simple as doing Group By then doing Min, Max and Count

– sallushan
Nov 15 '18 at 4:16





Have you tried anything? I think it's as simple as doing Group By then doing Min, Max and Count

– sallushan
Nov 15 '18 at 4:16













Thank, But I need separate Null data "Plan_ID"

– taotechnocom
Nov 15 '18 at 4:23






Thank, But I need separate Null data "Plan_ID"

– taotechnocom
Nov 15 '18 at 4:23














what do you mean by separate Null data "Plan_ID" ?

– Squirrel
Nov 15 '18 at 4:34





what do you mean by separate Null data "Plan_ID" ?

– Squirrel
Nov 15 '18 at 4:34













I need to finding match or best of avaliable of time slot

– taotechnocom
Nov 15 '18 at 4:37





I need to finding match or best of avaliable of time slot

– taotechnocom
Nov 15 '18 at 4:37












1 Answer
1






active

oldest

votes


















2














For each NULL entries I made a key by looking at the previous starting [ID] column, then performed the Group By on that new column. Try it.



select min(Date_Time) as Start_Date, max(Date_Time) as End_Date
, min(ID_Plan) as Plan_ID, count(1) as Count_PlanID
from (
select s.*
, case when ID_Plan is null
then
cast(
(
select max([ID])
from #tb_CountFinal as x
where x.ID < s.ID and ID_Plan is not null
) as varchar(100)
)
else
ID_Plan
end as ID_Plan_Adv_Start
from #tb_CountFinal as s
) as dt
group by dt.ID_Plan_Adv_Start
order by Start_Date





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%2f53312294%2fmin-max-date-time-and-count-data%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    For each NULL entries I made a key by looking at the previous starting [ID] column, then performed the Group By on that new column. Try it.



    select min(Date_Time) as Start_Date, max(Date_Time) as End_Date
    , min(ID_Plan) as Plan_ID, count(1) as Count_PlanID
    from (
    select s.*
    , case when ID_Plan is null
    then
    cast(
    (
    select max([ID])
    from #tb_CountFinal as x
    where x.ID < s.ID and ID_Plan is not null
    ) as varchar(100)
    )
    else
    ID_Plan
    end as ID_Plan_Adv_Start
    from #tb_CountFinal as s
    ) as dt
    group by dt.ID_Plan_Adv_Start
    order by Start_Date





    share|improve this answer



























      2














      For each NULL entries I made a key by looking at the previous starting [ID] column, then performed the Group By on that new column. Try it.



      select min(Date_Time) as Start_Date, max(Date_Time) as End_Date
      , min(ID_Plan) as Plan_ID, count(1) as Count_PlanID
      from (
      select s.*
      , case when ID_Plan is null
      then
      cast(
      (
      select max([ID])
      from #tb_CountFinal as x
      where x.ID < s.ID and ID_Plan is not null
      ) as varchar(100)
      )
      else
      ID_Plan
      end as ID_Plan_Adv_Start
      from #tb_CountFinal as s
      ) as dt
      group by dt.ID_Plan_Adv_Start
      order by Start_Date





      share|improve this answer

























        2












        2








        2







        For each NULL entries I made a key by looking at the previous starting [ID] column, then performed the Group By on that new column. Try it.



        select min(Date_Time) as Start_Date, max(Date_Time) as End_Date
        , min(ID_Plan) as Plan_ID, count(1) as Count_PlanID
        from (
        select s.*
        , case when ID_Plan is null
        then
        cast(
        (
        select max([ID])
        from #tb_CountFinal as x
        where x.ID < s.ID and ID_Plan is not null
        ) as varchar(100)
        )
        else
        ID_Plan
        end as ID_Plan_Adv_Start
        from #tb_CountFinal as s
        ) as dt
        group by dt.ID_Plan_Adv_Start
        order by Start_Date





        share|improve this answer













        For each NULL entries I made a key by looking at the previous starting [ID] column, then performed the Group By on that new column. Try it.



        select min(Date_Time) as Start_Date, max(Date_Time) as End_Date
        , min(ID_Plan) as Plan_ID, count(1) as Count_PlanID
        from (
        select s.*
        , case when ID_Plan is null
        then
        cast(
        (
        select max([ID])
        from #tb_CountFinal as x
        where x.ID < s.ID and ID_Plan is not null
        ) as varchar(100)
        )
        else
        ID_Plan
        end as ID_Plan_Adv_Start
        from #tb_CountFinal as s
        ) as dt
        group by dt.ID_Plan_Adv_Start
        order by Start_Date






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 4:38









        sallushansallushan

        979715




        979715





























            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%2f53312294%2fmin-max-date-time-and-count-data%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto