transforming rows and columns

D

Deb H

I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the Adults,
Seniors, and Children to display as rows. I tried both a crosstab query and a
pivot table but with no success. Each of the values are calculated
expressions based on date fields in my table. Any help is greatly
appreciated. I am using Access 2003.
 
D

Dale Fye

What does the originale data look like?

If this were in a table, I would tell you to create a normalizing query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab, but since
this is just the results of a query, I'd go back to the original data,
change the Field that has the RowHeading to the field that contains you
AgeGroups, set the column with the Qtr as the Column Headings, and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to the
original Totals query.

HTH
Dale
 
D

Deb H

This Totals query is actually the result of another query in which I
calculated the ages of each of the individuals using their date of birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1 Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4 Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7 Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3 Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6 Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9 Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0)) AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2 Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5 Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8 Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11 Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14 Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1 Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4 Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0)) AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I need to
display multiple rows and each of them will show the results of calulated
expressions and not values from a single field. I hope I'm making sense and
thanks for your help.

Dale Fye said:
What does the originale data look like?

If this were in a table, I would tell you to create a normalizing query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab, but since
this is just the results of a query, I'd go back to the original data,
change the Field that has the RowHeading to the field that contains you
AgeGroups, set the column with the Qtr as the Column Headings, and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to the
original Totals query.

HTH
Dale

Deb H said:
I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the Adults,
Seniors, and Children to display as rows. I tried both a crosstab query
and a
pivot table but with no success. Each of the values are calculated
expressions based on date fields in my table. Any help is greatly
appreciated. I am using Access 2003.
 
D

Dale Fye

Deb,

Maybe you need to go back one step further. Your [qry Qtr Stats] indicates
a whole bunch of fields ([Child 1 Age] .... [Adult 7 Age]) that obviously
make your quarter stats query extremely difficult to use in any other query.
I could understand creating a single query like this, to be used in a report,
but it is highly unlikely that I would then take this denormalized query (as
you have) and use it generate another query.

What does the SQL for [qry Qtr Stats] look like?

When I do a grouping similar to what you have done, I create a new table (in
this case I'd call it tbl_Age_Groups) with fields similar to (StartAge,
EndAge, Group1, Group2) and values like:
StartAge EndAge Group1 Group2
0 3 Child Child 1
3 6 Child Child 2
6 9 Child Child 3

This can make coming up with you age groupings much simplier.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
This Totals query is actually the result of another query in which I
calculated the ages of each of the individuals using their date of birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1 Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4 Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7 Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3 Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6 Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9 Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0)) AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2 Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5 Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8 Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11 Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14 Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1 Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4 Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0)) AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I need to
display multiple rows and each of them will show the results of calulated
expressions and not values from a single field. I hope I'm making sense and
thanks for your help.

Dale Fye said:
What does the originale data look like?

If this were in a table, I would tell you to create a normalizing query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab, but since
this is just the results of a query, I'd go back to the original data,
change the Field that has the RowHeading to the field that contains you
AgeGroups, set the column with the Qtr as the Column Headings, and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to the
original Totals query.

HTH
Dale

Deb H said:
I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the Adults,
Seniors, and Children to display as rows. I tried both a crosstab query
and a
pivot table but with no success. Each of the values are calculated
expressions based on date fields in my table. Any help is greatly
appreciated. I am using Access 2003.
 
D

Deb H

Here is the SQL for the qry Qtr Stats:

SELECT [tblClient Visits].VisitNumber, [tblClient Visits].[Date of Visit],
(IIf([tblClient]![Client
Nb],1,0))+(IIf([Other1],1,0))+(IIf([Other2],1,0))+(IIf([Other3],1,0))+(IIf([Other4],1,0))+(IIf([Other5],1,0))+(IIf([Other6],1,0))+(IIf([Other7],1,0))+(IIf([ChildFirst1],1,0))+(IIf([ChildFirst2],1,0))+(IIf([ChildFirst3],1,0))+(IIf([ChildFirst4],1,0))+(IIf([ChildFirst5],1,0))+(IIf([ChildFirst6],1,0))+(IIf([ChildFirst7],1,0))+(IIf([ChildFirst8],1,0))+(IIf([ChildFirst9],1,0))+(IIf([ChildFirst10],1,0))+(IIf([ChildFirst11],1,0))+(IIf([ChildFirst12],1,0))+(IIf([ChildFirst13],1,0))+(IIf([ChildFirst14],1,0))
AS [# in HH], DateDiff("yyyy",[Client DOB],[Date of
Visit])-IIf(Format([Client DOB],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Client
Age],
DateDiff("yyyy",[OtherDOB1],Date())-IIf(Format([OtherDOB1],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 1 Age],
DateDiff("yyyy",[OtherDOB2],Date())-IIf(Format([OtherDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 2 Age],
DateDiff("yyyy",[OtherDOB3],Date())-IIf(Format([OtherDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 3 Age],
DateDiff("yyyy",[OtherDOB4],Date())-IIf(Format([OtherDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 4 Age],
DateDiff("yyyy",[OtherDOB5],Date())-IIf(Format([OtherDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 5 Age],
DateDiff("yyyy",[OtherDOB6],Date())-IIf(Format([OtherDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 6 Age],
DateDiff("yyyy",[OtherDOB7],Date())-IIf(Format([OtherDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 7 Age], DateDiff("yyyy",[ChildDOB1],[Date of
Visit])-IIf(Format([ChildDOB1],"mmdd")>Format([Date of Visit],"mmdd"),1,0) AS
[Child 1 Age],
DateDiff("yyyy",[ChildDOB2],Date())-IIf(Format([ChildDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 2 Age],
DateDiff("yyyy",[ChildDOB3],Date())-IIf(Format([ChildDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 3 Age],
DateDiff("yyyy",[ChildDOB4],Date())-IIf(Format([ChildDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 4 Age],
DateDiff("yyyy",[ChildDOB5],Date())-IIf(Format([ChildDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 5 Age],
DateDiff("yyyy",[ChildDOB6],Date())-IIf(Format([ChildDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 6 Age],
DateDiff("yyyy",[ChildDOB7],Date())-IIf(Format([ChildDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 7 Age],
DateDiff("yyyy",[ChildDOB8],Date())-IIf(Format([ChildDOB8],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 8 Age],
DateDiff("yyyy",[ChildDOB9],Date())-IIf(Format([ChildDOB9],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 9 Age],
DateDiff("yyyy",[ChildDOB10],Date())-IIf(Format([ChildDOB10],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 10 Age],
DateDiff("yyyy",[ChildDOB11],Date())-IIf(Format([ChildDOB11],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 11 Age],
DateDiff("yyyy",[ChildDOB12],Date())-IIf(Format([ChildDOB12],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 12 Age],
DateDiff("yyyy",[ChildDOB13],Date())-IIf(Format([ChildDOB13],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 13 Age],
DateDiff("yyyy",[ChildDOB14],Date())-IIf(Format([ChildDOB14],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Child 14 Age]
FROM tblClient INNER JOIN [tblClient Visits] ON tblClient.[Client Nb] =
[tblClient Visits].[Client Nb];

I use this query to create a quarterly comparison report. The ages are based
on a date of visit for each adult/child field in the table. I don't
understand - even if I stored the age groups in a new table, wouldn't I still
be creating a calculated expression for each adult/child field? I also need
to be make sure that any child or adult is grouped correctly as a
child/adult/senior as their date of visit changes. I'm confused as to how
creating a new table of age groups is simpler, but willing to learn : - )

Dale Fye said:
Deb,

Maybe you need to go back one step further. Your [qry Qtr Stats] indicates
a whole bunch of fields ([Child 1 Age] .... [Adult 7 Age]) that obviously
make your quarter stats query extremely difficult to use in any other query.
I could understand creating a single query like this, to be used in a report,
but it is highly unlikely that I would then take this denormalized query (as
you have) and use it generate another query.

What does the SQL for [qry Qtr Stats] look like?

When I do a grouping similar to what you have done, I create a new table (in
this case I'd call it tbl_Age_Groups) with fields similar to (StartAge,
EndAge, Group1, Group2) and values like:
StartAge EndAge Group1 Group2
0 3 Child Child 1
3 6 Child Child 2
6 9 Child Child 3

This can make coming up with you age groupings much simplier.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
This Totals query is actually the result of another query in which I
calculated the ages of each of the individuals using their date of birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1 Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4 Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7 Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3 Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6 Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9 Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0)) AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2 Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5 Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8 Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11 Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14 Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1 Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4 Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0)) AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I need to
display multiple rows and each of them will show the results of calulated
expressions and not values from a single field. I hope I'm making sense and
thanks for your help.

Dale Fye said:
What does the originale data look like?

If this were in a table, I would tell you to create a normalizing query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab, but since
this is just the results of a query, I'd go back to the original data,
change the Field that has the RowHeading to the field that contains you
AgeGroups, set the column with the Qtr as the Column Headings, and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to the
original Totals query.

HTH
Dale

I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the Adults,
Seniors, and Children to display as rows. I tried both a crosstab query
and a
pivot table but with no success. Each of the values are calculated
expressions based on date fields in my table. Any help is greatly
appreciated. I am using Access 2003.
 
D

Dale Fye

Deb,

You have a serious flaw in your data structure, which is why these queries
are so complex. You are obviously using your database like a spreadsheet
instead of taking advantage of the features that a relational database
provides you. Because of this, you have to create enough columns in your
table to handle the worst case number and type of clients (in this case 8
Other and 14 Child columns), when you should have a second table that
accounts for all of these "Other" and "Child" fields.

My guess is it should be something like tblClientDependents with fields
(ClientDepID, ClientID, Last Name, First Name, DOB, SSN, Gender, ...). With
this type of structure, you can add as many dependents as are appropriate,
and you only have to compute Age across 1 field, not 20). Also, this kind of
structure provides the additional freedom that in the off chance you get a
client with 21 dependents, you don't have to expand your table or modify any
of your queries.

The Age groups table will not help you until you get your data normalized.
Can you post the structure of your tblClients (field names and data types)?
Under Database tools, select Database Documenter, select tblClients, then
click Options and select Names, Data Types, and Sizes for the fields, then
click OK. When you get the report, export it to RTF or text file, then cut
and paste the data. Once I receive that, I'll try to help you with
restructuring your table, or with generating a query that will help you with
your current situation.


--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
Here is the SQL for the qry Qtr Stats:

SELECT [tblClient Visits].VisitNumber, [tblClient Visits].[Date of Visit],
(IIf([tblClient]![Client
Nb],1,0))+(IIf([Other1],1,0))+(IIf([Other2],1,0))+(IIf([Other3],1,0))+(IIf([Other4],1,0))+(IIf([Other5],1,0))+(IIf([Other6],1,0))+(IIf([Other7],1,0))+(IIf([ChildFirst1],1,0))+(IIf([ChildFirst2],1,0))+(IIf([ChildFirst3],1,0))+(IIf([ChildFirst4],1,0))+(IIf([ChildFirst5],1,0))+(IIf([ChildFirst6],1,0))+(IIf([ChildFirst7],1,0))+(IIf([ChildFirst8],1,0))+(IIf([ChildFirst9],1,0))+(IIf([ChildFirst10],1,0))+(IIf([ChildFirst11],1,0))+(IIf([ChildFirst12],1,0))+(IIf([ChildFirst13],1,0))+(IIf([ChildFirst14],1,0))
AS [# in HH], DateDiff("yyyy",[Client DOB],[Date of
Visit])-IIf(Format([Client DOB],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Client
Age],
DateDiff("yyyy",[OtherDOB1],Date())-IIf(Format([OtherDOB1],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 1 Age],
DateDiff("yyyy",[OtherDOB2],Date())-IIf(Format([OtherDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 2 Age],
DateDiff("yyyy",[OtherDOB3],Date())-IIf(Format([OtherDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 3 Age],
DateDiff("yyyy",[OtherDOB4],Date())-IIf(Format([OtherDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 4 Age],
DateDiff("yyyy",[OtherDOB5],Date())-IIf(Format([OtherDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 5 Age],
DateDiff("yyyy",[OtherDOB6],Date())-IIf(Format([OtherDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 6 Age],
DateDiff("yyyy",[OtherDOB7],Date())-IIf(Format([OtherDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 7 Age], DateDiff("yyyy",[ChildDOB1],[Date of
Visit])-IIf(Format([ChildDOB1],"mmdd")>Format([Date of Visit],"mmdd"),1,0) AS
[Child 1 Age],
DateDiff("yyyy",[ChildDOB2],Date())-IIf(Format([ChildDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 2 Age],
DateDiff("yyyy",[ChildDOB3],Date())-IIf(Format([ChildDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 3 Age],
DateDiff("yyyy",[ChildDOB4],Date())-IIf(Format([ChildDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 4 Age],
DateDiff("yyyy",[ChildDOB5],Date())-IIf(Format([ChildDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 5 Age],
DateDiff("yyyy",[ChildDOB6],Date())-IIf(Format([ChildDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 6 Age],
DateDiff("yyyy",[ChildDOB7],Date())-IIf(Format([ChildDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 7 Age],
DateDiff("yyyy",[ChildDOB8],Date())-IIf(Format([ChildDOB8],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 8 Age],
DateDiff("yyyy",[ChildDOB9],Date())-IIf(Format([ChildDOB9],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 9 Age],
DateDiff("yyyy",[ChildDOB10],Date())-IIf(Format([ChildDOB10],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 10 Age],
DateDiff("yyyy",[ChildDOB11],Date())-IIf(Format([ChildDOB11],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 11 Age],
DateDiff("yyyy",[ChildDOB12],Date())-IIf(Format([ChildDOB12],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 12 Age],
DateDiff("yyyy",[ChildDOB13],Date())-IIf(Format([ChildDOB13],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 13 Age],
DateDiff("yyyy",[ChildDOB14],Date())-IIf(Format([ChildDOB14],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Child 14 Age]
FROM tblClient INNER JOIN [tblClient Visits] ON tblClient.[Client Nb] =
[tblClient Visits].[Client Nb];

I use this query to create a quarterly comparison report. The ages are based
on a date of visit for each adult/child field in the table. I don't
understand - even if I stored the age groups in a new table, wouldn't I still
be creating a calculated expression for each adult/child field? I also need
to be make sure that any child or adult is grouped correctly as a
child/adult/senior as their date of visit changes. I'm confused as to how
creating a new table of age groups is simpler, but willing to learn : - )

Dale Fye said:
Deb,

Maybe you need to go back one step further. Your [qry Qtr Stats] indicates
a whole bunch of fields ([Child 1 Age] .... [Adult 7 Age]) that obviously
make your quarter stats query extremely difficult to use in any other query.
I could understand creating a single query like this, to be used in a report,
but it is highly unlikely that I would then take this denormalized query (as
you have) and use it generate another query.

What does the SQL for [qry Qtr Stats] look like?

When I do a grouping similar to what you have done, I create a new table (in
this case I'd call it tbl_Age_Groups) with fields similar to (StartAge,
EndAge, Group1, Group2) and values like:
StartAge EndAge Group1 Group2
0 3 Child Child 1
3 6 Child Child 2
6 9 Child Child 3

This can make coming up with you age groupings much simplier.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
This Totals query is actually the result of another query in which I
calculated the ages of each of the individuals using their date of birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1 Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4 Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7 Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3 Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6 Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9 Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0)) AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2 Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5 Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8 Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11 Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14 Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1 Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4 Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0)) AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I need to
display multiple rows and each of them will show the results of calulated
expressions and not values from a single field. I hope I'm making sense and
thanks for your help.

:

What does the originale data look like?

If this were in a table, I would tell you to create a normalizing query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab, but since
this is just the results of a query, I'd go back to the original data,
change the Field that has the RowHeading to the field that contains you
AgeGroups, set the column with the Qtr as the Column Headings, and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to the
original Totals query.

HTH
Dale

I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the Adults,
Seniors, and Children to display as rows. I tried both a crosstab query
and a
pivot table but with no success. Each of the values are calculated
expressions based on date fields in my table. Any help is greatly
appreciated. I am using Access 2003.
 
D

Deb H

Here is the report for the table. I am sending the report for the Client
visits which is the related table that contains the fields we discussed. This
is the many side of a relationship to a Client table that contains fields for
the main client (head of household). Thanks for your patience and help.

Columns
Name Type Size
VisitNumber Long Integer 4
Client Nb Long Integer 4
Date of Visit Date/Time 8
Notes Text 255
ClothingOnly Yes/No 1
ExtraFood Yes/No 1
PersonalProd Yes/No 1
Backpack Long Integer 4
Clothes Long Integer 4
Diapers Long Integer 4
GasVoucher Long Integer 4
Layette Long Integer 4
Assistance Long Integer 4
BirthdayBag Long Integer 4
Other Long Integer 4
HHitems Long Integer 4
GiftCertif Currency 8
Other1 Text 50
OtherDOB1 Date/Time 8
OtherGender1 Text 50
Relationship1 Text 50
Other2 Text 50
OtherDOB2 Date/Time 8
OtherGender2 Text 50
Relationship2 Text 50
Other3 Text 50
OtherDOB3 Date/Time 8
OtherGender3 Text 50
Relationship3 Text 50
Other4 Text 50
OtherDOB4 Date/Time 8
OtherGender4 Text 50
Relationship4 Text 50
Other5 Text 50
OtherDOB5 Date/Time 8
OtherGender5 Text 50
Relationship5 Text 50
Other6 Text 50
OtherDOB6 Date/Time 8
OtherGender6 Text 50
Relationship6 Text 50
Other7 Text 50
OtherDOB7 Date/Time 8
OtherGender7 Text 50
Relationship7 Text 50
ChildFirst1 Text 50
ChildDOB1 Date/Time 8
ChildGender1 Text 50
RelationshipCh1 Text 50

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 2
ChildFirst2 Text 50
ChildDOB2 Date/Time 8
ChildGender2 Text 50
RelationshipCh2 Text 50
ChildFirst3 Text 50
ChildDOB3 Date/Time 8
ChildGender3 Text 50
RelationshipCh3 Text 50
ChildFirst4 Text 50
ChildDOB4 Date/Time 8
ChildGender4 Text 50
RelationshipCh4 Text 50
ChildFirst5 Text 50
ChildDOB5 Date/Time 8
ChildGender5 Text 50
RelationshipCh5 Text 50
ChildFirst6 Text 50
ChildDOB6 Date/Time 8
ChildGender6 Text 50
RelationshipCh6 Text 50
ChildFirst7 Text 50
ChildDOB7 Date/Time 8
ChildGender7 Text 50
RelationshipCh7 Text 50
ChildFirst8 Text 50
ChildDOB8 Date/Time 8
ChildGender8 Text 50
RelationshipCh8 Text 50
ChildFirst9 Text 50
ChildDOB9 Date/Time 8
ChildGender9 Text 50
RelationshipCh9 Text 50
ChildFirst10 Text 50
ChildDOB10 Date/Time 8
ChildGender10 Text 50
RelationshipCh10 Text 50
ChildFirst11 Text 50
ChildDOB11 Date/Time 8
RelationshipCh11 Text 50
ChildGender11 Text 50
ChildFirst12 Text 50
ChildDOB12 Date/Time 8
ChildGender12 Text 50
RelationshipCh12 Text 50
ChildFirst13 Text 50
ChildDOB13 Date/Time 8
ChildGender13 Text 50
RelationshipCh13 Text 50
ChildFirst14 Text 50
ChildDOB14 Date/Time 8
ChildGender14 Text 50
RelationshipCh14 Text 50
FTemployment Yes/No 1
PTemployment Yes/No 1

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 3
MFIP Yes/No 1
SocSec Yes/No 1
SSD Yes/No 1
GA Yes/No 1
UsingFS Yes/No 1
UsingWIC Yes/No 1
MA Yes/No 1
Relationships
tblClienttblClient Visits
tblClient tblClient Visits
Client Nb 1 ï‚¥ Client Nb
Attributes: Enforced
RelationshipType: One-To-Many


Dale Fye said:
Deb,

You have a serious flaw in your data structure, which is why these queries
are so complex. You are obviously using your database like a spreadsheet
instead of taking advantage of the features that a relational database
provides you. Because of this, you have to create enough columns in your
table to handle the worst case number and type of clients (in this case 8
Other and 14 Child columns), when you should have a second table that
accounts for all of these "Other" and "Child" fields.

My guess is it should be something like tblClientDependents with fields
(ClientDepID, ClientID, Last Name, First Name, DOB, SSN, Gender, ...). With
this type of structure, you can add as many dependents as are appropriate,
and you only have to compute Age across 1 field, not 20). Also, this kind of
structure provides the additional freedom that in the off chance you get a
client with 21 dependents, you don't have to expand your table or modify any
of your queries.

The Age groups table will not help you until you get your data normalized.
Can you post the structure of your tblClients (field names and data types)?
Under Database tools, select Database Documenter, select tblClients, then
click Options and select Names, Data Types, and Sizes for the fields, then
click OK. When you get the report, export it to RTF or text file, then cut
and paste the data. Once I receive that, I'll try to help you with
restructuring your table, or with generating a query that will help you with
your current situation.


--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
Here is the SQL for the qry Qtr Stats:

SELECT [tblClient Visits].VisitNumber, [tblClient Visits].[Date of Visit],
(IIf([tblClient]![Client
Nb],1,0))+(IIf([Other1],1,0))+(IIf([Other2],1,0))+(IIf([Other3],1,0))+(IIf([Other4],1,0))+(IIf([Other5],1,0))+(IIf([Other6],1,0))+(IIf([Other7],1,0))+(IIf([ChildFirst1],1,0))+(IIf([ChildFirst2],1,0))+(IIf([ChildFirst3],1,0))+(IIf([ChildFirst4],1,0))+(IIf([ChildFirst5],1,0))+(IIf([ChildFirst6],1,0))+(IIf([ChildFirst7],1,0))+(IIf([ChildFirst8],1,0))+(IIf([ChildFirst9],1,0))+(IIf([ChildFirst10],1,0))+(IIf([ChildFirst11],1,0))+(IIf([ChildFirst12],1,0))+(IIf([ChildFirst13],1,0))+(IIf([ChildFirst14],1,0))
AS [# in HH], DateDiff("yyyy",[Client DOB],[Date of
Visit])-IIf(Format([Client DOB],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Client
Age],
DateDiff("yyyy",[OtherDOB1],Date())-IIf(Format([OtherDOB1],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 1 Age],
DateDiff("yyyy",[OtherDOB2],Date())-IIf(Format([OtherDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 2 Age],
DateDiff("yyyy",[OtherDOB3],Date())-IIf(Format([OtherDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 3 Age],
DateDiff("yyyy",[OtherDOB4],Date())-IIf(Format([OtherDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 4 Age],
DateDiff("yyyy",[OtherDOB5],Date())-IIf(Format([OtherDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 5 Age],
DateDiff("yyyy",[OtherDOB6],Date())-IIf(Format([OtherDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 6 Age],
DateDiff("yyyy",[OtherDOB7],Date())-IIf(Format([OtherDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 7 Age], DateDiff("yyyy",[ChildDOB1],[Date of
Visit])-IIf(Format([ChildDOB1],"mmdd")>Format([Date of Visit],"mmdd"),1,0) AS
[Child 1 Age],
DateDiff("yyyy",[ChildDOB2],Date())-IIf(Format([ChildDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 2 Age],
DateDiff("yyyy",[ChildDOB3],Date())-IIf(Format([ChildDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 3 Age],
DateDiff("yyyy",[ChildDOB4],Date())-IIf(Format([ChildDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 4 Age],
DateDiff("yyyy",[ChildDOB5],Date())-IIf(Format([ChildDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 5 Age],
DateDiff("yyyy",[ChildDOB6],Date())-IIf(Format([ChildDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 6 Age],
DateDiff("yyyy",[ChildDOB7],Date())-IIf(Format([ChildDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 7 Age],
DateDiff("yyyy",[ChildDOB8],Date())-IIf(Format([ChildDOB8],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 8 Age],
DateDiff("yyyy",[ChildDOB9],Date())-IIf(Format([ChildDOB9],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 9 Age],
DateDiff("yyyy",[ChildDOB10],Date())-IIf(Format([ChildDOB10],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 10 Age],
DateDiff("yyyy",[ChildDOB11],Date())-IIf(Format([ChildDOB11],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 11 Age],
DateDiff("yyyy",[ChildDOB12],Date())-IIf(Format([ChildDOB12],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 12 Age],
DateDiff("yyyy",[ChildDOB13],Date())-IIf(Format([ChildDOB13],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 13 Age],
DateDiff("yyyy",[ChildDOB14],Date())-IIf(Format([ChildDOB14],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Child 14 Age]
FROM tblClient INNER JOIN [tblClient Visits] ON tblClient.[Client Nb] =
[tblClient Visits].[Client Nb];

I use this query to create a quarterly comparison report. The ages are based
on a date of visit for each adult/child field in the table. I don't
understand - even if I stored the age groups in a new table, wouldn't I still
be creating a calculated expression for each adult/child field? I also need
to be make sure that any child or adult is grouped correctly as a
child/adult/senior as their date of visit changes. I'm confused as to how
creating a new table of age groups is simpler, but willing to learn : - )

Dale Fye said:
Deb,

Maybe you need to go back one step further. Your [qry Qtr Stats] indicates
a whole bunch of fields ([Child 1 Age] .... [Adult 7 Age]) that obviously
make your quarter stats query extremely difficult to use in any other query.
I could understand creating a single query like this, to be used in a report,
but it is highly unlikely that I would then take this denormalized query (as
you have) and use it generate another query.

What does the SQL for [qry Qtr Stats] look like?

When I do a grouping similar to what you have done, I create a new table (in
this case I'd call it tbl_Age_Groups) with fields similar to (StartAge,
EndAge, Group1, Group2) and values like:
StartAge EndAge Group1 Group2
0 3 Child Child 1
3 6 Child Child 2
6 9 Child Child 3

This can make coming up with you age groupings much simplier.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



:

This Totals query is actually the result of another query in which I
calculated the ages of each of the individuals using their date of birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1 Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4 Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7 Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3 Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6 Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9 Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0)) AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2 Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5 Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8 Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11 Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14 Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1 Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4 Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0)) AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I need to
display multiple rows and each of them will show the results of calulated
expressions and not values from a single field. I hope I'm making sense and
thanks for your help.

:

What does the originale data look like?

If this were in a table, I would tell you to create a normalizing query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab, but since
this is just the results of a query, I'd go back to the original data,
change the Field that has the RowHeading to the field that contains you
AgeGroups, set the column with the Qtr as the Column Headings, and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to the
original Totals query.

HTH
Dale

I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the Adults,
Seniors, and Children to display as rows. I tried both a crosstab query
and a
pivot table but with no success. Each of the values are calculated
expressions based on date fields in my table. Any help is greatly
appreciated. I am using Access 2003.
 
D

Dale Fye

DEb,

I'll take a look at this tonight and get back to you.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
Here is the report for the table. I am sending the report for the Client
visits which is the related table that contains the fields we discussed. This
is the many side of a relationship to a Client table that contains fields for
the main client (head of household). Thanks for your patience and help.

Columns
Name Type Size
VisitNumber Long Integer 4
Client Nb Long Integer 4
Date of Visit Date/Time 8
Notes Text 255
ClothingOnly Yes/No 1
ExtraFood Yes/No 1
PersonalProd Yes/No 1
Backpack Long Integer 4
Clothes Long Integer 4
Diapers Long Integer 4
GasVoucher Long Integer 4
Layette Long Integer 4
Assistance Long Integer 4
BirthdayBag Long Integer 4
Other Long Integer 4
HHitems Long Integer 4
GiftCertif Currency 8
Other1 Text 50
OtherDOB1 Date/Time 8
OtherGender1 Text 50
Relationship1 Text 50
Other2 Text 50
OtherDOB2 Date/Time 8
OtherGender2 Text 50
Relationship2 Text 50
Other3 Text 50
OtherDOB3 Date/Time 8
OtherGender3 Text 50
Relationship3 Text 50
Other4 Text 50
OtherDOB4 Date/Time 8
OtherGender4 Text 50
Relationship4 Text 50
Other5 Text 50
OtherDOB5 Date/Time 8
OtherGender5 Text 50
Relationship5 Text 50
Other6 Text 50
OtherDOB6 Date/Time 8
OtherGender6 Text 50
Relationship6 Text 50
Other7 Text 50
OtherDOB7 Date/Time 8
OtherGender7 Text 50
Relationship7 Text 50
ChildFirst1 Text 50
ChildDOB1 Date/Time 8
ChildGender1 Text 50
RelationshipCh1 Text 50

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 2
ChildFirst2 Text 50
ChildDOB2 Date/Time 8
ChildGender2 Text 50
RelationshipCh2 Text 50
ChildFirst3 Text 50
ChildDOB3 Date/Time 8
ChildGender3 Text 50
RelationshipCh3 Text 50
ChildFirst4 Text 50
ChildDOB4 Date/Time 8
ChildGender4 Text 50
RelationshipCh4 Text 50
ChildFirst5 Text 50
ChildDOB5 Date/Time 8
ChildGender5 Text 50
RelationshipCh5 Text 50
ChildFirst6 Text 50
ChildDOB6 Date/Time 8
ChildGender6 Text 50
RelationshipCh6 Text 50
ChildFirst7 Text 50
ChildDOB7 Date/Time 8
ChildGender7 Text 50
RelationshipCh7 Text 50
ChildFirst8 Text 50
ChildDOB8 Date/Time 8
ChildGender8 Text 50
RelationshipCh8 Text 50
ChildFirst9 Text 50
ChildDOB9 Date/Time 8
ChildGender9 Text 50
RelationshipCh9 Text 50
ChildFirst10 Text 50
ChildDOB10 Date/Time 8
ChildGender10 Text 50
RelationshipCh10 Text 50
ChildFirst11 Text 50
ChildDOB11 Date/Time 8
RelationshipCh11 Text 50
ChildGender11 Text 50
ChildFirst12 Text 50
ChildDOB12 Date/Time 8
ChildGender12 Text 50
RelationshipCh12 Text 50
ChildFirst13 Text 50
ChildDOB13 Date/Time 8
ChildGender13 Text 50
RelationshipCh13 Text 50
ChildFirst14 Text 50
ChildDOB14 Date/Time 8
ChildGender14 Text 50
RelationshipCh14 Text 50
FTemployment Yes/No 1
PTemployment Yes/No 1

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 3
MFIP Yes/No 1
SocSec Yes/No 1
SSD Yes/No 1
GA Yes/No 1
UsingFS Yes/No 1
UsingWIC Yes/No 1
MA Yes/No 1
Relationships
tblClienttblClient Visits
tblClient tblClient Visits
Client Nb 1 ï‚¥ Client Nb
Attributes: Enforced
RelationshipType: One-To-Many


Dale Fye said:
Deb,

You have a serious flaw in your data structure, which is why these queries
are so complex. You are obviously using your database like a spreadsheet
instead of taking advantage of the features that a relational database
provides you. Because of this, you have to create enough columns in your
table to handle the worst case number and type of clients (in this case 8
Other and 14 Child columns), when you should have a second table that
accounts for all of these "Other" and "Child" fields.

My guess is it should be something like tblClientDependents with fields
(ClientDepID, ClientID, Last Name, First Name, DOB, SSN, Gender, ...). With
this type of structure, you can add as many dependents as are appropriate,
and you only have to compute Age across 1 field, not 20). Also, this kind of
structure provides the additional freedom that in the off chance you get a
client with 21 dependents, you don't have to expand your table or modify any
of your queries.

The Age groups table will not help you until you get your data normalized.
Can you post the structure of your tblClients (field names and data types)?
Under Database tools, select Database Documenter, select tblClients, then
click Options and select Names, Data Types, and Sizes for the fields, then
click OK. When you get the report, export it to RTF or text file, then cut
and paste the data. Once I receive that, I'll try to help you with
restructuring your table, or with generating a query that will help you with
your current situation.


--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
Here is the SQL for the qry Qtr Stats:

SELECT [tblClient Visits].VisitNumber, [tblClient Visits].[Date of Visit],
(IIf([tblClient]![Client
Nb],1,0))+(IIf([Other1],1,0))+(IIf([Other2],1,0))+(IIf([Other3],1,0))+(IIf([Other4],1,0))+(IIf([Other5],1,0))+(IIf([Other6],1,0))+(IIf([Other7],1,0))+(IIf([ChildFirst1],1,0))+(IIf([ChildFirst2],1,0))+(IIf([ChildFirst3],1,0))+(IIf([ChildFirst4],1,0))+(IIf([ChildFirst5],1,0))+(IIf([ChildFirst6],1,0))+(IIf([ChildFirst7],1,0))+(IIf([ChildFirst8],1,0))+(IIf([ChildFirst9],1,0))+(IIf([ChildFirst10],1,0))+(IIf([ChildFirst11],1,0))+(IIf([ChildFirst12],1,0))+(IIf([ChildFirst13],1,0))+(IIf([ChildFirst14],1,0))
AS [# in HH], DateDiff("yyyy",[Client DOB],[Date of
Visit])-IIf(Format([Client DOB],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Client
Age],
DateDiff("yyyy",[OtherDOB1],Date())-IIf(Format([OtherDOB1],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 1 Age],
DateDiff("yyyy",[OtherDOB2],Date())-IIf(Format([OtherDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 2 Age],
DateDiff("yyyy",[OtherDOB3],Date())-IIf(Format([OtherDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 3 Age],
DateDiff("yyyy",[OtherDOB4],Date())-IIf(Format([OtherDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 4 Age],
DateDiff("yyyy",[OtherDOB5],Date())-IIf(Format([OtherDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 5 Age],
DateDiff("yyyy",[OtherDOB6],Date())-IIf(Format([OtherDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 6 Age],
DateDiff("yyyy",[OtherDOB7],Date())-IIf(Format([OtherDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 7 Age], DateDiff("yyyy",[ChildDOB1],[Date of
Visit])-IIf(Format([ChildDOB1],"mmdd")>Format([Date of Visit],"mmdd"),1,0) AS
[Child 1 Age],
DateDiff("yyyy",[ChildDOB2],Date())-IIf(Format([ChildDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 2 Age],
DateDiff("yyyy",[ChildDOB3],Date())-IIf(Format([ChildDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 3 Age],
DateDiff("yyyy",[ChildDOB4],Date())-IIf(Format([ChildDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 4 Age],
DateDiff("yyyy",[ChildDOB5],Date())-IIf(Format([ChildDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 5 Age],
DateDiff("yyyy",[ChildDOB6],Date())-IIf(Format([ChildDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 6 Age],
DateDiff("yyyy",[ChildDOB7],Date())-IIf(Format([ChildDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 7 Age],
DateDiff("yyyy",[ChildDOB8],Date())-IIf(Format([ChildDOB8],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 8 Age],
DateDiff("yyyy",[ChildDOB9],Date())-IIf(Format([ChildDOB9],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 9 Age],
DateDiff("yyyy",[ChildDOB10],Date())-IIf(Format([ChildDOB10],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 10 Age],
DateDiff("yyyy",[ChildDOB11],Date())-IIf(Format([ChildDOB11],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 11 Age],
DateDiff("yyyy",[ChildDOB12],Date())-IIf(Format([ChildDOB12],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 12 Age],
DateDiff("yyyy",[ChildDOB13],Date())-IIf(Format([ChildDOB13],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 13 Age],
DateDiff("yyyy",[ChildDOB14],Date())-IIf(Format([ChildDOB14],"mmdd")>Format(Date(),"mmdd"),1,0) AS [Child 14 Age]
FROM tblClient INNER JOIN [tblClient Visits] ON tblClient.[Client Nb] =
[tblClient Visits].[Client Nb];

I use this query to create a quarterly comparison report. The ages are based
on a date of visit for each adult/child field in the table. I don't
understand - even if I stored the age groups in a new table, wouldn't I still
be creating a calculated expression for each adult/child field? I also need
to be make sure that any child or adult is grouped correctly as a
child/adult/senior as their date of visit changes. I'm confused as to how
creating a new table of age groups is simpler, but willing to learn : - )

:

Deb,

Maybe you need to go back one step further. Your [qry Qtr Stats] indicates
a whole bunch of fields ([Child 1 Age] .... [Adult 7 Age]) that obviously
make your quarter stats query extremely difficult to use in any other query.
I could understand creating a single query like this, to be used in a report,
but it is highly unlikely that I would then take this denormalized query (as
you have) and use it generate another query.

What does the SQL for [qry Qtr Stats] look like?

When I do a grouping similar to what you have done, I create a new table (in
this case I'd call it tbl_Age_Groups) with fields similar to (StartAge,
EndAge, Group1, Group2) and values like:
StartAge EndAge Group1 Group2
0 3 Child Child 1
3 6 Child Child 2
6 9 Child Child 3

This can make coming up with you age groupings much simplier.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



:

This Totals query is actually the result of another query in which I
calculated the ages of each of the individuals using their date of birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1 Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4 Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7 Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3 Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6 Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9 Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0)) AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2 Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5 Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8 Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11 Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14 Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1 Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4 Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0)) AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I need to
display multiple rows and each of them will show the results of calulated
expressions and not values from a single field. I hope I'm making sense and
thanks for your help.
 
D

Dale Fye

Deb,

How far along are you in development of this database application? The
reason I ask, is that although I would strongly recommend you redesign the
database structure along the lines I mentioned before, doing the redesign
could require a lot of re-engineering of the forms, queries, and reports you
already have created. BTW, most database developers will strongly advise
against putting spaces in the name of your tables or fields. If you feel
you need a space for readability, use and underscore ( _ ).

From the appearance of [tblClient_Visits], it appears that you will only
have data in one of the Other or Child blocks per visit, is that correct?

You didn't send me the indices, but I'll assume that VisitNumber is an
Autonumber primary key (if that is not correct, please let me know).

How do you know which fields (Other1 - Other7, ChildFirst1-ChildFirst14) to
use to record for a particular visit, or does a client bring in multiple
dependents during a given visit, and you just log them all in? For purposes
of the query below, I'm going to assume that the value that goes in these
fields is a name.

What are the values that ultimately go in the following fields ([Other1],
[OtherGender1], and [Relationship1])?
Once you have addressed these questions, I'll continue the re-engineering
thread.

In the meantime, let me address the query from your previous post. The
first thing I would do is create a normalizing query. This query will take
data from a non-normal data structure and put it into one that is easier to
query. To do this, we will create a union query which groups all of the
Other, Gender, DOB, and Relationship fields into 6 columns. The key to a
union query is that each individual SELECT statement in the query must
return the same number of fields, in the same sequence, and they must be of
the same data type as the fields in the same position in the first of the
SELECT statements.

You can start this out by using the query grid to get the first set of
values but it is really just as easy to start out in the SQL view. Create a
new query. Add [tblClient Visit] to the grid, close the dialog box, and
then change to the SQL view. The query will look like the following (I'm
not going to do all of the rows, but you will get the idea). Since you can
join back to [tblClient Visit] to get the visit date and other pertinent
data, I'll leave that out of the union query.

SELECT VisitNumber, "Other1" as FieldTitle, [Other1] as DepName,
[OtherDOB1] as DOB, OtherGender1 as Gender,
Relationship1 as Relation
FROM [tblClient Visit]
WHERE [Other1] is NOT NULL
UNION ALL
SELECT VisitNumber, "Other2" as FieldTitle, [Other2] as DepName,
[OtherDOB2] as DOB, OtherGender2 as Gender,
Relationship2 as Relation
FROM [tblClient Visit]
WHERE [Other2] is NOT NULL
UNION ALL
....
SELECT VisitNumber, "ChildFirst1" as FieldTitle, [ChildFirst1] as DepName,
[ChildDOB1] as DOB, ChildGender1 as Gender,
RelationshipCh1 as Relation
FROM [tblClient Visit]
WHERE [ChildFirst1] is NOT NULL
UNION ALL
SELECT VisitNumber, "ChildFirst1" as FieldTitle, [ChildFirst1] as DepName,
[ChildDOB1] as DOB, ChildGender1 as Gender,
RelationshipCh1 as Relation
FROM [tblClient Visit]
WHERE [ChildFirst1] is NOT NULL
UNION ALL
....
SELECT VisitNumber, "ChildFirst14" as FieldTitle, [ChildFirst14] as DepName,
[ChildDOB14] as DOB, ChildGender14 as Gender,
RelationshipCh14 as Relation
FROM [tblClient Visit]
WHERE [ChildFirst1] is NOT NULL

Hopefully, this will work the first time you run it, but you must pay
meticulous attention as you copy and paste from one group of fields to the
next that you don't forget to change the numeric value at the end of the
field. Once you fill in all of the ... to take account of all of the other
field groups, this query will give you a list of all of the dependents (or
others) that were seen during a given visit. This assumes that the [Other1]
or [ChildFirst1] field will be NULL if those segments were not used. Once
the query runs, save it query as [qryClientVisitNormal].

Then, to test whether you got all of the colums right, you might try
something like the following. This should give you a list of all of the
fields that you have used (Other1 - Other7, ChildFirst1-ChildFirst14) and
the count of the number of times they are used in the table. Don't worry
about the count, it is just there to give you an idea of how many times that
field (or group of fields) are used in the table.

SELECT [FieldTitle], Count([VisitNumber] FROM [qryClientVisitNormal]
GROUP BY [FieldTitle]

To expand this to include the actual client, you could add another UNION ALL
and SELECT statement as shown below, although I didn't see fields for the
client name, dob, gender in [tblClient Visit], so I'm thinking that you may
have these stashed away in [tblClient]. The addition to the above query
might look like:

UNION SELECT
SELECT VisitNumber, "Client" as FieldTitle, [ClientName] as DepName,
[Client DOB] as DOB, [Client Gender] as Gender,
"Client" as Relation
FROM tblClient INNER JOIN [tblClient Visits]
ON tblClient.[Client Nb] = [tblClient Visits].[Client Nb];

The next step is to join this query to [tblClient Visits] to get the Number
in household, for each visit. Since the same client may report different
dependents on different visits. Don't know if this is really an option, or
of importance. If you don't add the last step mentioned above, then you
will need to change the Count( ) to Count( ) + 1 as [# in HH] to account for
the actual client.

SELECT [tblClient Visits].[Client Nb], [tblClient Visits].VisitNumber,
Count(qryClientVisitNormal.FieldTitle) as [# in HH]
FROM [tblClient Visits] INNER JOIN [qryClientVisitNormal]
ON [tblClient Visits].VisitNumber = [qryClientVisitNormal].VisitNumber
GROUP BY [tblClient Visits].[Client Nb], [tblClient Visits].VisitNumber

Getting the age of each of the members of the HH is the next step. The first
thing I notices looking at your query is that you are not computing the ages
on the date of the visit, but rather, on the date that you run the report.
WAS THAT YOUR INTENT? I'll assume not, since that really doesn't make a lot
of sense. So, the query would now look like:

SELECT [tblClient Visits].[Client Nb], [tblClient Visits].VisitNumber,
[FieldTitle], [DepName]
DateDiff("yyyy", [DOB], [Date of Visit]) - IIF(Format([DOB],
"mmdd") > Format([Date of Visit], "mmdd"), 1, 0)
FROM [tblClient Visits] INNER JOIN [qryClientVisitNormal]
ON [tblClient Visits].[VisitNumber] = [qryClientVisitNormal].[VisitNumber]

This is a lot to absorb, so if you get to this point, and are ready to
continue, let me know. Then we can address your original Totals query and
the transformation of that.

HTH
Dale

Deb H said:
Here is the report for the table. I am sending the report for the Client
visits which is the related table that contains the fields we discussed.
This
is the many side of a relationship to a Client table that contains fields
for
the main client (head of household). Thanks for your patience and help.

Columns
Name Type Size
VisitNumber Long Integer 4
Client Nb Long Integer 4
Date of Visit Date/Time 8
Notes Text 255
ClothingOnly Yes/No 1
ExtraFood Yes/No 1
PersonalProd Yes/No 1
Backpack Long Integer 4
Clothes Long Integer 4
Diapers Long Integer 4
GasVoucher Long Integer 4
Layette Long Integer 4
Assistance Long Integer 4
BirthdayBag Long Integer 4
Other Long Integer 4
HHitems Long Integer 4
GiftCertif Currency 8
Other1 Text 50
OtherDOB1 Date/Time 8
OtherGender1 Text 50
Relationship1 Text 50
Other2 Text 50
OtherDOB2 Date/Time 8
OtherGender2 Text 50
Relationship2 Text 50
Other3 Text 50
OtherDOB3 Date/Time 8
OtherGender3 Text 50
Relationship3 Text 50
Other4 Text 50
OtherDOB4 Date/Time 8
OtherGender4 Text 50
Relationship4 Text 50
Other5 Text 50
OtherDOB5 Date/Time 8
OtherGender5 Text 50
Relationship5 Text 50
Other6 Text 50
OtherDOB6 Date/Time 8
OtherGender6 Text 50
Relationship6 Text 50
Other7 Text 50
OtherDOB7 Date/Time 8
OtherGender7 Text 50
Relationship7 Text 50
ChildFirst1 Text 50
ChildDOB1 Date/Time 8
ChildGender1 Text 50
RelationshipCh1 Text 50

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 2
ChildFirst2 Text 50
ChildDOB2 Date/Time 8
ChildGender2 Text 50
RelationshipCh2 Text 50
ChildFirst3 Text 50
ChildDOB3 Date/Time 8
ChildGender3 Text 50
RelationshipCh3 Text 50
ChildFirst4 Text 50
ChildDOB4 Date/Time 8
ChildGender4 Text 50
RelationshipCh4 Text 50
ChildFirst5 Text 50
ChildDOB5 Date/Time 8
ChildGender5 Text 50
RelationshipCh5 Text 50
ChildFirst6 Text 50
ChildDOB6 Date/Time 8
ChildGender6 Text 50
RelationshipCh6 Text 50
ChildFirst7 Text 50
ChildDOB7 Date/Time 8
ChildGender7 Text 50
RelationshipCh7 Text 50
ChildFirst8 Text 50
ChildDOB8 Date/Time 8
ChildGender8 Text 50
RelationshipCh8 Text 50
ChildFirst9 Text 50
ChildDOB9 Date/Time 8
ChildGender9 Text 50
RelationshipCh9 Text 50
ChildFirst10 Text 50
ChildDOB10 Date/Time 8
ChildGender10 Text 50
RelationshipCh10 Text 50
ChildFirst11 Text 50
ChildDOB11 Date/Time 8
RelationshipCh11 Text 50
ChildGender11 Text 50
ChildFirst12 Text 50
ChildDOB12 Date/Time 8
ChildGender12 Text 50
RelationshipCh12 Text 50
ChildFirst13 Text 50
ChildDOB13 Date/Time 8
ChildGender13 Text 50
RelationshipCh13 Text 50
ChildFirst14 Text 50
ChildDOB14 Date/Time 8
ChildGender14 Text 50
RelationshipCh14 Text 50
FTemployment Yes/No 1
PTemployment Yes/No 1

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 3
MFIP Yes/No 1
SocSec Yes/No 1
SSD Yes/No 1
GA Yes/No 1
UsingFS Yes/No 1
UsingWIC Yes/No 1
MA Yes/No 1
Relationships
tblClienttblClient Visits
tblClient tblClient Visits
Client Nb 1 ? Client Nb
Attributes: Enforced
RelationshipType: One-To-Many


Dale Fye said:
Deb,

You have a serious flaw in your data structure, which is why these
queries
are so complex. You are obviously using your database like a spreadsheet
instead of taking advantage of the features that a relational database
provides you. Because of this, you have to create enough columns in your
table to handle the worst case number and type of clients (in this case 8
Other and 14 Child columns), when you should have a second table that
accounts for all of these "Other" and "Child" fields.

My guess is it should be something like tblClientDependents with fields
(ClientDepID, ClientID, Last Name, First Name, DOB, SSN, Gender, ...).
With
this type of structure, you can add as many dependents as are
appropriate,
and you only have to compute Age across 1 field, not 20). Also, this
kind of
structure provides the additional freedom that in the off chance you get
a
client with 21 dependents, you don't have to expand your table or modify
any
of your queries.

The Age groups table will not help you until you get your data
normalized.
Can you post the structure of your tblClients (field names and data
types)?
Under Database tools, select Database Documenter, select tblClients, then
click Options and select Names, Data Types, and Sizes for the fields,
then
click OK. When you get the report, export it to RTF or text file, then
cut
and paste the data. Once I receive that, I'll try to help you with
restructuring your table, or with generating a query that will help you
with
your current situation.


--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



Deb H said:
Here is the SQL for the qry Qtr Stats:

SELECT [tblClient Visits].VisitNumber, [tblClient Visits].[Date of
Visit],
(IIf([tblClient]![Client
Nb],1,0))+(IIf([Other1],1,0))+(IIf([Other2],1,0))+(IIf([Other3],1,0))+(IIf([Other4],1,0))+(IIf([Other5],1,0))+(IIf([Other6],1,0))+(IIf([Other7],1,0))+(IIf([ChildFirst1],1,0))+(IIf([ChildFirst2],1,0))+(IIf([ChildFirst3],1,0))+(IIf([ChildFirst4],1,0))+(IIf([ChildFirst5],1,0))+(IIf([ChildFirst6],1,0))+(IIf([ChildFirst7],1,0))+(IIf([ChildFirst8],1,0))+(IIf([ChildFirst9],1,0))+(IIf([ChildFirst10],1,0))+(IIf([ChildFirst11],1,0))+(IIf([ChildFirst12],1,0))+(IIf([ChildFirst13],1,0))+(IIf([ChildFirst14],1,0))
AS [# in HH], DateDiff("yyyy",[Client DOB],[Date of
Visit])-IIf(Format([Client DOB],"mmdd")>Format(Date(),"mmdd"),1,0) AS
[Client
Age],
DateDiff("yyyy",[OtherDOB1],Date())-IIf(Format([OtherDOB1],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 1 Age],
DateDiff("yyyy",[OtherDOB2],Date())-IIf(Format([OtherDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 2 Age],
DateDiff("yyyy",[OtherDOB3],Date())-IIf(Format([OtherDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 3 Age],
DateDiff("yyyy",[OtherDOB4],Date())-IIf(Format([OtherDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 4 Age],
DateDiff("yyyy",[OtherDOB5],Date())-IIf(Format([OtherDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 5 Age],
DateDiff("yyyy",[OtherDOB6],Date())-IIf(Format([OtherDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 6 Age],
DateDiff("yyyy",[OtherDOB7],Date())-IIf(Format([OtherDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Adult 7 Age], DateDiff("yyyy",[ChildDOB1],[Date of
Visit])-IIf(Format([ChildDOB1],"mmdd")>Format([Date of
Visit],"mmdd"),1,0) AS
[Child 1 Age],
DateDiff("yyyy",[ChildDOB2],Date())-IIf(Format([ChildDOB2],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 2 Age],
DateDiff("yyyy",[ChildDOB3],Date())-IIf(Format([ChildDOB3],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 3 Age],
DateDiff("yyyy",[ChildDOB4],Date())-IIf(Format([ChildDOB4],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 4 Age],
DateDiff("yyyy",[ChildDOB5],Date())-IIf(Format([ChildDOB5],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 5 Age],
DateDiff("yyyy",[ChildDOB6],Date())-IIf(Format([ChildDOB6],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 6 Age],
DateDiff("yyyy",[ChildDOB7],Date())-IIf(Format([ChildDOB7],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 7 Age],
DateDiff("yyyy",[ChildDOB8],Date())-IIf(Format([ChildDOB8],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 8 Age],
DateDiff("yyyy",[ChildDOB9],Date())-IIf(Format([ChildDOB9],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 9 Age],
DateDiff("yyyy",[ChildDOB10],Date())-IIf(Format([ChildDOB10],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 10 Age],
DateDiff("yyyy",[ChildDOB11],Date())-IIf(Format([ChildDOB11],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 11 Age],
DateDiff("yyyy",[ChildDOB12],Date())-IIf(Format([ChildDOB12],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 12 Age],
DateDiff("yyyy",[ChildDOB13],Date())-IIf(Format([ChildDOB13],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 13 Age],
DateDiff("yyyy",[ChildDOB14],Date())-IIf(Format([ChildDOB14],"mmdd")>Format(Date(),"mmdd"),1,0)
AS [Child 14 Age]
FROM tblClient INNER JOIN [tblClient Visits] ON tblClient.[Client Nb] =
[tblClient Visits].[Client Nb];

I use this query to create a quarterly comparison report. The ages are
based
on a date of visit for each adult/child field in the table. I don't
understand - even if I stored the age groups in a new table, wouldn't I
still
be creating a calculated expression for each adult/child field? I also
need
to be make sure that any child or adult is grouped correctly as a
child/adult/senior as their date of visit changes. I'm confused as to
how
creating a new table of age groups is simpler, but willing to learn
: - )

:

Deb,

Maybe you need to go back one step further. Your [qry Qtr Stats]
indicates
a whole bunch of fields ([Child 1 Age] .... [Adult 7 Age]) that
obviously
make your quarter stats query extremely difficult to use in any other
query.
I could understand creating a single query like this, to be used in a
report,
but it is highly unlikely that I would then take this denormalized
query (as
you have) and use it generate another query.

What does the SQL for [qry Qtr Stats] look like?

When I do a grouping similar to what you have done, I create a new
table (in
this case I'd call it tbl_Age_Groups) with fields similar to
(StartAge,
EndAge, Group1, Group2) and values like:
StartAge EndAge Group1 Group2
0 3 Child Child 1
3 6 Child Child 2
6 9 Child Child 3

This can make coming up with you age groupings much simplier.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.



:

This Totals query is actually the result of another query in which
I
calculated the ages of each of the individuals using their date of
birth.
Here is the SQL of the totals query:

SELECT Format("Qtr ",[Date of Visit]) & Format([Date of Visit],"q,
yyyy") AS
Qtr, Sum(IIf([Client Age]>17,1,0)+IIf([Adult 1
Age]>17,1,0)+IIf([Adult 2
Age]>17,1,0)+IIf([Adult 3 Age]>17,1,0)+IIf([Adult 4
Age]>17,1,0)+IIf([Adult 5
Age]>17,1,0)+IIf([Adult 6 Age]>17,1,0)+IIf([Adult 7
Age]>17,1,0)+IIf([Child 1
Age]>17,1,0)+IIf([Child 2 Age]>17,1,0)+IIf([Child 3
Age]>17,1,0)+IIf([Child 4
Age]>17,1,0)+IIf([Child 5 Age]>17,1,0)+IIf([Child 6
Age]>17,1,0)+IIf([Child 7
Age]>17,1,0)+IIf([Child 8 Age]>17,1,0)+IIf([Child 9
Age]>17,1,0)+IIf([Child
10 Age]>17,1,0)+IIf([Child 11 Age]>17,1,0)+IIf([Child 12
Age]>17,1,0)+IIf([Child 13 Age]>17,1,0)+IIf([Child 14 Age]>17,1,0))
AS
Adults, Sum(IIf([Child 1 Age]<18,1,0)+IIf([Child 2
Age]<18,1,0)+IIf([Child 3
Age]<18,1,0)+IIf([Child 4 Age]<18,1,0)+IIf([Child 5
Age]<18,1,0)+IIf([Child 6
Age]<18,1,0)+IIf([Child 7 Age]<18,1,0)+IIf([Child 8
Age]<18,1,0)+IIf([Child 9
Age]<18,1,0)+IIf([Child 10 Age]<18,1,0)+IIf([Child 11
Age]<18,1,0)+IIf([Child
12 Age]<18,1,0)+IIf([Child 13 Age]<18,1,0)+IIf([Child 14
Age]<18,1,0)) AS
Children, Sum(IIf([Client Age]>64,1,0)+IIf([Adult 1
Age]>64,1,0)+IIf([Adult 2
Age]>64,1,0)+IIf([Adult 3 Age]>64,1,0)+IIf([Adult 4
Age]>64,1,0)+IIf([Adult 5
Age]>64,1,0)+IIf([Adult 6 Age]>64,1,0)+IIf([Adult 7 Age]>64,1,0))
AS Seniors
FROM [qry Qtr Stats]
GROUP BY Format("Qtr ",[Date of Visit]) & Format([Date of
Visit],"q, yyyy")
ORDER BY Min(Format([Date of Visit],"yyyy")), Min(Format([Date of
Visit],"q"));

Your suggestion that I go back to the original data and change the
row and
column headings - Should this be done in a crosstab query?
My difficulty with setting this up as a crosstab query was that I
need to
display multiple rows and each of them will show the results of
calulated
expressions and not values from a single field. I hope I'm making
sense and
thanks for your help.

:

What does the originale data look like?

If this were in a table, I would tell you to create a normalizing
query,
then crosstab on that:

Select Qtr, "Adults" as AgeGroup, [Adults] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Seniors" as AgeGroup, [Seniors] as Qty
FROM yourTable
UNION ALL
Select Qtr, "Children" as AgeGroup, [Children] as Qty
FROM yourTable

Then, save this query and use it as the source for your crosstab,
but since
this is just the results of a query, I'd go back to the original
data,
change the Field that has the RowHeading to the field that
contains you
AgeGroups, set the column with the Qtr as the Column Headings,
and count or
sum or whatever on the other column to get the Quantity

If you need more help, post the SQL of the query that got you to
the
original Totals query.

HTH
Dale

I have a Totals query that correctly shows the following:

Qtr Adults Seniors Children
1 10 8 2
2 7 3 6
3 2 4 5
4 9 1 3

What I need is the Qtr to display as column headings and the
Adults,
Seniors, and Children to display as rows. I tried both a
crosstab query
and a
pivot table but with no success. Each of the values are
calculated
expressions based on date fields in my table. Any help is
greatly
appreciated. I am using Access 2003.
 
D

Deb H

Thank you for taking the time to help me improve this database. To answer
each of your questions:

This database was nearly complete when I first posted my query question. So
I have to decide whether or not I should redesign it because of the time and
delay factor in getting this up and running. At the present time the tables
contain only test data. If I proceed with the design of the tables as I set
them up (Clients and ClientVisits) and not create the Dependents table, what
are the downsides to that decision? Will the database run more slowly, do I
risk errors in accuracy of my monthly and quarterly reports, etc.

The database is used for households who receive monthly food, clothing, and
other assistance. We need to track the "items" they receive at each visit,
how many individuals are in the household at the time of visit (tends to vary
from visit to visit), and the number of individuals by age groups who
received the items. A household usually has both other adults and children
for each visit.

VisitNumber is set as the primary key in the Client_Visits table.

On a first time visit, all of the individuals (including head of household
which is stored in the Clients table) are entered. All other adults and
children are stored in the Visits table. On subsequent visits, this data is
copied forward to the new visit record. If household members need to be added
or removed, these changes are made to that visit.

Other and Child fields hold names for each field. I've created combo boxes
with value lists for the relationship fields. Gender is F or M.

Any further advise on how to proceed is much appreciated. I do understand
your comment about designing the table as a spreadsheet. At the time, it
seemed like the best way to store the visit data and show it to my end users.
I'll wait to hear from you.

Dale Fye said:
Deb,

How far along are you in development of this database application? The
reason I ask, is that although I would strongly recommend you redesign the
database structure along the lines I mentioned before, doing the redesign
could require a lot of re-engineering of the forms, queries, and reports you
already have created. BTW, most database developers will strongly advise
against putting spaces in the name of your tables or fields. If you feel
you need a space for readability, use and underscore ( _ ).

From the appearance of [tblClient_Visits], it appears that you will only
have data in one of the Other or Child blocks per visit, is that correct?

You didn't send me the indices, but I'll assume that VisitNumber is an
Autonumber primary key (if that is not correct, please let me know).

How do you know which fields (Other1 - Other7, ChildFirst1-ChildFirst14) to
use to record for a particular visit, or does a client bring in multiple
dependents during a given visit, and you just log them all in? For purposes
of the query below, I'm going to assume that the value that goes in these
fields is a name.

What are the values that ultimately go in the following fields ([Other1],
[OtherGender1], and [Relationship1])?
Once you have addressed these questions, I'll continue the re-engineering
thread.

In the meantime, let me address the query from your previous post. The
first thing I would do is create a normalizing query. This query will take
data from a non-normal data structure and put it into one that is easier to
query. To do this, we will create a union query which groups all of the
Other, Gender, DOB, and Relationship fields into 6 columns. The key to a
union query is that each individual SELECT statement in the query must
return the same number of fields, in the same sequence, and they must be of
the same data type as the fields in the same position in the first of the
SELECT statements.

You can start this out by using the query grid to get the first set of
values but it is really just as easy to start out in the SQL view. Create a
new query. Add [tblClient Visit] to the grid, close the dialog box, and
then change to the SQL view. The query will look like the following (I'm
not going to do all of the rows, but you will get the idea). Since you can
join back to [tblClient Visit] to get the visit date and other pertinent
data, I'll leave that out of the union query.

SELECT VisitNumber, "Other1" as FieldTitle, [Other1] as DepName,
[OtherDOB1] as DOB, OtherGender1 as Gender,
Relationship1 as Relation
FROM [tblClient Visit]
WHERE [Other1] is NOT NULL
UNION ALL
SELECT VisitNumber, "Other2" as FieldTitle, [Other2] as DepName,
[OtherDOB2] as DOB, OtherGender2 as Gender,
Relationship2 as Relation
FROM [tblClient Visit]
WHERE [Other2] is NOT NULL
UNION ALL
....
SELECT VisitNumber, "ChildFirst1" as FieldTitle, [ChildFirst1] as DepName,
[ChildDOB1] as DOB, ChildGender1 as Gender,
RelationshipCh1 as Relation
FROM [tblClient Visit]
WHERE [ChildFirst1] is NOT NULL
UNION ALL
SELECT VisitNumber, "ChildFirst1" as FieldTitle, [ChildFirst1] as DepName,
[ChildDOB1] as DOB, ChildGender1 as Gender,
RelationshipCh1 as Relation
FROM [tblClient Visit]
WHERE [ChildFirst1] is NOT NULL
UNION ALL
....
SELECT VisitNumber, "ChildFirst14" as FieldTitle, [ChildFirst14] as DepName,
[ChildDOB14] as DOB, ChildGender14 as Gender,
RelationshipCh14 as Relation
FROM [tblClient Visit]
WHERE [ChildFirst1] is NOT NULL

Hopefully, this will work the first time you run it, but you must pay
meticulous attention as you copy and paste from one group of fields to the
next that you don't forget to change the numeric value at the end of the
field. Once you fill in all of the ... to take account of all of the other
field groups, this query will give you a list of all of the dependents (or
others) that were seen during a given visit. This assumes that the [Other1]
or [ChildFirst1] field will be NULL if those segments were not used. Once
the query runs, save it query as [qryClientVisitNormal].

Then, to test whether you got all of the colums right, you might try
something like the following. This should give you a list of all of the
fields that you have used (Other1 - Other7, ChildFirst1-ChildFirst14) and
the count of the number of times they are used in the table. Don't worry
about the count, it is just there to give you an idea of how many times that
field (or group of fields) are used in the table.

SELECT [FieldTitle], Count([VisitNumber] FROM [qryClientVisitNormal]
GROUP BY [FieldTitle]

To expand this to include the actual client, you could add another UNION ALL
and SELECT statement as shown below, although I didn't see fields for the
client name, dob, gender in [tblClient Visit], so I'm thinking that you may
have these stashed away in [tblClient]. The addition to the above query
might look like:

UNION SELECT
SELECT VisitNumber, "Client" as FieldTitle, [ClientName] as DepName,
[Client DOB] as DOB, [Client Gender] as Gender,
"Client" as Relation
FROM tblClient INNER JOIN [tblClient Visits]
ON tblClient.[Client Nb] = [tblClient Visits].[Client Nb];

The next step is to join this query to [tblClient Visits] to get the Number
in household, for each visit. Since the same client may report different
dependents on different visits. Don't know if this is really an option, or
of importance. If you don't add the last step mentioned above, then you
will need to change the Count( ) to Count( ) + 1 as [# in HH] to account for
the actual client.

SELECT [tblClient Visits].[Client Nb], [tblClient Visits].VisitNumber,
Count(qryClientVisitNormal.FieldTitle) as [# in HH]
FROM [tblClient Visits] INNER JOIN [qryClientVisitNormal]
ON [tblClient Visits].VisitNumber = [qryClientVisitNormal].VisitNumber
GROUP BY [tblClient Visits].[Client Nb], [tblClient Visits].VisitNumber

Getting the age of each of the members of the HH is the next step. The first
thing I notices looking at your query is that you are not computing the ages
on the date of the visit, but rather, on the date that you run the report.
WAS THAT YOUR INTENT? I'll assume not, since that really doesn't make a lot
of sense. So, the query would now look like:

SELECT [tblClient Visits].[Client Nb], [tblClient Visits].VisitNumber,
[FieldTitle], [DepName]
DateDiff("yyyy", [DOB], [Date of Visit]) - IIF(Format([DOB],
"mmdd") > Format([Date of Visit], "mmdd"), 1, 0)
FROM [tblClient Visits] INNER JOIN [qryClientVisitNormal]
ON [tblClient Visits].[VisitNumber] = [qryClientVisitNormal].[VisitNumber]

This is a lot to absorb, so if you get to this point, and are ready to
continue, let me know. Then we can address your original Totals query and
the transformation of that.

HTH
Dale

Deb H said:
Here is the report for the table. I am sending the report for the Client
visits which is the related table that contains the fields we discussed.
This
is the many side of a relationship to a Client table that contains fields
for
the main client (head of household). Thanks for your patience and help.

Columns
Name Type Size
VisitNumber Long Integer 4
Client Nb Long Integer 4
Date of Visit Date/Time 8
Notes Text 255
ClothingOnly Yes/No 1
ExtraFood Yes/No 1
PersonalProd Yes/No 1
Backpack Long Integer 4
Clothes Long Integer 4
Diapers Long Integer 4
GasVoucher Long Integer 4
Layette Long Integer 4
Assistance Long Integer 4
BirthdayBag Long Integer 4
Other Long Integer 4
HHitems Long Integer 4
GiftCertif Currency 8
Other1 Text 50
OtherDOB1 Date/Time 8
OtherGender1 Text 50
Relationship1 Text 50
Other2 Text 50
OtherDOB2 Date/Time 8
OtherGender2 Text 50
Relationship2 Text 50
Other3 Text 50
OtherDOB3 Date/Time 8
OtherGender3 Text 50
Relationship3 Text 50
Other4 Text 50
OtherDOB4 Date/Time 8
OtherGender4 Text 50
Relationship4 Text 50
Other5 Text 50
OtherDOB5 Date/Time 8
OtherGender5 Text 50
Relationship5 Text 50
Other6 Text 50
OtherDOB6 Date/Time 8
OtherGender6 Text 50
Relationship6 Text 50
Other7 Text 50
OtherDOB7 Date/Time 8
OtherGender7 Text 50
Relationship7 Text 50
ChildFirst1 Text 50
ChildDOB1 Date/Time 8
ChildGender1 Text 50
RelationshipCh1 Text 50

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 2
ChildFirst2 Text 50
ChildDOB2 Date/Time 8
ChildGender2 Text 50
RelationshipCh2 Text 50
ChildFirst3 Text 50
ChildDOB3 Date/Time 8
ChildGender3 Text 50
RelationshipCh3 Text 50
ChildFirst4 Text 50
ChildDOB4 Date/Time 8
ChildGender4 Text 50
RelationshipCh4 Text 50
ChildFirst5 Text 50
ChildDOB5 Date/Time 8
ChildGender5 Text 50
RelationshipCh5 Text 50
ChildFirst6 Text 50
ChildDOB6 Date/Time 8
ChildGender6 Text 50
RelationshipCh6 Text 50
ChildFirst7 Text 50
ChildDOB7 Date/Time 8
ChildGender7 Text 50
RelationshipCh7 Text 50
ChildFirst8 Text 50
ChildDOB8 Date/Time 8
ChildGender8 Text 50
RelationshipCh8 Text 50
ChildFirst9 Text 50
ChildDOB9 Date/Time 8
ChildGender9 Text 50
RelationshipCh9 Text 50
ChildFirst10 Text 50
ChildDOB10 Date/Time 8
ChildGender10 Text 50
RelationshipCh10 Text 50
ChildFirst11 Text 50
ChildDOB11 Date/Time 8
RelationshipCh11 Text 50
ChildGender11 Text 50
ChildFirst12 Text 50
ChildDOB12 Date/Time 8
ChildGender12 Text 50
RelationshipCh12 Text 50
ChildFirst13 Text 50
ChildDOB13 Date/Time 8
ChildGender13 Text 50
RelationshipCh13 Text 50
ChildFirst14 Text 50
ChildDOB14 Date/Time 8
ChildGender14 Text 50
RelationshipCh14 Text 50
FTemployment Yes/No 1
PTemployment Yes/No 1

C:\Documents and Settings\Deb\My Documents\Database\ACBC
clients.mdb Tuesday, March 17, 2009
Table: tblClient Visits Page: 3
MFIP Yes/No 1
SocSec Yes/No 1
SSD Yes/No 1
GA Yes/No 1
UsingFS Yes/No 1
UsingWIC Yes/No 1
MA Yes/No 1
Relationships
tblClienttblClient Visits
tblClient tblClient Visits
Client Nb 1 ? Client Nb
Attributes: Enforced
RelationshipType: One-To-Many
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top