i have a table of pilots that have 4 places of ressidence now i have a qry that is suppose to show how many pilots are in each ressidence , i tryed using the simple count funcion but it wont show ressidence if no pilots lives there.
i want the qry to show 0 if no pilots leave in that place
i tryed using Expr1: Nz(Count([BaseResidence],'0') but it won display anything.
this is the sql
SELECT tbl_Pilots.BaseResidence, Count(tbl_Pilots.BaseResidence) AS CountמתוךBaseResidence, Nz([BaseResidence],'0') AS Expr1
FROM tbl_Pilots
GROUP BY tbl_Pilots.BaseResidence, Nz([BaseResidence],'0');
6 1102
Hi
I don't think trying to Count() is the corrct function here.
I would use something like this - SELECT tbl_Pilots.BaseResidence, Sum(IIF(IsNull(tbl_Pilots.BaseResidence),0,1)) AS CountOfBaseResidence
-
FROM tbl_Pilots
-
GROUP BY tbl_Pilots.BaseResidence;
-
HTH
MTB
Hello thanks for your help but this still doesnt work.
here is a picture of the data fields and the qry
as you can see it shows what i put but i have also 2 more fields and it doesnt show 0 with them.
If you want a record for every Residence, then you should select those records first, then either Join to an Aggregate Query of the Counts, or perform a DCount() as a SubQuery.
This method is pretty simple. Use whatever Table that the ComboBox in the lower left of post #3 is based on to start with as it looks like it has a list of all the Residence records. I'm assuming the Table is tblResidence, then create a Query similar to this, but with your actual field and table names: - SELECT
-
BaseResidence
-
, DCount("BaseResidence", "tbl_Pilots", "BaseResidence='" & [BaseResidence] & "'") AS CountOfBaseResidence
-
FROM tblResidence
thanks again but that doesnt work. im kinda new to access so maybe im doing something wrong
i used this code you sent, but now it shows me twice the base i have 2 pilots in, and once the base i have 1 pilot in, but it doesn't show the 2 other bases that have no pilots
table name is: tbl_Pilots
inside it i have: BaseResidence
which has 4 option for 4 basses: we can call them x y z u
i want to do a qry that shows
x 2
y 1
z 0
u 0
where xyzu are the basses name and 2 1 0 0 is the number of pilots in them
this is the code from qry, thanks again! - SELECT tbl_Pilots.BaseResidence, DCount("BaseResidence","tbl_Pilots","BaseResidence='" & [BaseResidence] & "'") AS CountOfBaseResidence
-
FROM tbl_Pilots;
Sorry to but in folks, but the database is not normalised and that doesn't help.
There should be a table of Residences and a table of Pilots with a Foreign Key pointing to their Residence.
A very rough guide to normalisation is that Pilots exist in their own right and a table should contain only information about pilots.
(Name, age sex etc)
Residences exist in their own right and have absolutely nothing to do with pilots. I live in a residence. TheResidence table will have information like address, land line number etc.
Now to associate a pilot with a residence you need a pointer in the Pilot table - ResidenceID
If you do that, I think something like this will work:- -
SELECT Residence.Residence, Count(Pilot.PilotName) AS CountOfPilotName
-
FROM Pilot RIGHT JOIN Residence ON Pilot.ResidenceID = Residence.ResidenceID
-
GROUP BY Residence.Residence;
-
I assumed there would be a Table named something like tblResidence that was the ControlSource of the ComboBox shown in the Picture. Sounds like that is not the case, since you based the Query on tbl_Pilots.
Like Phil has posted, Normalizing the Residences into their own table would probably be best and his solution should work well if you are up to adding the Detail.
If you still want to create your query based on your un-normalized table, this might work for you. Create a Query called qry_BaseResidence : - SELECT Distinct BaseResidence FROM tbl_Pilots
Then you can write either this Query: - SELECT
-
BaseResidence
-
, DCount("BaseResidence", "tbl_Pilots", "BaseResidence='" & [BaseResidence] & "'") AS CountOfBaseResidence
-
FROM qry_BaseResidence
Or this one based on Phil's suggestion: - SELECT qry_BaseResidence.BaseResidence , Count(tbl_Pilots.PilotName) AS CountOfPilotName
-
FROM tbl_Pilots RIGHT JOIN qry_BaseResidence ON tbl_Pilots.BaseResidence = qry_BaseResidence.BaseResidence
-
GROUP BY qry_BaseResidence.BaseResidence ;
Beware, there may be typos.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: vic |
last post by:
Hello, Dean
Try this:
select distinct c1, c2 into #tmp_1 from t1
select count(*) as cnt from #tmp_1
drop table #tmp_1
With best regards.
|
by: DataFreakFromUtah |
last post by:
Hello!
No question here, just a procedure for the archive.
Search critera:
count records imported count data imported count number of rows
imported
count number of records imported record import...
|
by: Aaron West |
last post by:
Try this script to see what queries are taking over a second.
To get some real output, you need a long-running query. Here's one
(estimated to take over an hour):
PRINT GETDATE()
select...
|
by: isabelle |
last post by:
hi, every body..
I have two program I couldn’t solve them
So, can any body help me. please!!
1-Write a program that accepts a character and count number of occurrences in a file.
The file...
|
by: Mack |
last post by:
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
Thanks in advance,
-Mukesh
|
by: mfaisalwarraich |
last post by:
Hi Everybody,
I am using the following code to get the recordset of an external database.
Dim dbPatients As Database
Dim rsCountPatients As Recordset ' to count number of...
|
by: jambonjamasb |
last post by:
I am wanting to create a report that summarises the number of items within a date range.
For example I have a FIELD called System_Change.
This is a drop down COMBOBOX that uses words like unix,...
|
by: jlt206 |
last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below)
I want to place the current number into a variable $MemberNo or into a FormField to be sent...
|
by: Miguel Valenzue |
last post by:
I want to count the number of items that occur within a range of time throughout different days.
My original data comes from a machine that logs the time a car passes a point in a road over...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |