Query to categorize records based on time column without using case statement





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







1















Hope you're doing well.
There is table as you can see below:



[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)


and records in the table:



1   XX   0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913


format of the data in [time] column is hh:mm so 0030 is 00:30.
I want to write a query to categorize the records based on their time . I need to have 4 categories like this:



category1   00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12


I need to see how many records do I have in each category.
what I've written so far is like this :



With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]

)
select groupid,count(*) as recordcount
from cte
group by groupid


the query works fine but I just want to know whether there are better ways to write this query and avoid using Case statement.
Thanks in advance










share|improve this question




















  • 1





    'group' + CAST(1 + CAST(time/300 AS INTEGER) AS CHAR) AS groupID

    – Akina
    3 hours ago






  • 2





    So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a TIME data type?

    – Michael Kutz
    3 hours ago






  • 1





    What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.

    – Aaron Bertrand
    2 hours ago




















1















Hope you're doing well.
There is table as you can see below:



[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)


and records in the table:



1   XX   0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913


format of the data in [time] column is hh:mm so 0030 is 00:30.
I want to write a query to categorize the records based on their time . I need to have 4 categories like this:



category1   00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12


I need to see how many records do I have in each category.
what I've written so far is like this :



With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]

)
select groupid,count(*) as recordcount
from cte
group by groupid


the query works fine but I just want to know whether there are better ways to write this query and avoid using Case statement.
Thanks in advance










share|improve this question




















  • 1





    'group' + CAST(1 + CAST(time/300 AS INTEGER) AS CHAR) AS groupID

    – Akina
    3 hours ago






  • 2





    So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a TIME data type?

    – Michael Kutz
    3 hours ago






  • 1





    What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.

    – Aaron Bertrand
    2 hours ago
















1












1








1








Hope you're doing well.
There is table as you can see below:



[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)


and records in the table:



1   XX   0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913


format of the data in [time] column is hh:mm so 0030 is 00:30.
I want to write a query to categorize the records based on their time . I need to have 4 categories like this:



category1   00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12


I need to see how many records do I have in each category.
what I've written so far is like this :



With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]

)
select groupid,count(*) as recordcount
from cte
group by groupid


the query works fine but I just want to know whether there are better ways to write this query and avoid using Case statement.
Thanks in advance










share|improve this question
















Hope you're doing well.
There is table as you can see below:



[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)


and records in the table:



1   XX   0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913


format of the data in [time] column is hh:mm so 0030 is 00:30.
I want to write a query to categorize the records based on their time . I need to have 4 categories like this:



category1   00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12


I need to see how many records do I have in each category.
what I've written so far is like this :



With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]

)
select groupid,count(*) as recordcount
from cte
group by groupid


the query works fine but I just want to know whether there are better ways to write this query and avoid using Case statement.
Thanks in advance







sql-server t-sql query-performance optimization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Akina

5,1511411




5,1511411










asked 3 hours ago









Pantea TourangPantea Tourang

737




737








  • 1





    'group' + CAST(1 + CAST(time/300 AS INTEGER) AS CHAR) AS groupID

    – Akina
    3 hours ago






  • 2





    So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a TIME data type?

    – Michael Kutz
    3 hours ago






  • 1





    What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.

    – Aaron Bertrand
    2 hours ago
















  • 1





    'group' + CAST(1 + CAST(time/300 AS INTEGER) AS CHAR) AS groupID

    – Akina
    3 hours ago






  • 2





    So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a TIME data type?

    – Michael Kutz
    3 hours ago






  • 1





    What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.

    – Aaron Bertrand
    2 hours ago










1




1





'group' + CAST(1 + CAST(time/300 AS INTEGER) AS CHAR) AS groupID

– Akina
3 hours ago





'group' + CAST(1 + CAST(time/300 AS INTEGER) AS CHAR) AS groupID

– Akina
3 hours ago




2




2





So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a TIME data type?

– Michael Kutz
3 hours ago





So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a TIME data type?

– Michael Kutz
3 hours ago




1




1





What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.

– Aaron Bertrand
2 hours ago







What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.

– Aaron Bertrand
2 hours ago












2 Answers
2






active

oldest

votes


















2














You stored Time as an int but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE expression this could just be left to the ELSE clause, but if you eliminate CASE, you will have to deal with that exception explicitly - that's all the COALESCE/NULLIF stuff at the end.



;WITH x AS 
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
), y AS
(
SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
FROM x
)
SELECT ID, Product, [Time],
[GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
FROM y;


Results:



ID  Product  Time   GroupID
-- ------- ---- -------
1 XX 0030 group1
2 UY 0354 group2
3 YY 0517 group2
4 ZZ 0712 group3
5 WW 0415 group2
6 GG 1112 group4
7 MM 1030 group4
8 HH 0913 group4


I strongly recommend you use the actual time data type, as that is what it was designed for. Then you can use DATEPART(HOUR( in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369 and 9997. Or if the leading zeros are important but you don't care about validation, use char(4) instead of int.



I also think you need to handle the case where an event happens in the afternoon.



And FWIW I am not sure why you don't want to use a CASE expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:



;WITH x AS 
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
)
SELECT ID, Product, [Time],
GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
WHEN 0 THEN '1'
WHEN 1 THEN '2'
WHEN 2 THEN '3'
ELSE '4' END
FROM x;





share|improve this answer

































    1














    You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.



    (00/3) + 1 = 1



    (01/3) + 1 = 1



    (02/3) + 1 = 1



    (03/3) + 1 = 2



    (04/3) + 1 = 2



    ...



    In that way you will no longer need the case.






    share|improve this answer








    New contributor




    Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















    • thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

      – Pantea Tourang
      3 hours ago






    • 1





      You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

      – Jandisson
      3 hours ago












    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fdba.stackexchange.com%2fquestions%2f237015%2fquery-to-categorize-records-based-on-time-column-without-using-case-statement%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









    2














    You stored Time as an int but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE expression this could just be left to the ELSE clause, but if you eliminate CASE, you will have to deal with that exception explicitly - that's all the COALESCE/NULLIF stuff at the end.



    ;WITH x AS 
    (
    SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
    FROM dbo.ProductTT
    ), y AS
    (
    SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
    FROM x
    )
    SELECT ID, Product, [Time],
    [GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
    FROM y;


    Results:



    ID  Product  Time   GroupID
    -- ------- ---- -------
    1 XX 0030 group1
    2 UY 0354 group2
    3 YY 0517 group2
    4 ZZ 0712 group3
    5 WW 0415 group2
    6 GG 1112 group4
    7 MM 1030 group4
    8 HH 0913 group4


    I strongly recommend you use the actual time data type, as that is what it was designed for. Then you can use DATEPART(HOUR( in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369 and 9997. Or if the leading zeros are important but you don't care about validation, use char(4) instead of int.



    I also think you need to handle the case where an event happens in the afternoon.



    And FWIW I am not sure why you don't want to use a CASE expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:



    ;WITH x AS 
    (
    SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
    FROM dbo.ProductTT
    )
    SELECT ID, Product, [Time],
    GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
    WHEN 0 THEN '1'
    WHEN 1 THEN '2'
    WHEN 2 THEN '3'
    ELSE '4' END
    FROM x;





    share|improve this answer






























      2














      You stored Time as an int but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE expression this could just be left to the ELSE clause, but if you eliminate CASE, you will have to deal with that exception explicitly - that's all the COALESCE/NULLIF stuff at the end.



      ;WITH x AS 
      (
      SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
      FROM dbo.ProductTT
      ), y AS
      (
      SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
      FROM x
      )
      SELECT ID, Product, [Time],
      [GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
      FROM y;


      Results:



      ID  Product  Time   GroupID
      -- ------- ---- -------
      1 XX 0030 group1
      2 UY 0354 group2
      3 YY 0517 group2
      4 ZZ 0712 group3
      5 WW 0415 group2
      6 GG 1112 group4
      7 MM 1030 group4
      8 HH 0913 group4


      I strongly recommend you use the actual time data type, as that is what it was designed for. Then you can use DATEPART(HOUR( in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369 and 9997. Or if the leading zeros are important but you don't care about validation, use char(4) instead of int.



      I also think you need to handle the case where an event happens in the afternoon.



      And FWIW I am not sure why you don't want to use a CASE expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:



      ;WITH x AS 
      (
      SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
      FROM dbo.ProductTT
      )
      SELECT ID, Product, [Time],
      GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
      WHEN 0 THEN '1'
      WHEN 1 THEN '2'
      WHEN 2 THEN '3'
      ELSE '4' END
      FROM x;





      share|improve this answer




























        2












        2








        2







        You stored Time as an int but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE expression this could just be left to the ELSE clause, but if you eliminate CASE, you will have to deal with that exception explicitly - that's all the COALESCE/NULLIF stuff at the end.



        ;WITH x AS 
        (
        SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
        FROM dbo.ProductTT
        ), y AS
        (
        SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
        FROM x
        )
        SELECT ID, Product, [Time],
        [GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
        FROM y;


        Results:



        ID  Product  Time   GroupID
        -- ------- ---- -------
        1 XX 0030 group1
        2 UY 0354 group2
        3 YY 0517 group2
        4 ZZ 0712 group3
        5 WW 0415 group2
        6 GG 1112 group4
        7 MM 1030 group4
        8 HH 0913 group4


        I strongly recommend you use the actual time data type, as that is what it was designed for. Then you can use DATEPART(HOUR( in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369 and 9997. Or if the leading zeros are important but you don't care about validation, use char(4) instead of int.



        I also think you need to handle the case where an event happens in the afternoon.



        And FWIW I am not sure why you don't want to use a CASE expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:



        ;WITH x AS 
        (
        SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
        FROM dbo.ProductTT
        )
        SELECT ID, Product, [Time],
        GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
        WHEN 0 THEN '1'
        WHEN 1 THEN '2'
        WHEN 2 THEN '3'
        ELSE '4' END
        FROM x;





        share|improve this answer















        You stored Time as an int but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE expression this could just be left to the ELSE clause, but if you eliminate CASE, you will have to deal with that exception explicitly - that's all the COALESCE/NULLIF stuff at the end.



        ;WITH x AS 
        (
        SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
        FROM dbo.ProductTT
        ), y AS
        (
        SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
        FROM x
        )
        SELECT ID, Product, [Time],
        [GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
        FROM y;


        Results:



        ID  Product  Time   GroupID
        -- ------- ---- -------
        1 XX 0030 group1
        2 UY 0354 group2
        3 YY 0517 group2
        4 ZZ 0712 group3
        5 WW 0415 group2
        6 GG 1112 group4
        7 MM 1030 group4
        8 HH 0913 group4


        I strongly recommend you use the actual time data type, as that is what it was designed for. Then you can use DATEPART(HOUR( in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369 and 9997. Or if the leading zeros are important but you don't care about validation, use char(4) instead of int.



        I also think you need to handle the case where an event happens in the afternoon.



        And FWIW I am not sure why you don't want to use a CASE expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:



        ;WITH x AS 
        (
        SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
        FROM dbo.ProductTT
        )
        SELECT ID, Product, [Time],
        GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
        WHEN 0 THEN '1'
        WHEN 1 THEN '2'
        WHEN 2 THEN '3'
        ELSE '4' END
        FROM x;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 2 hours ago









        Aaron BertrandAaron Bertrand

        155k18301497




        155k18301497

























            1














            You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.



            (00/3) + 1 = 1



            (01/3) + 1 = 1



            (02/3) + 1 = 1



            (03/3) + 1 = 2



            (04/3) + 1 = 2



            ...



            In that way you will no longer need the case.






            share|improve this answer








            New contributor




            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





















            • thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

              – Pantea Tourang
              3 hours ago






            • 1





              You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

              – Jandisson
              3 hours ago
















            1














            You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.



            (00/3) + 1 = 1



            (01/3) + 1 = 1



            (02/3) + 1 = 1



            (03/3) + 1 = 2



            (04/3) + 1 = 2



            ...



            In that way you will no longer need the case.






            share|improve this answer








            New contributor




            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





















            • thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

              – Pantea Tourang
              3 hours ago






            • 1





              You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

              – Jandisson
              3 hours ago














            1












            1








            1







            You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.



            (00/3) + 1 = 1



            (01/3) + 1 = 1



            (02/3) + 1 = 1



            (03/3) + 1 = 2



            (04/3) + 1 = 2



            ...



            In that way you will no longer need the case.






            share|improve this answer








            New contributor




            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.










            You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.



            (00/3) + 1 = 1



            (01/3) + 1 = 1



            (02/3) + 1 = 1



            (03/3) + 1 = 2



            (04/3) + 1 = 2



            ...



            In that way you will no longer need the case.







            share|improve this answer








            New contributor




            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 3 hours ago









            JandissonJandisson

            1111




            1111




            New contributor




            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Jandisson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.













            • thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

              – Pantea Tourang
              3 hours ago






            • 1





              You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

              – Jandisson
              3 hours ago



















            • thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

              – Pantea Tourang
              3 hours ago






            • 1





              You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

              – Jandisson
              3 hours ago

















            thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

            – Pantea Tourang
            3 hours ago





            thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.

            – Pantea Tourang
            3 hours ago




            1




            1





            You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

            – Jandisson
            3 hours ago





            You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.

            – Jandisson
            3 hours ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • 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%2fdba.stackexchange.com%2fquestions%2f237015%2fquery-to-categorize-records-based-on-time-column-without-using-case-statement%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

            GameSpot

            connect to host localhost port 22: Connection refused

            Getting a Wifi WPA2 wifi connection