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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 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