473,386 Members | 1,733 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,386 software developers and data experts.

Group Level Count

I can't seem to find a straight answer for my specific issue.
Any help would be appreciated.

I would like to count the various items in a table where the fields
have a 'group' relationship.
I would like to count the items per group.
(Please forgive the inappropriate use of database terms.)

An example follows:

F1 F2 F3
------------------------------
a 1 A
a 1 B
a 5 A
a 3 C
b 7 A
b 7 C
b 2 D
b 2 E
b 5 A
b 3 A
c 4 E
c 1 C
c 3 B

The information I need is...

F1 F2 F3
-------------------------------
a 3 4
b 4 6
c 3 3

Analysis:
---------------------------------------------------------------
The example uses simplified data, however, neither field has items that
could be ordered consecutively.

Of course, the 'c' group has one F2 item with only one F3 item.
I find this easy to query with a group by clause.
However, as you can see with group 'a' this is not the case.

Generally it seems easy (with a group by) to get the count on the
final level (field) with respect to any 'higher' level, however it
escapes me on how to get the 'in between' levels too, grouping of
course, on the highest level.
This way you have a count of each 'lower' level with respect to the
highest level in the query.
I suspect that I would have to have a 'sub query' for each 'in between'
level, looking up the key value and counting the items as usual.
In Access 2000, I tried a Dlookup on a 10k record table and this was
somewhat slow. I need to do this for multiple levels (>=3) and this
would become VERY SLOW. (I only tried it on one level, though.)

Is there is a general discussion on this issue?
In general, is there a way to do this for any amount of levels?
Is this possible, with an elegant query, or will it involve multiple
queries/sub queries?
I'm using Access 2000, however, any additional comments with respect to
SQL Server (cursors, perhaps) is ok.

Finally, I'm not sure how this complicates the issue, the Fields are
from joined tables, (I think outer right join) where there can be
nulls, therefore, the null should not be counted.

Any advice would be greatly appreciated.

TIA

Nov 13 '05 #1
2 3120
<ce*******@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I can't seem to find a straight answer for my specific issue.
Any help would be appreciated.

I would like to count the various items in a table where the fields
have a 'group' relationship. [...] The information I need is...

F1 F2 F3
-------------------------------
a 3 4
b 4 6
c 3 3


Sure the above table of info is correct?
If so, how do you calculate them?

Bruno
Nov 13 '05 #2
ce*******@yahoo.com wrote:
I can't seem to find a straight answer for my specific issue.
Any help would be appreciated.


Using your example (called tblExample),

qry1:
TRANSFORM Count(tblExample.F3) AS [The Value] SELECT tblExample.F1,
Count([The Value]) AS DistinctF2Count, Sum([The Value]) AS
DistinctF1Count FROM tblExample GROUP BY tblExample.F1, tblExample.F2
PIVOT tblExample.F1;
qry2:
SELECT qry1.F1, Sum(qry1.DistinctF2Count) AS F2,
Sum(qry1.DistinctF1Count) AS F3 FROM qry1 GROUP BY qry1.F1;

yielded:
F1 F2 F3
a 3 4
b 4 6
c 3 3

When the last F3 value was nulled out in tblExample it yielded:
F1 F2 F3
a 3 4
b 4 6
c 3 2

When I changed qry1 to:
TRANSFORM Count(tblExample.F3) AS [The Value] SELECT tblExample.F1,
Count([The Value]) AS DistinctF2Count, Sum([The Value]) AS
DistinctF1Count FROM tblExample WHERE tblExample.F2 IS NOT NULL GROUP
BY tblExample.F1, tblExample.F2 PIVOT tblExample.F1;

and additionally nulled out an F2 value (10th one) I got:
F1 F2 F3
a 3 4
b 3 5
c 3 2
I'm not sure whether this technique can be generalized to multiple
levels or not but I start thinking about crosstab queries immediately
now whenever an 'in between' level grouping is desired. I didn't test
many cases where null values were involved but I feel confident that
it's close to what you need when grouping a few levels. Also, your
example counts didn't seem to differentiate duplicate F3 values.

James A. Fortune

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joshua Moore-Oliva | last post by:
I have a query that is asking me to GROUP a column, yet when I GROUP it it causes an error near GROUP. What is very strange about the following query is that the line list_size_active =...
0
by: Lester Knutsen | last post by:
Washington Area IBM Informix/DB2 User Group meeting - June 7, 2005 -------------------------------------------------------------- Mark the date, our next user group meeting will be another...
4
by: BJ | last post by:
Can anyone tell me how to include a count of group records in a group footer. I know (I think I know!) that I have to include an unbound text box in the footer, but what expressions do I use to...
7
by: SueB | last post by:
Greetings. I have a report based on the following query (hang in there ... it's quite long): SELECT Year(.) AS Yr, tblEvents.eventID, tblEvents.eventname, tblEvents.eventhost,...
7
by: Darin | last post by:
I have a report that sub-totals on a group, then grand-totals at the report footer. If there's only one group, the sub-total and grand total are redundant, so I only want to show one of them. I...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
10
by: Rudolf Bargholz | last post by:
Perhaps some kind soul could help me out with an SQL I have been trying all day to get to work, where one colum is just not summing up the way I want it to. I have the following data GRP_SEQ ...
0
by: bluefalcon904 via AccessMonster.com | last post by:
Hi... Can someone please help me? Im trying to run a report using the query below... SELECT & "_" & AS , dbo_SDB_ABM0270.KYKMEIKT, dbo_viewPOMS_RegRep., dbo_viewPOMS_RegRep.Account,...
5
by: Seb | last post by:
I want to count activity in a given month. I'm trying to do so with the linq code below however it reports: Error 1 'a' is inaccessible due to its protection level var ActivityByMonths = from a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.