Solved

SQL query with count

Posted on 2008-10-21
4
352 Views
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
0
Comment
Question by:rdillion
4 Comments
 
LVL 16

Accepted Solution

by:
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.
0
 
LVL 9

Expert Comment

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

0
 
LVL 3

Assisted Solution

by:richard_crist
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.
0
 

Author Closing Comment

by:rdillion
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!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Read about achieving the basic levels of HRIS security in the workplace.
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now