Dink_buddy,
In order to follow along with my answer, please download your database
from
http://www.hammerdata.com/Google/
I have built the queries you requested into your database, so you can
try them on your sample data.
In order to make this more understandable and maintainable, I did this
as a query and subquery, rather than using a single SQL statement. You
still only need to run a single query to get your results.
If you look at the list of queries in your database, you will find two
new ones:
qryHDSMain - This is the one you run to get your results
qryHDSCountSE - The workhorse. Does some presummarizing to feed
qryHDSMain
Query Details
---------------
**** qryHDSCountSE ****
Here is the SQL broken out and commented. It is available in a
pastable form in the sample database.
' Use DISTINCT to get an accurate count of the number of search
engines. This combines the SE/SP pairs so that each SE only appears
once per report, regardless of how many phrases you tested with.
SELECT DISTINCT
----------------------------------------
' Include the Report No so we can link to qryHDSMain
TblSearchPositions.RNo,
------------------------------------------
' Include the SENo so we know which engines hit
TblSearchPositions.SENo,
--------------------------------------------
' Get the number of positions per engine by counting how many phrases
hit
Count(TblSearchPositions.SPNo) AS CountOfSPNo,
---------------------------------------------
' This section figures out how many hits landed in the specified
range. We're using a trick here. In the first line, which checks for
number 1 hits, the boolean expression [Overall]=1 returns True if
Overall=1 and False if it doesn't. True = -1 and False = 0. We apply
Abs to change it to True = 1 and False = 0. Now, each record where
Overall = 1 will place a 1 in the field. Sum adds them up and we know
how many there were. To get the other counts, we just change the
range!
Sum(Abs([Overall]=1)) AS Top1,
Sum(Abs([Overall]<=5)) AS Top5,
Sum(Abs([Overall]<=10)) AS Top10,
Sum(Abs([Overall]<=20)) AS Top20
-----------------------------------------
' Specify which table we want
FROM TblSearchPositions
-------------------------------------------
' The fields we GROUP BY determine what is considered DISTINCT
GROUP BY TblSearchPositions.RNo, TblSearchPositions.SENo
----------------------------------------------
' Sort the records
ORDER BY TblSearchPositions.RNo, TblSearchPositions.SENo;
-----------------------------------------------
So far, so good? Okay, the other query...
**** qryHDSMain ****
' Select the regular fields
SELECT TblReports.ClientNo, TblReports.RNo, TblReports.DateRun,
------------------------------------------------
' Select fields from qryHDSCountSE Counting and Summing as needed
Count(qryHDSCountSE.SENo) AS SECount,
Sum(qryHDSCountSE.CountOfSPNo) AS NumOfPositions,
Sum(qryHDSCountSE.Top1) AS SumOfTop1,
Sum(qryHDSCountSE.Top5) AS SumOfTop5,
Sum(qryHDSCountSE.Top10) AS SumOfTop10,
Sum(qryHDSCountSE.Top20) AS SumOfTop20
---------------------------------------------------
' Joint qryHDSCountSE to this query by matching the Report Number
FROM TblReports INNER JOIN qryHDSCountSE ON TblReports.RNo =
qryHDSCountSE.RNo
----------------------------------------------------
' GROUP BY determines which records get counted and summed
GROUP BY TblReports.ClientNo, TblReports.RNo, TblReports.DateRun
-----------------------------------------------------
' Ask for the Client Number parameter
HAVING (((TblReports.ClientNo)=[Enter Client Number]))
-------------------------------------------------------
' Sort by report number
ORDER BY TblReports.RNo;
---------------------------------------------------------
This will make a lot more sense when you see it in Design and
Datasheet View!
Search strategy: None. Used Access 2000.
Additional Resources:
There is an excellent Access FAQ by MVPS.org
http://www.mvps.org/access/
You may want to take a look at the page about Lookups. You've used
them extensively and you may want to reconsider.
The Evils of Lookup Fields - MVPS.org
http://www.mvps.org/access/lookupfields.htm
Please ask for clarification if you need anything explained further.
Good luck with your Access project!
- Hammer |