understanding SQL counting and grouping

miconian

Well-known Member
Joined
Aug 18, 2004
Messages
769
So I have a table called [2012 raw], and it has many fields, two of which are [Capture ID] and [Internet Site].

I want to count how many internet sites there are per capture ID.

So I created this query:

Code:
SELECT [2012 raw].[Capture ID], Count([2012 raw].[Internet Site]) AS [CountOfInternet Site]
FROM [2012 raw]
GROUP BY [2012 raw].[Capture ID];

But the counting numbers are way too high. Clearly, there is some kind of double counting going on.

Now, there are other fields besides the ones listed above. Lots of them. And there are some pairs of records that have the same data for Internet Site and Capture ID, but different data for the other fields.

But those other fields are not included in the query. I'm confused about how to prevent the double counting.

So what I end up doing in cases like this, is that first I'll do a query where I just group both fields, like this:

Code:
SELECT [2012 raw].[Capture ID], [2012 raw].[Internet Site]
FROM [2012 raw]
GROUP BY [2012 raw].[Capture ID], [2012 raw].[Internet Site];

...and then I'll run that as a make-table, and then I'll run the count query against that new table, and then I'll get the results I want.

Is that how it's typically done? Obviously I'm not very good at SQL and need help understanding the order of operations.

thanks...
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,
You are on the right track with your solution described above - the idea to first group the items, then count the grouped items...

Another solution:
Code:
SELECT t.[Capture ID], Count(t.[Internet Site]) As [CountOfInternet Site]
FROM (
    SELECT DISTINCT [2012 raw].[Capture ID], [2012 raw].[Internet Site]
    FROM [2012 raw]
    ) AS t
GROUP BY t.[Capture ID];

Using Distinct in the subquery creates the subset of unique captureID-InternetSite items, which you then can count.

Note that in Access I quite often use two physical queries rather than a query with a subquery - so I'd say your original solution is good on that score too. Access doesn't always perform well with subqueries.

HTH,
ξ
 
Last edited:
Upvote 0
What do you see if you run this query?
Code:
SELECT [2012 raw].[Capture ID], [2012 raw].[Internet Site]
FROM [2012 raw]
ORDER BY 1,2;
Do you see what looks like a lot of duplicate records? If so, that is because you have a lot of records that may have these two fields the same, and the other fields you are leaving out may be different. It is important to understand that not displaying those other fields does NOT alter the record count in any way.

Now, if you just want to count the number of different, unique Internet Sites associated with each Capture ID (weeding out all the duplicates), you will need to get rid of those duplicates first (you can use an Aggregate Query or use the DISTINCT statement with your select), So you will either need to do this in two queries, or one nested query. The nested query might look something like this:

Code:
SELECT [Query1].[Capture ID], Count([Query1].[Internet Site]) as [CountOfInternet Site]
FROM
(SELECT DISTINCT [2012 raw].[Capture ID], [2012 raw].[Internet Site]
FROM [2012 raw]) as Query1
GROUP BY [Query1].[Capture ID];
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,031
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top