views:

35

answers:

2

Hi, I'm trying to get the AVERAGE from the results of two separate sql queries built in MS Access. The first sql query pulls the largest record:

SELECT DISTINCTROW Sheet1.Tx_Date, Sheet1.LName, Sheet1.Patient_Name, Sheet1.MRN, 
  Max(Sheet1.) AS [Max Of FEV1_ACT], 
  Max(Sheet1.FEF_25_75_ACT) AS [Max Of FEF_25_75_ACT]
FROM Sheet1
GROUP BY Sheet1.Tx_Date, Sheet1.LName, Sheet1.Patient_Name, Sheet1.MRN;

The second sql query pulls the second largest record:

SELECT Sheet1.MRN, Sheet1.Patient_Name, Sheet1.Lname, 
  Max(Sheet1.FEV1_ACT) AS 2ndLrgOfFEV1_ACT, 
  Max(Sheet1.FEF_25_75_ACT) AS 2ndLrgOfFEF_25_75_ACT
FROM Sheet1
WHERE (((Sheet1.FEV1_ACT)<(SELECT MAX( FEV1_ACT )
                 FROM Sheet1 )))
GROUP BY Sheet1.MRN, Sheet1.Patient_Name, Sheet1.Lname;

These two queries work great, I just need some help on pulling the AVERAGE of the results of these two queries into one. Thanks.

A: 

union those queries and average the recordset

select avg(select field from table1 union select field from table2)
David
Tried that. I get a "this operation not allowed in subqueries" error.
A: 

How about

SELECT Avg(FEV1_ACT) FROM
    (SELECT Top 2 FEV1_ACT FROM Sheet1 ORDER BY FEV1_ACT DESC)
Remou
Thanks Remou, that was actually a tremendous help! Running this query, does it prevent me from pulling the other fields as well or can I get the value only?
You can get other fields, ORDER BY controls what the top two are to be. Note that TOP n will select more than n if rows have equal values. If you need other fields, they must be in a GROUP BY or such like in the outer query.
Remou