Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Group By 30 Minutes

Group By 30 Minutes

Scheduled Pinned Locked Moved Database
sales
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    It_tech
    wrote on last edited by
    #1

    Hi All, I have used (datepart(hour,CR_callstart)) As Hours to group Data by Hours. What i would like is to group them by Every 30 Minutes. Is there any thing i can use so that I can get data grouped every hlaf an hour. At the moment i can get : Time Revenue 10:00 $100 11:00 $200 With : Select (datepart(hour,CR_callstart)) As Hours,sum(cost)as Revenue from dbo.Tablename Group by Hours But I want to get Time Revenue 10:00 $75 10:30 $25 11:00 $100 11:30 $100 Please advice. Thanks

    M J 2 Replies Last reply
    0
    • I It_tech

      Hi All, I have used (datepart(hour,CR_callstart)) As Hours to group Data by Hours. What i would like is to group them by Every 30 Minutes. Is there any thing i can use so that I can get data grouped every hlaf an hour. At the moment i can get : Time Revenue 10:00 $100 11:00 $200 With : Select (datepart(hour,CR_callstart)) As Hours,sum(cost)as Revenue from dbo.Tablename Group by Hours But I want to get Time Revenue 10:00 $75 10:30 $25 11:00 $100 11:30 $100 Please advice. Thanks

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Test the time component (using datepart) for the number of minutes, set a value 0 or 30 and then group by the value. This probably can all me done in 1 select statement.

      Never underestimate the power of human stupidity RAH

      I 1 Reply Last reply
      0
      • M Mycroft Holmes

        Test the time component (using datepart) for the number of minutes, set a value 0 or 30 and then group by the value. This probably can all me done in 1 select statement.

        Never underestimate the power of human stupidity RAH

        I Offline
        I Offline
        It_tech
        wrote on last edited by
        #3

        Thanks so Much for your reply.Its much appreciated.Sorry i am Having trouble to understand it.How do i modify : (datepart(hour,CR_callstart)) As Hours Many thanks.

        1 Reply Last reply
        0
        • I It_tech

          Hi All, I have used (datepart(hour,CR_callstart)) As Hours to group Data by Hours. What i would like is to group them by Every 30 Minutes. Is there any thing i can use so that I can get data grouped every hlaf an hour. At the moment i can get : Time Revenue 10:00 $100 11:00 $200 With : Select (datepart(hour,CR_callstart)) As Hours,sum(cost)as Revenue from dbo.Tablename Group by Hours But I want to get Time Revenue 10:00 $75 10:30 $25 11:00 $100 11:30 $100 Please advice. Thanks

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          This should do it.

          SELECT
          DATEPART(hour,CR_callstart) as hour,
          CASE
          WHEN DATEPART(minute,CR_callstart) between 0 AND 29 THEN 0
          ELSE 30
          END as minute,
          sum(cost) as revenue
          from tablename
          group by DATEPART(hour,CR_callstart),
          CASE
          WHEN DATEPART(minute,CR_callstart) between 0 AND 29 THEN 0
          ELSE 30
          END

          I 1 Reply Last reply
          0
          • J J4amieC

            This should do it.

            SELECT
            DATEPART(hour,CR_callstart) as hour,
            CASE
            WHEN DATEPART(minute,CR_callstart) between 0 AND 29 THEN 0
            ELSE 30
            END as minute,
            sum(cost) as revenue
            from tablename
            group by DATEPART(hour,CR_callstart),
            CASE
            WHEN DATEPART(minute,CR_callstart) between 0 AND 29 THEN 0
            ELSE 30
            END

            I Offline
            I Offline
            It_tech
            wrote on last edited by
            #5

            That is Fantastic.Is there any way I can put Hour and Minute in One Column. Like : Time 10:00 10:30 11:00 11:30 etcc.. Thanks so much for your time.

            J 1 Reply Last reply
            0
            • I It_tech

              That is Fantastic.Is there any way I can put Hour and Minute in One Column. Like : Time 10:00 10:30 11:00 11:30 etcc.. Thanks so much for your time.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              It_tech wrote:

              Is there any way I can put Hour and Minute in One Column.

              Sure, but thats presentation logic and should be handled appropriately.

              I 1 Reply Last reply
              0
              • J J4amieC

                It_tech wrote:

                Is there any way I can put Hour and Minute in One Column.

                Sure, but thats presentation logic and should be handled appropriately.

                I Offline
                I Offline
                It_tech
                wrote on last edited by
                #7

                Thanks For your reply and sorry to bother you.Can i do that from a Sql level? It would be great if you can give me an example. Many thanks.

                M 1 Reply Last reply
                0
                • I It_tech

                  Thanks For your reply and sorry to bother you.Can i do that from a Sql level? It would be great if you can give me an example. Many thanks.

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  He wrote the code for you - what do you want, that he should go out and test your app as well. Jamie stated that the requirement is PRESENTATION logic therefore it does not belong in the database and you then ask him if it can be done in the database. Do your own work and use your brain instead of relying on the forum for ideas.

                  Never underestimate the power of human stupidity RAH

                  I 1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    He wrote the code for you - what do you want, that he should go out and test your app as well. Jamie stated that the requirement is PRESENTATION logic therefore it does not belong in the database and you then ask him if it can be done in the database. Do your own work and use your brain instead of relying on the forum for ideas.

                    Never underestimate the power of human stupidity RAH

                    I Offline
                    I Offline
                    It_tech
                    wrote on last edited by
                    #9

                    Thanks.I have already sorted it out. Cheers

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups