473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How do I count subrecords returned from a query?

What started as a simple requirement is turning into something rather more complex. In the following (simplified) query:

Expand|Select|Wrap|Line Numbers
  1. Select field1, field2
  2. FROM table
  3. WHERE ....
  4. GROUP BY field1, field2
  5.  
Which produces a result along the lines:

Expand|Select|Wrap|Line Numbers
  1. field1  field2
  2. aa    1
  3. aa    6    
  4. aa    1000
  5. bb    10    
  6. bb    25
  7.  
How do I include a count of the number of aa and bb records produced as part of the rows returned?

I tried using count(field1) but the result returns the count of field1/field2 rows (ie 1 in each of the above cases).

I found a convoluted way by outputing the results into a result table using SELECT INTO and running a seperate query to determine the counts but then found this did not work when I tried to open a recordset in VBA on the SELECT INTO (I get error number 3219: Operation Invalid).

So I then tried to use OpenQuery to create the results table. However, I think I have learned from these pages that I can't run a stored action query from VBA using the OpenQuery method and pass parameters to it in the same way as I pass parameters to a stored "non action" query when opening a recordset, so I've ended up with something like:

Expand|Select|Wrap|Line Numbers
  1. Select field1, count(field1)
  2. From (Select field1, field2
  3. FROM table
  4. WHERE ....
  5. GROUP BY field1, field2)
  6.  
Which seems to work but I then have to generate two recordsets in the VBA to achieve what I set out to do. It feels overly complicate !!
Sep 8 '10 #1
1 1202
Stewart Ross
2,545 Expert Mod 2GB
What you've arrived at using a subquery is correct, and is not over-complicated really.

There are two distinct levels of aggregation involved: aggregation by field 1 along with field 2 (the example you show), and aggregation by field 1 alone. The set of records returned by the field 1 and field 2 grouping is a subset of that for field 1 alone, so there is no single-query way within that subset to count the larger set from which it was taken.

In summary, a two-query or query/subquery approach is correct for this kind of multiple aggregation requirement, so well-done for coming up with a good solution!

-Stewart
Sep 12 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Toby Dann | last post by:
I have an aggregate query as recordsource for a form to show a list of invoices, including the totals (calculated by the aggregate query - I'm trying to duplicate as little info as possible here)....
6
by: edwin | last post by:
I'm creating a database (information has been entered already) where I want to count some subrecords linked by a record. An example: TABLE1: ID | TITLE 1 | TEST 2 | TEST2 TABLE2: ID |...
1
by: Bill | last post by:
I'm trying to write a query that will select a distinct count of more than one field. I have records that display user productivity. Each of the records have a time associated with it and I want to...
5
by: jslssuze | last post by:
I created a database with registration information and my query works well on simply the count of people registered. Instead of count, however, my clients wants number of spaces available (i.e....
1
by: veaux | last post by:
Might not have explained this correctly in subject, but query results look like below: Name ID Phone Bill 001 123 Bill 001 234 Bill 001 ...
5
by: Genalube | last post by:
I am trying to count the number of owners that show up in a query (conveyQuery). The query will produce a column OwnName that will contain names like John Smith, Mike Jones, Frank Vaugn. Each of...
1
by: wilmers | last post by:
I am attempting to add a running count to my query so that i can identify the batches of results returned within my application. I am fairly new to SQL and would appreciate any advice.
4
by: vba challenge | last post by:
I have an attendance table that list the status of each student on a particular day. e.g. P - present A for absent and L for Late. I want to run a query where I count both the number of presents...
4
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Here is what I have: private int NationalCount() { Int32 numRecords = 0; using (SqlConnection dataConnection = new SqlConnection(GlobalVars.sqlConnString)) { SqlCommand dataCommand = new...
2
by: dmne05974 | last post by:
As a novice in Access i am currently writing a database to track certain financial information for a non-profit organisation. As part of the funding they have to monitor age ranges and ethnic...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.