SQL query with count

Posted on 2008-10-21
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
LVL 16

Accepted Solution

brad2575 earned 125 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 125 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

733 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