473,654 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to group by date but not time?

8,435 Recognized Expert Expert
Hi all.

Hopefully a simple one for any SQL guru. In an Access query, how can I group by a date field, without having my data broken down by time? In other words, I just want a count per day, not per second or whatever.

I'm using Access 2003, but I doubt it matters in this case.

At present I am just using a function (see below) to strip off the date, but I consider this terribly inefficient.

I did try some searching, but this is one case where there's too much info available. Just within TSDN there are dozens upon dozens of postings mentioning grouping by date, but they seem to mostly relate to reasons why or why not, different situations where one should group by dates, and so on.

What I've tried so far:
  1. First I tried setting the GROUP BY column to Format(DateFiel d,"dd/mm/yyyy") and this worked, but obviously sorted in the wrong sequence.
  2. Currently my GROUP BY column is set to DateOnly(DateFi eld), and I have written...
    Expand|Select|Wrap|Line Numbers
    1. Public Function DateOnly(ByVal pDateTime As Date) As Date
    2.   ' Simple function to strip off the time, leaving only the date.
    3.   DateOnly = DateSerial(Year(pDateTime), Month(pDateTime), Day(pDateTime))
    4. End Function
    This does leave me the benefits of using an actual date field, including the ability to apply a format without stuffing up the sort sequence.
Apr 17 '07 #1
13 29395
pks00
280 Recognized Expert Contributor
If u have grouped the date field, u now have your data in short format

Next thing is ordering, have u tried specifying column numbers?

eg

select format(f1,"shor t date")
from mytable
group by format(f1,"shor t date")
order by 1
Apr 18 '07 #2
MMcCarthy
14,534 Recognized Expert Moderator MVP
If you GROUP BY Format(DateFiel d,"dd/mm/yyyy") and ORDER BY DateField does this solve your problem?
Mary
Apr 18 '07 #3
Killer42
8,435 Recognized Expert Expert
If you GROUP BY Format(DateFiel d,"dd/mm/yyyy") and ORDER BY DateField does this solve your problem?
I'll try it, and let you know.
Apr 18 '07 #4
Killer42
8,435 Recognized Expert Expert
If u have grouped the date field, u now have your data in short format
Not sure what you mean by this. If you mean I've got just the date, then yes. But I'd prefer not to have to call my function for every record to produce the date to group on.

Next thing is ordering, have u tried specifying column numbers?
eg
select format(f1,"shor t date")
from mytable
group by format(f1,"shor t date")
order by 1
I'll try, but don't see how that will help. It's already ordered by that column, by default. The problem is that the formatted date is useless for ordering purposes. As an example, they are sorted...
01/01/2003
01/01/2004
01/01/2005
01/01/2006
01/01/2007
01/02/2003
01/02/2004
01/02/2005
01/02/2006
01/02/2007
This is pointless, even if it was U.S. format. I can get the right sequence if I format it as YYYYMMDD, but then only a geek will recognise the date.
Apr 18 '07 #5
Killer42
8,435 Recognized Expert Expert
If you GROUP BY Format(DateFiel d,"dd/mm/yyyy") and ORDER BY DateField does this solve your problem?
Nope. It won't let me ORDER BY a field unless it's one of the ones specified in the aggregate functions.

Sigh... :(
Apr 18 '07 #6
ADezii
8,834 Recognized Expert Expert
Nope. It won't let me ORDER BY a field unless it's one of the ones specified in the aggregate functions.

Sigh... :(
Try Ordering by DateField, set it to an Aggregate Function such as Min, Max, and deselect the Show Box. Does this produce your desired results?
Apr 18 '07 #7
Killer42
8,435 Recognized Expert Expert
Try Ordering by DateField, set it to an Aggregate Function such as Min, Max, and deselect the Show Box. Does this produce your desired results?
Woo! Looking good.

So far, I created a new query, put in field MyDate:Format(D ateField,"dd/mm/yyyy"), then added DateField again with Total: Max and Sort: Ascending.

It appears to work. Here's the resulting SQL, which I feel we may still be able to refine a bit...
Expand|Select|Wrap|Line Numbers
  1. SELECT Format([DateField],"dd/mm/yyyy") AS MyDate
  2. FROM TableName
  3. GROUP BY Format([DateField],"dd/mm/yyyy")
  4. ORDER BY Max(TableName.DateField);
(All table and field names have been changed to protect the innocent).

Of course, when I tried removing the Max( ) function in the SQL, I got the old "must be part of an aggregate function" message.

One thing I noticed was that this appeared to execute much faster than the version using my function. Which is why I didn't want to use the function in the first place, of course.
Apr 19 '07 #8
ADezii
8,834 Recognized Expert Expert
Woo! Looking good.

So far, I created a new query, put in field MyDate:Format(D ateField,"dd/mm/yyyy"), then added DateField again with Total: Max and Sort: Ascending.

It appears to work. Here's the resulting SQL, which I feel we may still be able to refine a bit...
Expand|Select|Wrap|Line Numbers
  1. SELECT Format([DateField],"dd/mm/yyyy") AS MyDate
  2. FROM TableName
  3. GROUP BY Format([DateField],"dd/mm/yyyy")
  4. ORDER BY Max(TableName.DateField);
(All table and field names have been changed to protect the innocent).

Of course, when I tried removing the Max( ) function in the SQL, I got the old "must be part of an aggregate function" message.

One thing I noticed was that this appeared to execute much faster than the version using my function. Which is why I didn't want to use the function in the first place, of course.
Is this liftoff?
Apr 19 '07 #9
Killer42
8,435 Recognized Expert Expert
Is this liftoff?
Very possibly. I have the "real" query running now. It may still take quite some time, but we'll see.

It was a bit odd when I ran the function version yesterday. After a while the CPU that Access was using dropped a bit (um... to around 30-40%, I think) but Explorer jumped up to nearly 50% CPU, and stayed that way for maybe half an hour before I gave up (it was affecting other things too much) and cancelled the query. I wonder what Explorer was doing...

If was Access using too much CPU, I could just turn down the priority, but turn down Explorer and you don't really improve matters.

Oh well. I'll see how the current one goes. Shouldn't have to run it very often.
Apr 19 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
776
by: bobb | last post by:
Is there a way to select count(*) by grouping by date, and having multiple date ranges? combining... select field,count(*) from table where datefield > 2004/1/1 and datefield < 2004/1/31 and select field,count(*) from table where datefield > 2004/2/1 and datefield < 2004/2/29
1
2003
by: Rajani | last post by:
Hello, I have a table(msaccess) with the structure... job_code text 6 style text 10 qty number fabrication text 65 ship_date date/time
6
17912
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by year & month When I try to place a date filter 'Between x And y ' on an expression field
8
4724
by: Thomas | last post by:
Hi! I´m a newbie with this and I´m trying to build a forum of my own but have stumbled on my first problem as early as the opening page. To the problem: I want to show a simple forum layout with tables looking something like this Forum Name | Topics | Posts | Last Post
7
1697
by: matteosartori | last post by:
Hi all, I've spent all morning trying to work this one out: I've got the following string: <td>04/01/2006</td><td>Wednesday</td><td>&nbsp;</td><td>09:14</td><td>12:44</td><td>12:50</td><td>17:58</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>08:14</td> from which I'm attempting to extract the date, and the five times from into a list. Only the very last time is guaranteed to be there so it
2
11996
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT convert(varchar,J1708Data.dtDateTime,120), count(convert(varchar,J1708Data.dtDateTime,120))
2
4113
by: crackerbox | last post by:
I have a table that I need to find the maximum date per Doc_No per Parent_Doccategory. I can get the maximum date per Doc_No but I can't get the information for the next level. The following script is getting the maximum date per airplane but I should get a maximum date each time the parent_doccategory changes. Can anyone help? This is the script so far: CREATE PROCEDURE . AS select Effective_Updated , parent_doc_no,...
12
18898
by: anonymous | last post by:
Hello, I have a table which has a date column. The values in the date column are along with time i.e., 2/1/2008 12:00:00 PM 2/1/2008 2:13:00 PM 2/3/2008 4:00:00 AM 3/1/2008 1:00:00 PM 3/1/2008 4:00:00 PM 3/3/2008 2:18:00 AM 3/3/2008 5:00:00 PM 3/3/2008 7:08:12 PM
5
5060
by: HowHow | last post by:
First time using crosstab query because tired of creating queries and put them together in one query (such a stupid thing to do :D). Not sure how it works still. I have link table called dbo_Roster to access 2000, I try to group data by Service and dates, for example 01/01 to 01/31 is Jan08, 01/02 to 28/2 is Feb08 and so on. The result should be like below : Service Jan08 Feb08 Mar08 Apr08 May08 Jun08 ...
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.