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

using NZ to count a number of items in a filed and show 0 as well

3
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');
Jan 13 '17 #1
6 1102
MikeTheBike
639 Expert 512MB
Hi

I don't think trying to Count() is the corrct function here.
I would use something like this
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Pilots.BaseResidence, Sum(IIF(IsNull(tbl_Pilots.BaseResidence),0,1)) AS CountOfBaseResidence 
  2. FROM tbl_Pilots
  3. GROUP BY tbl_Pilots.BaseResidence;
  4.  
HTH


MTB
Jan 13 '17 #2
itzikd
3
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.

Jan 13 '17 #3
jforbes
1,107 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   BaseResidence
  3. , DCount("BaseResidence", "tbl_Pilots", "BaseResidence='" & [BaseResidence] & "'") AS CountOfBaseResidence 
  4. FROM tblResidence
Jan 13 '17 #4
itzikd
3
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!
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Pilots.BaseResidence, DCount("BaseResidence","tbl_Pilots","BaseResidence='" & [BaseResidence] & "'") AS CountOfBaseResidence
  2. FROM tbl_Pilots;
Jan 13 '17 #5
PhilOfWalton
1,430 Expert 1GB
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:-

Expand|Select|Wrap|Line Numbers
  1. SELECT Residence.Residence, Count(Pilot.PilotName) AS CountOfPilotName
  2. FROM Pilot RIGHT JOIN Residence ON Pilot.ResidenceID = Residence.ResidenceID
  3. GROUP BY Residence.Residence;
  4.  
Jan 13 '17 #6
jforbes
1,107 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT Distinct BaseResidence FROM tbl_Pilots
Then you can write either this Query:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    BaseResidence
  3.  , DCount("BaseResidence", "tbl_Pilots", "BaseResidence='" & [BaseResidence] & "'") AS CountOfBaseResidence 
  4.  FROM qry_BaseResidence
Or this one based on Phil's suggestion:
Expand|Select|Wrap|Line Numbers
  1. SELECT qry_BaseResidence.BaseResidence , Count(tbl_Pilots.PilotName) AS CountOfPilotName
  2. FROM tbl_Pilots RIGHT JOIN qry_BaseResidence ON tbl_Pilots.BaseResidence  = qry_BaseResidence.BaseResidence 
  3. GROUP BY qry_BaseResidence.BaseResidence ;
Beware, there may be typos.
Jan 14 '17 #7

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

Similar topics

1
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.
0
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...
1
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...
5
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...
11
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
2
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...
5
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,...
1
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...
3
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...
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:
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...
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
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,...
0
jinu1996
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...

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.