SQL query with count

Posted on 2008-10-21
Medium Priority
Last Modified: 2013-11-30
Hello Experts,
I am trying to write a sql query with a count result, and am running into a problem.  It has been a while since I've written much sql, and am not sure if this is something that can be done in a single statement or not.  I am trying to track the number of hits to a website, and determine the number of hits from different sites.  I have a very basic table with colunms ConnectingSite (with values equal to SiteA, SiteB, SiteC, SiteD, or ExternalSite), ConnectingTime, and RemoteIPAddress.  I am trying to query that table for the total number of connections from each site.  I have tried to use COUNT (DISTINCT ConnectingSite) but have seen some information that Access doesn't support COUNT (DISTINCT ....), and am not even sure if that would work.  Thanks in advance for any insight you can provide.
Russ Dillion
Question by:rdillion
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Accepted Solution

brad2575 earned 500 total points
ID: 22768281
select ConnectingSite , count(ConnectingSite )
From TableName
Group By ConnectingSite

The group by will get the counts by each type of site.

Expert Comment

ID: 22769215
select count(*) from
   select ConnectingSite
   From TableName
   Group By ConnectingSite
) AA


Assisted Solution

richard_crist earned 500 total points
ID: 22769276
"I have a very basic table with columns ConnectingSite (with values equal to SiteA, SiteB, SiteC, SiteD, or ExternalSite), ConnectingTime, and RemoteIPAddress.  I am trying to query that table for the total number of connections from each site."

I believe the following might work:

select ConnectingSite, count(*) from mytable group by ConnectingSite;

The above will work if you have one column with differing values.  If you actually have multiple columns (which it does not sound like you do) then it might get more complicated.

Also, you can add more than one column to get counts of combinations.  If you had ConnectingSite and ConnectionType, for instance, you could do the following to get counts for each combination of ConnectingSite and ConnectionType:

select ConnectingSite, ConnectionType, count(*) from mytable group by ConnectingSite, ConnectionType;

count, min, max and other functions like that are called aggregate functions and have many uses.  You just have to get familiar with the concept as implemented in most SQL systems.

Author Closing Comment

ID: 31508411
brad2575, richard crist;
Thank you very much for your help.  You were both dead on.  I awarded points to both, as brad2575's response was returned quickly and richard crist's added some extra insight.  I hope this is acceptable.
Thanks again!!

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question