Link to home
Start Free TrialLog in
Avatar of Bill Henderson
Bill HendersonFlag for United States of America

asked on

Access 2003 query question

Hi,

I use a Dreamweaver extension (Webassist DB Search) to create a date range search page using ASPvb.

I have a recordset in my results page taken from multiple queries I built inside Access to Join several filtered tables from the same source table (one version uses a value of Accepted, one with Rejected, etc).

I'm using multiple queries inside Access because I need to COUNT, SUM, and AVG various values based on Accepted, Rejected, etc. The current query breaks this down by a UserID assigned to every record in the source table.

The results are like so:

Name | Accepted Count | Total $ Accepted | Rejected Count | Total $ Rejected

Bob         5                               $500                       3                        $300
Doug        7                              $1200                     2                        $1100

And so on.

All of this works! I have all the breakdowns behaving as expected with one caveat: my date range search.

All the records in the source table for these queries contain a date. But if I add date field to any of the queries or the master query that joins them altogether, I get all the SUMS and AVGs but with a row for every actual record, because date becomes part of the query and becomes the default Sort By

Is there a way for my date range search to be tied to these queries in a way that my looped array of records does not need to display all records or dates, but displayed results can still be filtered by date?

Thanks

Bill

Avatar of miqrogroove
miqrogroove
Flag of United States of America image

It sounds like you don't want to add the date to the SELECT portion of the query, but only to the WHERE portion.  For example:

SELECT COUNT(field1) FROM table1 WHERE date1 < #1/1/2008#
Avatar of Bill Henderson

ASKER

Hi - I think the issue there is that all of my selects are occuring inside of Access except the final one. The final one, on my ASP page, can't use a WHERE clause for Date (which is where I would need it to it can vary based on the date range search), because I can't get a date field in any of my Access queries without getting multiple records instead of the single SUM, AVG, COUNT results. or could I somehow have Access pull the date variables from the ASP page and apply them to all of the original queries I'm Joining together?
The SQL language allows for nested queries, as you seem to be aware.  If you paste the SQL code of the queries you have so far, I can combine them into one query for you.  If you could run a combined query all at once from ASP would that make it easier to specify the date condition?
Certainly it would! Thanks.

Here are the separate queries:

Query "TOTALSUMS":

SELECT Sum(Jobs.EstimatedPrice) AS TotalEstimateDollars, Avg(Jobs.EstimatedPrice) AS AvgOfTotalEstimatedPrice, Sum(Jobs.AcceptedPrice) AS SumOfTotalAcceptedPrice, Avg(Jobs.AcceptedPrice) AS AvgOfTotalAcceptedPrice, Count(Jobs.EstimateStatus) AS TotalEstimatesHandled, Users.UserFirstName, Jobs.EstimatorAssigned
FROM Jobs LEFT JOIN Users ON Jobs.EstimatorAssigned = Users.UserID
WHERE (((Jobs.EstimatedPrice)<>0))
GROUP BY Users.UserFirstName, Jobs.EstimatorAssigned;

Query "Accepted":

SELECT Count(Jobs.JobID) AS CountofAccepted, Count(Jobs.EstimatedPrice) AS CountOfAcceptedEstimatedPrice, Avg(Jobs.EstimatedPrice) AS AvgOfAcceptedEstimatedPrice, Sum(Jobs.AcceptedPrice) AS SumOfAcceptedAcceptedPrice, Avg(Jobs.AcceptedPrice) AS AvgOfAcceptedAcceptedPrice, Jobs.EstimatorAssigned
FROM Jobs
WHERE (((Jobs.AcceptedPrice)<>0))
GROUP BY Jobs.EstimatorAssigned;

Query "Created"

SELECT Count(Jobs.JobID) AS CountOfCreated, Jobs.EstimatorAssigned
FROM Jobs
WHERE (((Jobs.EstimateStatus)="Created"))
GROUP BY Jobs.EstimatorAssigned;

Query "Estimated"

SELECT Count(Jobs.JobID) AS CountOfCurrentEstimated, Sum(Jobs.EstimatedPrice) AS SumOfCurrentEstimatedPrice, Avg(Jobs.EstimatedPrice) AS AvgOfCurrentEstimatedPrice, Jobs.EstimatorAssigned
FROM Jobs
WHERE (((Jobs.EstimateStatus)="Estimated"))
GROUP BY Jobs.EstimatorAssigned;

Query "Rejected":

SELECT Count(Jobs.JobID) AS CountOfRejected, Jobs.EstimatorAssigned, Sum(Jobs.EstimatedPrice) AS SumOfRejectedEstimatedPrice, Avg(Jobs.EstimatedPrice) AS AvgOfRejectedEstimatedPrice
FROM Jobs
WHERE (((Jobs.EstimateStatus)="Rejected"))
GROUP BY Jobs.EstimatorAssigned;

And then the query that puts them all together "FinalQuery"

SELECT TOTALSUMS.UserFirstName, TOTALSUMS.TotalEstimatesHandled, TOTALSUMS.TotalEstimateDollars, TOTALSUMS.AvgOfTotalEstimatedPrice, Accepted.CountofAccepted, TOTALSUMS.SumOfTotalAcceptedPrice, TOTALSUMS.AvgOfTotalAcceptedPrice, Estimated.CountOfCurrentEstimated, Estimated.SumOfCurrentEstimatedPrice, Estimated.AvgOfCurrentEstimatedPrice, Accepted.CountofAccepted, Accepted.CountOfAcceptedEstimatedPrice, Accepted.AvgOfAcceptedEstimatedPrice, Accepted.SumOfAcceptedAcceptedPrice, Accepted.AvgOfAcceptedAcceptedPrice, Rejected.CountOfRejected, Rejected.SumOfRejectedEstimatedPrice, Rejected.AvgOfRejectedEstimatedPrice
FROM ((TOTALSUMS LEFT JOIN Estimated ON TOTALSUMS.EstimatorAssigned = Estimated.EstimatorAssigned) LEFT JOIN Accepted ON TOTALSUMS.EstimatorAssigned = Accepted.EstimatorAssigned) LEFT JOIN Rejected ON TOTALSUMS.EstimatorAssigned = Rejected.EstimatorAssigned;

Thanks for your time!

Bill

This is just a sample, as I imagine you can format the rest of the code to your needs ;)

I've taken the first two queries and combined them into one big query using the same joins and nesting.  If you continue this format with the other three queries it should get you to the final result.  The WHERE conditions will need to be parsed 5 times, but it seems like you have a grip on that part. ;)
SELECT
 TOTALSUMS1.UserFirstName,
 TOTALSUMS1.TotalEstimatesHandled,
 TOTALSUMS1.TotalEstimateDollars,
 TOTALSUMS1.AvgOfTotalEstimatedPrice,
 TOTALSUMS1.SumOfTotalAcceptedPrice,
 TOTALSUMS1.AvgOfTotalAcceptedPrice,
 
 Accepted2.CountofAccepted,
 Accepted2.CountofAccepted,
 Accepted2.CountOfAcceptedEstimatedPrice,
 Accepted2.AvgOfAcceptedEstimatedPrice,
 Accepted2.SumOfAcceptedAcceptedPrice,
 Accepted2.AvgOfAcceptedAcceptedPrice,
 
FROM
(
 SELECT
  Users.UserFirstName,
  Jobs.EstimatorAssigned,
  Sum(Jobs.EstimatedPrice) AS TotalEstimateDollars,
  Avg(Jobs.EstimatedPrice) AS AvgOfTotalEstimatedPrice,
  Sum(Jobs.AcceptedPrice) AS SumOfTotalAcceptedPrice,
  Avg(Jobs.AcceptedPrice) AS AvgOfTotalAcceptedPrice,
  Count(Jobs.EstimateStatus) AS TotalEstimatesHandled
 FROM Jobs LEFT JOIN Users ON Jobs.EstimatorAssigned = Users.UserID
 WHERE Jobs.EstimatedPrice <> 0 AND Jobs.Date < #1/1/2008#
 GROUP BY Users.UserFirstName, Jobs.EstimatorAssigned
) AS TOTALSUMS1
 
LEFT JOIN
(
 SELECT
  Count(Jobs.JobID) AS CountofAccepted,
  Count(Jobs.EstimatedPrice) AS CountOfAcceptedEstimatedPrice,
  Avg(Jobs.EstimatedPrice) AS AvgOfAcceptedEstimatedPrice,
  Sum(Jobs.AcceptedPrice) AS SumOfAcceptedAcceptedPrice,
  Avg(Jobs.AcceptedPrice) AS AvgOfAcceptedAcceptedPrice,
  Jobs.EstimatorAssigned
 FROM Jobs
 WHERE Jobs.AcceptedPrice <> 0 AND Jobs.Date < #1/1/2008#
 GROUP BY Jobs.EstimatorAssigned;
) AS Accepted2
ON TOTALSUMS.EstimatorAssigned = Accepted.EstimatorAssigned

Open in new window

The last line there should be corrected to:

ON TOTALSUMS1.EstimatorAssigned = Accepted2.EstimatorAssigned
ASKER CERTIFIED SOLUTION
Avatar of miqrogroove
miqrogroove
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks - it'll take me a while to get my head around this - I'll keep you posted...

Bill
haha okay :)  The only mistake I know I made in that last one was I forgot the 3's and 4's in the query names at the top.  And if you need that 5th table to be in there then that will need to be done.

This query can be run just like the other one you had in ASP.  You will need to insert your date into the query string in 4 or 5 places, but if you can get this to run as-is you're 90% done.
Thanks!