PC Review


Reply
Thread Tools Rate Thread

Creating calculated fields for a query using data from another table

 
 
John S. Ford, MD
Guest
Posts: n/a
 
      1st Dec 2006
I have two tables:

tblEmployees:
EmployeeIDNum (primary key)
Name

tblEmployeeReviews
EmployeeReviewIDNum (primary key)
EmployeeIDNum (foreign key)
Score (an integer 1, 2 or 3)

This is a one-to-many relationship in that every employee will have multiple
reviews.

What I want is to create a query such that for each employee in
tblEmployees, I have three fields (Score1, Score2, Score3) containing the
percentage of that employee's Scores that were 1, 2 and 3. For each Employee
(i.e. each record in the query), these three fields should add up to 100%.

I figure I can start with tblEmployees and add fields using some form of the
DCount function eg. for Score1:

Score1: DCount("EmployeeReviewIDNum", "tblEmployeeReviews",
tblEmployeeReviews!EmployeeIDNum=tblEmployees!EmployeeIDNum AND
tblEmployeeReviews!Score= 1")/DCount("EmployeeReviewIDNum",
"tblEmployeeReviews",
tblEmployeeReviews!EmployeeIDNum=tblEmployees!EmployeeIDNum")

I can't get this to work. Does anyone know what I'm doing wrong or is there
an easier way to do this?

John



 
Reply With Quote
 
 
 
 
John Spencer
Guest
Posts: n/a
 
      1st Dec 2006
UNTESTED SQL Statement follows

SELECT E.Name
, Abs(Sum(Score=1)) as Ones
, Abs(Sum(Score=2)) as Twos
, Abs(Sum(Score=1)) as Threes
, Count(Score) as NumberReviews
, Ones/NumberReviews as OneScore
, Twos/NumberReviews as TwoScore
, Threes/NumberReviews as ThreeScore
FROM TblEmployees as E Inner Join tblEmployeeReviews as R
ON E.EmployeeIDNum = R.EmployeeID.Num
GROUP BY E.Name

You might have to substitute the following for the percentage calculations
Abs(Sum(Score=1)) /Count(Score) as OneScore

"John S. Ford, MD" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have two tables:
>
> tblEmployees:
> EmployeeIDNum (primary key)
> Name
>
> tblEmployeeReviews
> EmployeeReviewIDNum (primary key)
> EmployeeIDNum (foreign key)
> Score (an integer 1, 2 or 3)
>
> This is a one-to-many relationship in that every employee will have
> multiple
> reviews.
>
> What I want is to create a query such that for each employee in
> tblEmployees, I have three fields (Score1, Score2, Score3) containing the
> percentage of that employee's Scores that were 1, 2 and 3. For each
> Employee
> (i.e. each record in the query), these three fields should add up to 100%.
>
> I figure I can start with tblEmployees and add fields using some form of
> the
> DCount function eg. for Score1:
>
> Score1: DCount("EmployeeReviewIDNum", "tblEmployeeReviews",
> tblEmployeeReviews!EmployeeIDNum=tblEmployees!EmployeeIDNum AND
> tblEmployeeReviews!Score= 1")/DCount("EmployeeReviewIDNum",
> "tblEmployeeReviews",
> tblEmployeeReviews!EmployeeIDNum=tblEmployees!EmployeeIDNum")
>
> I can't get this to work. Does anyone know what I'm doing wrong or is
> there
> an easier way to do this?
>
> John
>
>
>



 
Reply With Quote
 
John S. Ford, MD
Guest
Posts: n/a
 
      2nd Dec 2006
Thanks John, I'm going to try that. But shouldn't I use:

Abs(Count(Score=1))/Count(Score) As OneScore instead of

Abs(Sum(Score=1))/Count(Score) As OneScore?

John

"John Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> UNTESTED SQL Statement follows
>
> SELECT E.Name
> , Abs(Sum(Score=1)) as Ones
> , Abs(Sum(Score=2)) as Twos
> , Abs(Sum(Score=1)) as Threes
> , Count(Score) as NumberReviews
> , Ones/NumberReviews as OneScore
> , Twos/NumberReviews as TwoScore
> , Threes/NumberReviews as ThreeScore
> FROM TblEmployees as E Inner Join tblEmployeeReviews as R
> ON E.EmployeeIDNum = R.EmployeeID.Num
> GROUP BY E.Name
>
> You might have to substitute the following for the percentage calculations
> Abs(Sum(Score=1)) /Count(Score) as OneScore
>
> "John S. Ford, MD" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >I have two tables:
> >
> > tblEmployees:
> > EmployeeIDNum (primary key)
> > Name
> >
> > tblEmployeeReviews
> > EmployeeReviewIDNum (primary key)
> > EmployeeIDNum (foreign key)
> > Score (an integer 1, 2 or 3)
> >
> > This is a one-to-many relationship in that every employee will have
> > multiple
> > reviews.
> >
> > What I want is to create a query such that for each employee in
> > tblEmployees, I have three fields (Score1, Score2, Score3) containing

the
> > percentage of that employee's Scores that were 1, 2 and 3. For each
> > Employee
> > (i.e. each record in the query), these three fields should add up to

100%.
> >
> > I figure I can start with tblEmployees and add fields using some form of
> > the
> > DCount function eg. for Score1:
> >
> > Score1: DCount("EmployeeReviewIDNum", "tblEmployeeReviews",
> > tblEmployeeReviews!EmployeeIDNum=tblEmployees!EmployeeIDNum AND
> > tblEmployeeReviews!Score= 1")/DCount("EmployeeReviewIDNum",
> > "tblEmployeeReviews",
> > tblEmployeeReviews!EmployeeIDNum=tblEmployees!EmployeeIDNum")
> >
> > I can't get this to work. Does anyone know what I'm doing wrong or is
> > there
> > an easier way to do this?
> >
> > John
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make table query with calculated date fields Harry Microsoft Access Queries 9 28th Nov 2007 01:10 PM
Trouble with creating a simple query with calculated fields. Rex Microsoft Access 6 15th Mar 2007 02:09 PM
Creating calculated fields for a query using data from another table John S. Ford, MD Microsoft Access Queries 2 2nd Dec 2006 06:44 AM
Data from calculated fields in form not getting into table.. =?Utf-8?B?U2VhbiBEdVBvbnQ=?= Microsoft Access Getting Started 16 4th Nov 2006 10:39 PM
Calculated Form Fields do not fill data into related table fields =?Utf-8?B?cmljaDE4Mzg=?= Microsoft Access Forms 1 12th Feb 2005 08:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:55 AM.