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 date

Group by date

Scheduled Pinned Locked Moved Database
questiondatabase
9 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.
  • S Offline
    S Offline
    Sasuko
    wrote on last edited by
    #1

    I've a table where ia save the date when user have subscreibed. How can i query the number of subscriber signed in the 20 April 2006 or all the users signed since to 12 may 2006? Or between 14 april and 21 may?

    K C E 3 Replies Last reply
    0
    • S Sasuko

      I've a table where ia save the date when user have subscreibed. How can i query the number of subscriber signed in the 20 April 2006 or all the users signed since to 12 may 2006? Or between 14 april and 21 may?

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      if you save dates in the integer format like this: YYYYMMDD then you should be able to do something like this to get a count of subscribers on April 20, 2006: SELECT COUNT(*) FROM Subscribe_Table WHERE Date_Column = 20060420 To get a count of subscribers since May 12, 2006: SELECT COUNT(*) FROM Subscribe_Table WHERE Date_Column > 20060512 To get a count of subscribers between April 14 and May 21 of 2006: SELECT COUNT(*) FROM Subscribe_Table WHERE Date_Column > 20060414 AND Date_Column < 20060521

      S 1 Reply Last reply
      0
      • K Kschuler

        if you save dates in the integer format like this: YYYYMMDD then you should be able to do something like this to get a count of subscribers on April 20, 2006: SELECT COUNT(*) FROM Subscribe_Table WHERE Date_Column = 20060420 To get a count of subscribers since May 12, 2006: SELECT COUNT(*) FROM Subscribe_Table WHERE Date_Column > 20060512 To get a count of subscribers between April 14 and May 21 of 2006: SELECT COUNT(*) FROM Subscribe_Table WHERE Date_Column > 20060414 AND Date_Column < 20060521

        S Offline
        S Offline
        Sasuko
        wrote on last edited by
        #3

        Uhm... the value is a DateTime. What can i do without change the database?

        K 1 Reply Last reply
        0
        • S Sasuko

          Uhm... the value is a DateTime. What can i do without change the database?

          K Offline
          K Offline
          Kschuler
          wrote on last edited by
          #4

          I don't typically work with DateTime columns. Maybe this site will help you with the comparison part of it: http://www.databasejournal.com/features/mssql/article.php/2209321[^] Sorry I couldn't be more helpful.

          1 Reply Last reply
          0
          • S Sasuko

            I've a table where ia save the date when user have subscreibed. How can i query the number of subscriber signed in the 20 April 2006 or all the users signed since to 12 may 2006? Or between 14 april and 21 may?

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Okay, I'm assuming you are using SQL Server, and the column is a DATETIME or SMALLDATETIME. For a single day you can use this:

            SELECT * FROM MyTable WHERE [date] >= '2006-04-20 00:00:00' AND [date] <= '2006-04-20 23:59:59'

            If you are only storing the date element without the time then you can do a simpler [date] = '2006-04-20' instead. The same works for longer date ranges. e.g.

            SELECT * FROM MyTable WHERE [date] >= '2006-04-14 00:00:00' AND [date] <= '2006-05-21 23:59:59'

            Does this help?


            "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

            1 Reply Last reply
            0
            • S Sasuko

              I've a table where ia save the date when user have subscreibed. How can i query the number of subscriber signed in the 20 April 2006 or all the users signed since to 12 may 2006? Or between 14 april and 21 may?

              E Offline
              E Offline
              Eric Dahlvang
              wrote on last edited by
              #6

              Does this work? on April 20th 2006 SELECT COUNT(*) AS totalsubscribers FROM subscribers WHERE CONVERT(VARCHAR, datesubscribed,101) = '04/20/2006' since May 12th 2006 SELECT COUNT(*) AS totalsubscribers FROM subscribers WHERE CONVERT(VARCHAR, datesubscribed,101) >= '05/12/2006' between April 14th and May 21st SELECT COUNT(*) AS totalsubscribers FROM subscribers WHERE CONVERT(VARCHAR, datesubscribed,101) BETWEEN '04/14/2006' AND '05/21/2006' ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

              C 1 Reply Last reply
              0
              • E Eric Dahlvang

                Does this work? on April 20th 2006 SELECT COUNT(*) AS totalsubscribers FROM subscribers WHERE CONVERT(VARCHAR, datesubscribed,101) = '04/20/2006' since May 12th 2006 SELECT COUNT(*) AS totalsubscribers FROM subscribers WHERE CONVERT(VARCHAR, datesubscribed,101) >= '05/12/2006' between April 14th and May 21st SELECT COUNT(*) AS totalsubscribers FROM subscribers WHERE CONVERT(VARCHAR, datesubscribed,101) BETWEEN '04/14/2006' AND '05/21/2006' ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                That would do a string based comparison, so you would be better using an ISO format where it puts YYYY-DD-MM (i.e. most significant digits first) - It also keeps it culture neutral. You don't need to convert the DATETIME or SMALLDATETIME into varchars either because a string literal will be implicity converted to a date if the format is appropriate.


                "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

                E 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  That would do a string based comparison, so you would be better using an ISO format where it puts YYYY-DD-MM (i.e. most significant digits first) - It also keeps it culture neutral. You don't need to convert the DATETIME or SMALLDATETIME into varchars either because a string literal will be implicity converted to a date if the format is appropriate.


                  "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

                  E Offline
                  E Offline
                  Eric Dahlvang
                  wrote on last edited by
                  #8

                  Oh. I figured that the conversion would be necessary in order to get rid of the time portion from the datesubscribed field. In your example you provided the time: '2006-04-20 23:59:59' - but it seemed like he just wanted to compare dates. How would you do this? ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

                  C 1 Reply Last reply
                  0
                  • E Eric Dahlvang

                    Oh. I figured that the conversion would be necessary in order to get rid of the time portion from the datesubscribed field. In your example you provided the time: '2006-04-20 23:59:59' - but it seemed like he just wanted to compare dates. How would you do this? ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    If the [date] column in the database contains no time elements then, say, 22-May-2006 would be stored as midnight (00:00:00). In that case you wouldn't need to do the 23:59:59 (or 23:59 on a SMALLDATETIME) thing. It has confused me why there isn't a useful built-in date only function (to extract just the date part) for comparisons such as this. Solutions I've used in the past include:

                    -- Performance is okay
                    ...WHERE DAY([date]) = 22 AND MONTH([date]) = 5 AND YEAR([date]) = 2006

                    -- Performance is abysmal on large datasets and useless if part of a join (What was I thinking!?!)
                    WHERE [date] = dbo.datetime_rounddown(@someDateTime)

                    -- Okay as a replacement for a column that is being SELECTed
                    SELECT dbo.datetime_rounddown([date]) FROM MyTable

                    -- If using a temp table to add an extra column that specifically excluded the time portion

                    -- Or if the table is sufficiently large, you could add an extra column to hold the date only version

                    I've never tried to convert it to an varchar and use that so I don't know what the performance is like, but it would have to be the ISO format because greater/less-than comparisons wouldn't work properly, e.g. (using the British format) 22/05/2006 22/05/2007 -- Jump a year 23/05/2005 -- then jump almost two years back again. You'd get a similar situation with the US format date too. If the comparison is something that is going to be done a lot and the table is large, then it may be worth considering denormalising the data model slightly to boost the performance. I've done that before where the table was holding millions of rows. Some queries went from taking several minutes to taking a few seconds.


                    "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

                    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