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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now