Counting records


Count is an aggregate function which returns the number of records held in a table or a query. It only returns the observations which are NOT NULL.


EXAMPLE DATABASE: Nhanes

DATA INPUT

Table nhanes in the database Nhanes.mdb is used as input to the query.



QUERY count_records

SELECT ethn, sex, COUNT(height) AS height_observation, 
                  COUNT(weight) AS weight_observation, 
                  COUNT(*) AS total_observations
FROM nhanes
GROUP BY ethn, sex
ORDER BY ethn, sex;

The query count_records lists the number of height and weight observations, and the total number of observations per ethnicity and sex.



DISCUSSION

The query counts the number of height and weight records using the COUNT( ) function. It excludes all the NULL records. Count function aggregates the result by ethnicity and sex. The statement count(*) returns all the records that is included in the nhanes table.


Last modified September 23, 2008 7:23 am