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 Clause...

GROUP BY Clause...

Scheduled Pinned Locked Moved Database
databasehelpquestion
5 Posts 4 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
    Illegal Operation
    wrote on last edited by
    #1

    Hi! I have a table that contains a Date and Minutes. I want to create a query to group the entries by Date so that instead of seeing more than one entry on a day, you will only see one entry date with the total minutes? Can anyone shed some light on this issue? Thank you!!

    Illegal Operation

    N M 2 Replies Last reply
    0
    • I Illegal Operation

      Hi! I have a table that contains a Date and Minutes. I want to create a query to group the entries by Date so that instead of seeing more than one entry on a day, you will only see one entry date with the total minutes? Can anyone shed some light on this issue? Thank you!!

      Illegal Operation

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      Try this Sample Data

      declare @t table(dt date, tm int)
      insert into @t
      select '2010-01-05',15 union all
      select '2010-01-05',16 union all
      select '2010-01-05',18 union all
      select '2010-01-05',36 union all
      select '2010-01-05',59 union all
      select '2010-01-06',01 union all
      select '2010-01-06',20 union all
      select '2010-01-07',30 union all
      select '2009-12-21',34 union all
      select '2009-12-22',12

      Query

      select dt,CntMins = COUNT(tm),TotalMins = SUM(tm) from @t
      group by dt
      --order by SUM(tm) desc --[Incase you want to see the result in Descending Order]

      Output

      dt CntMins TotalMins
      2009-12-21 1 34
      2009-12-22 1 12
      2010-01-05 5 144
      2010-01-06 2 21
      2010-01-07 1 30

      :)

      Niladri Biswas

      1 Reply Last reply
      0
      • I Illegal Operation

        Hi! I have a table that contains a Date and Minutes. I want to create a query to group the entries by Date so that instead of seeing more than one entry on a day, you will only see one entry date with the total minutes? Can anyone shed some light on this issue? Thank you!!

        Illegal Operation

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

        Group by only the date part of the datetime, modified being the datetime field

        GROUP BY CONVERT(DATETIME,Modified,103)

        [edit] the above does not work, if you are usiong 2008 the following will work using the new DATE data type.

        SELECT COUNT(*)
        FROM EquityCounter
        GROUP BY CONVERT(DATE,Modified)

        If you are using 2005 I would suggest chopping up the string daettime like so:

        GROUP BY CONVERT(DATETIME,LEFT(CONVERT(VARCHAR(50),Modified),11))

        [/edit] Still gotta be better than a temp table!

        Never underestimate the power of human stupidity RAH

        modified on Tuesday, January 5, 2010 1:16 AM

        J 1 Reply Last reply
        0
        • M Mycroft Holmes

          Group by only the date part of the datetime, modified being the datetime field

          GROUP BY CONVERT(DATETIME,Modified,103)

          [edit] the above does not work, if you are usiong 2008 the following will work using the new DATE data type.

          SELECT COUNT(*)
          FROM EquityCounter
          GROUP BY CONVERT(DATE,Modified)

          If you are using 2005 I would suggest chopping up the string daettime like so:

          GROUP BY CONVERT(DATETIME,LEFT(CONVERT(VARCHAR(50),Modified),11))

          [/edit] Still gotta be better than a temp table!

          Never underestimate the power of human stupidity RAH

          modified on Tuesday, January 5, 2010 1:16 AM

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

          Mycroft Holmes wrote:

          Still gotta be better than a temp table!

          The temp table in the answer above was only to provide example data methinks!

          M 1 Reply Last reply
          0
          • J J4amieC

            Mycroft Holmes wrote:

            Still gotta be better than a temp table!

            The temp table in the answer above was only to provide example data methinks!

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

            Me didn't look at the details of the answer, just looked at the complexity and shook the head. I don't think Nilandri answered the question which was to group by the date only in a datetime field that has the time component in it.

            Never underestimate the power of human stupidity RAH

            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