473,479 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple counts in a query

34 New Member
I would like three counts in one query based on differing criteria. Each count is counting the same items. The difference between the items being counted is the characteristics (eg like to short).

I have created a three separate queries (with the help of the query wizard) that perform this. Can they be combined into one? Or should I be using some other function?

This is the query
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW tblOrders.JobID, tblOrders.JobSubID, tblFloors.Floor, Count(*) AS [Total Number of Panels]
  2. FROM (tblOrders INNER JOIN tblFloors ON tblOrders.JobOrderID = tblFloors.JobOrderID) INNER JOIN tblPanels ON tblFloors.FloorID = tblPanels.FloorID
  3. GROUP BY tblOrders.JobID, tblOrders.JobSubID, tblFloors.Floor, tblOrders.JobOrderID, tblFloors.FloorID;
  4.  
the other queries have a WHERE clause
Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblPanels.Height) Between 72 And 145.125))
  2.  
and
Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblPanels.Length)<48) AND ((tblPanels.Height) Between 72 And 145.125))
  2.  
Feb 7 '07 #1
8 2411
MSeda
159 Recognized Expert New Member
you could add the last two counts to the first query by using Dcount.

=DCount("[JobID]"," myQuery"," (tblPanels.Height) Between 72 And 145.125")
Feb 8 '07 #2
Tetelestai
34 New Member
Having difficulty implementing this. I most likely have not given enough information.

Note I'm using A 2K

The original query is a summary query that is counting the number of panels in tblPanels that belongs to a floor in tblFloors which belongs to a order in tblOrders.

Here is the code with Dcount. Right now it is counting all the panels that are between that range, not seperating them per Floor or Order
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW tblOrders.JobID, tblOrders.JobSubID, tblFloors.Floor, Count(*) AS [Total Number of Panels], Nz(DCount("[JobID]","qryPanelsSummary3","(tblPanels.Height) Between 72 And 145.125")) AS [Number of Panels]
  2. FROM (tblOrders INNER JOIN tblFloors ON tblOrders.JobOrderID = tblFloors.JobOrderID) INNER JOIN tblPanels ON tblFloors.FloorID = tblPanels.FloorID
  3. GROUP BY tblOrders.JobID, tblOrders.JobSubID, tblFloors.Floor, tblOrders.JobOrderID, tblFloors.FloorID;
  4.  
Am I using dcount properly?
Feb 8 '07 #3
Tetelestai
34 New Member
I will also give table structure

tblOrders

JobOrderID
JobID
JobSubID
Model

tblFloors
FloorID
JobOrderID
Floor

tblPanels
PanelID
FloorID
Panel
Length
Height
Feb 8 '07 #4
MSeda
159 Recognized Expert New Member
since you gave info on all your tables I decided to build a test on your actual table structure. the SQL i ended up with looked like this
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW tblOrders.JobOrderID, tblOrders.JobID, tblOrders.JobSubID, tblFloors.FloorID, Nz(DCount("[PanelID]","[tblPanels]","[FloorID] = '" & [tblfloors].[FloorID] & "'")) AS ttlno, Nz(DCount("[PanelID]","[tblPanels]","[Length]<10 AND [FloorID] = '" & [tblfloors].[FloorID] & "'")) AS ttlShort, Nz(DCount("[PanelID]","[tblPanels]","[Length]>=10 AND [FloorID] = '" & [tblfloors].[FloorID] & "'")) AS ttlLong
  2.  
  3. FROM (tblOrders INNER JOIN tblFloors ON tblOrders.JobOrderID = tblFloors.JobOrderID) INNER JOIN tblPanels ON tblFloors.FloorID = tblPanels.FloorID
  4.  
  5. GROUP BY tblOrders.JobOrderID, tblOrders.JobID, tblOrders.JobSubID, tblFloors.FloorID, Nz(DCount("[PanelID]","[tblPanels]","[FloorID] = '" & [tblfloors].[FloorID] & "'")), Nz(DCount("[PanelID]","[tblPanels]","[Length]<10 AND [FloorID] = '" & [tblfloors].[FloorID] & "'")), Nz(DCount("[PanelID]","[tblPanels]","[Length]>=10 AND [FloorID] = '" & [tblfloors].[FloorID] & "'"));
  6.  
I used dcount for all of the counting to simplify things I also based mine on simpler criteria but you should easily be able to swap in your own. the syntax in domain aggregates can be tricky, you may want to review a thread that explains that topic better.
It looked like you were basicaly counting Panels per Floor so thats how I set up my test query if you want to count panels per job the sql would be slightly different.
See how you can make this work for you and let us know.
Megan
Feb 8 '07 #5
Tetelestai
34 New Member
Thanks Megan works great.

The ' character created a datatype mismatch on mine.

Here is my final code (for posterity)
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW tblOrders.JobSubID, tblOrders.Model, tblOrders.JobID, tblFloors.Floor, Nz(DCount("[PanelID]","[tblPanels]","[Length]<48 AND [HEIGHT]>72 AND [HEIGHT]<145.125 AND [FloorID] = " & [tblfloors].[FloorID] & "")) AS [Short Panels], IIf([Line Panels]=0,"-",Format([Short Panels]/[Line Panels],"0%")) AS Percentage, Nz(DCount("[PanelID]","[tblPanels]","[HEIGHT]>72 AND [HEIGHT]<145.125 AND [FloorID] = " & [tblfloors].[FloorID] & "")) AS [Line Panels], Nz(DCount("[PanelID]","[tblPanels]","[FloorID] = " & [tblfloors].[FloorID] & "")) AS [Total Panels]
  2. FROM (tblOrders INNER JOIN tblFloors ON tblOrders.JobOrderID = tblFloors.JobOrderID) INNER JOIN tblPanels ON tblFloors.FloorID = tblPanels.FloorID
  3. GROUP BY tblOrders.JobSubID, tblOrders.Model, tblOrders.JobID, tblFloors.Floor, tblOrders.JobOrderID, tblFloors.FloorID;
  4.  
If I need it by Job I will give it a go before I ask, and have a working example now to play with.
Feb 8 '07 #6
Tetelestai
34 New Member
In the above code ^^

Is it advisable not to put formating in the expression, but instead place formating in the text box control where it is being displayed?
Feb 8 '07 #7
MSeda
159 Recognized Expert New Member
Glad it worked out for you
Feb 8 '07 #8
NeoPa
32,556 Recognized Expert Moderator MVP
Another concept you could use (more efficient - but that will only be important for fairly large numbers of records) is to have :
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS NoOfPanels, Sum(IIf(tblPanels.Height Between 72 And 145.125,1,0)) AS NoOfHighPanels,...
Obviously change the names and criteria to fit your needs.
Feb 10 '07 #9

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

Similar topics

5
3701
by: WertmanTheMad | last post by:
Ive been playing with this for a few days and thought I might thow it out for seggestions. I have Several Queries that need counts returned The Queries are Mutually Exclusive meaning whatever...
4
10621
by: randy.p.ho | last post by:
Using JDBC, is there a way to call a stored procedure with multiple return values? Thanks.
4
8078
by: Jim | last post by:
I'm having trouble doing multiple counts with groups, ordering, and joins. Can someone help me out? Below is two table samples and what I'm trying to get my output to be: TABLEA ID ...
1
3103
by: Randy K | last post by:
I have a table with some 35000 records and I need some help sorting it out. The goal is to get counts of failures modes oraganized by serial number. the table is set up roughly like this. s/n ...
1
3429
by: Chris Wolfe | last post by:
I have two fields that draw their combobox values from the same table. LogBook04.Insurance is the Primary Insurance and LogBook04.SecIns is the Secondary Insurance. Both draw their values from...
7
3753
by: Mischa | last post by:
Hello, I am trying to use realloc multiple times to extend an array of doubles but unfortunatly it keeps failing. I think I am mixing up the size to which the old memory block needs to be...
1
2223
by: josecruz | last post by:
I have to create a summary report that will provide counts and # average of days for multiple "Status" by entering different dates for every criteria. I have created several queries that calculates...
3
3627
by: Bill Hutchison | last post by:
I have a query that returns different results (3508 rows for snapshot, 6288 for dynaset) and that is the only thing I change to get the different results. When I try to make a table from the...
9
20305
by: ncyankeefan | last post by:
I am trying to get the number of specific alerts for each month of the year. I would like to get these counts in one query or subquery. There is a date field in table so I can specifically get the...
0
7027
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
7019
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
6847
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...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.