Another possible approach would be a UNION ALL operation using appropriate
text constants as the 'category' for each count:
SELECT "Type 1 diabetes" AS [Category],
COUNT(*) AS [Number of Patients]
FROM [Patients]
WHERE [Condition] = "Type 1 diabetes"
UNION ALL
SELECT "Type 2 diabetes",
COUNT(*) FROM [Patients]
WHERE [Condition] = "Type 2 diabetes"
UNION ALL
SELECT "Gestational diabetes 1-18 years",
COUNT(*) FROM [Patients]
WHERE [Condition] = "Gestational diabetes"
AND GetAge([DateOfBirth]) <= 18
UNION ALL
SELECT "Gestational diabetes 19+ years",
COUNT(*) FROM [Patients]
WHERE [Condition] = "Gestational diabetes"
AND GetAge([DateOfBirth]) >= 19
UNION ALL
SELECT "Exercise Rated 1",
COUNT(*) FROM [Patients]
WHERE [ExerciseRating] = 1
UNION ALL
SELECT "Exercise Rated 2",
COUNT(*) FROM [Patients]
WHERE [ExerciseRating] = 2
UNION ALL
SELECT "Exercise Rated 3",
COUNT(*) FROM [Patients]
WHERE [ExerciseRating] = 3
UNION ALL
SELECT "Exercise Rated 4",
COUNT(*) FROM [Patients]
WHERE [ExerciseRating] = 4
UNION ALL
SELECT "Exercise Rated 5",
COUNT(*) FROM [Patients]
WHERE [ExerciseRating] = 5;
Where Patients is the table name and Condition is a column of text data type
with the patient's medical condition, DateOfBirth is a column of date/time
data type with the patient's date of birth, and ExerciseRating is the column
of integer number data type with the patient's assessment of their exercise
taken.
The age of the patient is computed from their date of birth by the following
function, which should be pasted into a standard module in the database:
Public Function GetAge(varDob, Optional varDateAt)
Dim intYears As Integer
' return date at current date if no second argument
' passed into function
If IsMissing(varDateAt) Then varDateAt = VBA.Date
If Not IsNull(varDob) Then
' get difference in years
intYears = DateDiff("yyyy", varDob, varDateAt)
' adjust result if date of birth falls later in year
' than date at which age to be calculated
If varDateAt < DateSerial(Year(varDateAt), Month(varDob), Day(varDob))
Then
intYears = intYears - 1
End If
GetAge = intYears
End If
End Function
The above query does assume a rather simply structured table, particularly
with regard to the Condition column. In a well-designed database this is
more likely to be in a related Conditions table, with a primary key
ConditionID say, which is referenced by a foreign key ConditionID column in
the Patients table, so these would need to be joined in each part of the
query as follows:
SELECT "Type 1 diabetes" AS [Category],
COUNT(*) AS [Number of Patients]
FROM [Patients] INNER JOIN [Conditions]
ON [Patients].[ConditionID] = [Conditions].[ConditionID]
WHERE [Condition] = "Type 1 diabetes"
UNION ALL
<and so on>
Each part of the query could of course be restricted to a particular subset
of patients if necessary, e.g. to return patients admitted in a particular
month on the basis of an AdmissionDate column:
SELECT "Type 1 diabetes" As Category,
COUNT(*) AS [Number of Patients]
FROM [Patients] INNER JOIN [Conditions]
ON [Patients].[ConditionID] = [Conditions].[ConditionID]
WHERE [Condition] = "Type 1 diabetes"
AND YEAR([AdmissionDate]) = [Enter year admitted:]
AND MONTH([AdmissionDate]) = [Enter month admitted ( 1-12):]
UNION ALL
<and so on>
which would prompt for the year and month at runtime.
Ken Sheridan
Stafford, England
I have a database for our hospital diabetes center. We created a great set of
forms etc... to stream line our paperwork, now we need to create some
reports. I am trying to create queries to show:
Number of patients who have Type 1 diabetes, Number of patients who have
Type 2 diabetes, number of patients who have gestational diabetes 1-18 years
old and >19 years old. I can create a query for each one of these number of
patients and get a long list of each. As soon as I insert a count or other
criteria into the query Access comes back and tells me the expression is to
complicated.
We also ask patients to rate their amount of exercise on a scale of 1 - 5.
Again I am sort of asking for the same thing as above. How many patients
stated they were at a one, at a two etc..... I create a query and get along
list of all of the ones in a row, then the twos in a row etc... When I
introduce a count into the equation I ge the complicated expression again.
Any help would be greatly appreciated!
--
.