473,765 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access Group by and Count problem

Hi,

I'm trying to display the total page views per page within a given date
range, but the correct SQL is seemingly beyond me. I get the correct result
with a straightforward Group By and Count clause eg
SELECT DISTINCT tblPageViews.Pa geVisited,
Count(tblPageVi ews.PageVisited ) AS CountOfPageVisi ted
FROM tblPageViews
GROUP BY tblPageViews.Pa geVisited;

but as soon as I introduce some date criteria:
SELECT DISTINCT tblPageViews.Pa geVisited,
Count(tblPageVi ews.PageVisited ) AS CountOfPageVisi ted
FROM tblPageViews
GROUP BY tblPageViews.Pa geVisited, tblPageViews.St artTime
HAVING tblPageViews.St artTime Between #11/1/2004 0:0:1# And #11/1/2004
23:59:59#;

the results change completely. For example, I know the default.asp was
viewed nearly 5000 times within the date range, but it appears twice in the
second resultset, both times with a count of 1.

Incidentally, I have taken into account that the results will differ between
the 2 examples as no criteria restrictions are placed on the first query.

Am I being idiotic in relying on Access's Query Wizard?

TIA
Jul 21 '05 #1
2 3090
Paxton wrote:
Hi,

I'm trying to display the total page views per page within a given
date range, but the correct SQL is seemingly beyond me. I get the
correct result with a straightforward Group By and Count clause eg
SELECT DISTINCT tblPageViews.Pa geVisited,
You should get rid of the DISTINCT keyword. GROUP BY is already insuring
distinct records...
Count(tblPageVi ews.PageVisited ) AS CountOfPageVisi ted
FROM tblPageViews
GROUP BY tblPageViews.Pa geVisited;

but as soon as I introduce some date criteria:
SELECT DISTINCT tblPageViews.Pa geVisited,
Count(tblPageVi ews.PageVisited ) AS CountOfPageVisi ted
FROM tblPageViews
GROUP BY tblPageViews.Pa geVisited, tblPageViews.St artTime
HAVING tblPageViews.St artTime Between #11/1/2004 0:0:1# And
#11/1/2004 23:59:59#;
This criterion should be put in the WHERE clause, since it has nothing to do
with the aggregated data. Criteria in the WHERE clause are enforced BEFORE
the data is grouped and aggregated. Criteria in the HAVING clause are
enforced AFTER the grouping and aggregation. Whenever possible, you should
put the criteria in the WHERE clause, since this will minimize the number of
records that the grouping engine will work with, improving performance. Your
query should read:

SELECT PageVisited,Cou nt(PageVisited) AS CountOfPageVisi ted
FROM tblPageViews
WHERE StartTime Between #11/1/2004 0:0:1# And #11/1/2004
23:59:59#
GROUP BY PageVisited, StartTime


the results change completely. For example, I know the default.asp
was viewed nearly 5000 times within the date range, but it appears
twice in the second resultset, both times with a count of 1.

Incidentally, I have taken into account that the results will differ
between the 2 examples as no criteria restrictions are placed on the
first query.

Am I being idiotic in relying on Access's Query Wizard?


No, just learn to use it correctly. You can choose "Where" in the Total row
in the grid, instead of one of the aggregation functions (Min, Sum, etc).
That's what you should choose in the StartTime column in the grid (You'll
need to create a second column containing StartTime, since you are both
filtering and grouping by it. To tell you the truth, I'm not sure why you
are grouping by StartTime ...).

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 21 '05 #2

"Bob Barrows [MVP]" <re******@NOyah oo.SPAMcom> wrote in message
news:Oj******** *****@tk2msftng p13.phx.gbl...
Paxton wrote:
Hi,

I'm trying to display the total page views per page within a given
date range, but the correct SQL is seemingly beyond me. I get the
correct result with a straightforward Group By and Count clause eg
SELECT DISTINCT tblPageViews.Pa geVisited,


You should get rid of the DISTINCT keyword. GROUP BY is already insuring
distinct records...
Count(tblPageVi ews.PageVisited ) AS CountOfPageVisi ted
FROM tblPageViews
GROUP BY tblPageViews.Pa geVisited;

but as soon as I introduce some date criteria:
SELECT DISTINCT tblPageViews.Pa geVisited,
Count(tblPageVi ews.PageVisited ) AS CountOfPageVisi ted
FROM tblPageViews
GROUP BY tblPageViews.Pa geVisited, tblPageViews.St artTime
HAVING tblPageViews.St artTime Between #11/1/2004 0:0:1# And
#11/1/2004 23:59:59#;


This criterion should be put in the WHERE clause, since it has nothing to
do
with the aggregated data. Criteria in the WHERE clause are enforced BEFORE
the data is grouped and aggregated. Criteria in the HAVING clause are
enforced AFTER the grouping and aggregation. Whenever possible, you should
put the criteria in the WHERE clause, since this will minimize the number
of
records that the grouping engine will work with, improving performance.
Your
query should read:

SELECT PageVisited,Cou nt(PageVisited) AS CountOfPageVisi ted
FROM tblPageViews
WHERE StartTime Between #11/1/2004 0:0:1# And #11/1/2004
23:59:59#
GROUP BY PageVisited, StartTime


the results change completely. For example, I know the default.asp
was viewed nearly 5000 times within the date range, but it appears
twice in the second resultset, both times with a count of 1.

Incidentally, I have taken into account that the results will differ
between the 2 examples as no criteria restrictions are placed on the
first query.

Am I being idiotic in relying on Access's Query Wizard?


No, just learn to use it correctly. You can choose "Where" in the Total
row
in the grid, instead of one of the aggregation functions (Min, Sum, etc).
That's what you should choose in the StartTime column in the grid (You'll
need to create a second column containing StartTime, since you are both
filtering and grouping by it. To tell you the truth, I'm not sure why you
are grouping by StartTime ...).


I was grouping by StartTime because, initially, the Wizard told me I had to
include it in my aggregate function. I didn't want to - and your
explanation has shown me how to get rid of it, and what I was doing wrong.
I got rid of the first (Groupby) StartTime column, retained your (Where)
StartTime column and got the results I wanted. Many thanks for your help,
Bob.

Paxton
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5419
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB 7.2 environment, the choices the optimizer makes often seem flaky. But this last example really floored me. I was hoping someone could explain why I get worse response time when the optimizer uses two indexes, than when it uses one. Some context:
2
2503
by: Mark | last post by:
I am producing a report that requires me, among many other things, to calculate the average contracts for about 1000 companies during the yr 2003. The "quirk" is that all the companies do not have the same number of months. When using DB2 the below code counts the average contracts of each group for 2003. What code can I use in MS ACCESS to produce the same result? SELECT COMPANY_NAME,
2
15817
by: Clark Savage Jr | last post by:
I am running a bunch of aggregate insert queries to populate a table. When I run the SQL statement via a saved query, all is well. However, when I run it as an ADO recordset in VBA, the count is one greater than the actual number of records. Out of over 15 queries, only two do this and there does not appear to be anything special about any of the records that satisfy the criteria. Any ideas?! (Code below) Fred
9
1612
by: Warren | last post by:
I am trying to create a report that does the following: Access Data in Query: NAME | DATE | SALE TYPE | ------------------------- John DOE | 1282003 | TYPE A Jane DOE | 1282003 | TYPE C Jane DOE | 1282003 | TYPE D Jane DOE | 1282003 | TYPE C
3
5023
by: Dagpauk | last post by:
Assume the following table holding information about the planning date and execution date for an imaginary "objects" ObjectPlan ObjectID PlannedDate ExecutedDate 1 19.03.04 28.03.04 2 19.03.04 24.03.04 3 19.03.04 19.04.04 1 19.04.04 20.04.04
7
8869
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
11
6600
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
1
14493
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT aggregate function, but there is no reference, at least that I can find anywhere, of how to do this. I have multiple lines fields for which I would like to do a "count distinct", but for simplicity, I am showing an example of only one field. Here is...
4
12441
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
9568
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
10007
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
9959
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
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5277
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.