Bill Henderson
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
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
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?
ASKER
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 CountOfAcceptedEstimatedPr ice, Avg(Jobs.EstimatedPrice) AS AvgOfAcceptedEstimatedPric e, 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)="C reated"))
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)="E stimated") )
GROUP BY Jobs.EstimatorAssigned;
Query "Rejected":
SELECT Count(Jobs.JobID) AS CountOfRejected, Jobs.EstimatorAssigned, Sum(Jobs.EstimatedPrice) AS SumOfRejectedEstimatedPric e, Avg(Jobs.EstimatedPrice) AS AvgOfRejectedEstimatedPric e
FROM Jobs
WHERE (((Jobs.EstimateStatus)="R ejected"))
GROUP BY Jobs.EstimatorAssigned;
And then the query that puts them all together "FinalQuery"
SELECT TOTALSUMS.UserFirstName, TOTALSUMS.TotalEstimatesHa ndled, TOTALSUMS.TotalEstimateDol lars, TOTALSUMS.AvgOfTotalEstima tedPrice, Accepted.CountofAccepted, TOTALSUMS.SumOfTotalAccept edPrice, TOTALSUMS.AvgOfTotalAccept edPrice, Estimated.CountOfCurrentEs timated, Estimated.SumOfCurrentEsti matedPrice , Estimated.AvgOfCurrentEsti matedPrice , Accepted.CountofAccepted, Accepted.CountOfAcceptedEs timatedPri ce, Accepted.AvgOfAcceptedEsti matedPrice , Accepted.SumOfAcceptedAcce ptedPrice, Accepted.AvgOfAcceptedAcce ptedPrice, Rejected.CountOfRejected, Rejected.SumOfRejectedEsti matedPrice , Rejected.AvgOfRejectedEsti matedPrice
FROM ((TOTALSUMS LEFT JOIN Estimated ON TOTALSUMS.EstimatorAssigne d = Estimated.EstimatorAssigne d) LEFT JOIN Accepted ON TOTALSUMS.EstimatorAssigne d = Accepted.EstimatorAssigned ) LEFT JOIN Rejected ON TOTALSUMS.EstimatorAssigne d = Rejected.EstimatorAssigned ;
Thanks for your time!
Bill
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)
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)
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)="C
GROUP BY Jobs.EstimatorAssigned;
Query "Estimated"
SELECT Count(Jobs.JobID) AS CountOfCurrentEstimated, Sum(Jobs.EstimatedPrice) AS SumOfCurrentEstimatedPrice
FROM Jobs
WHERE (((Jobs.EstimateStatus)="E
GROUP BY Jobs.EstimatorAssigned;
Query "Rejected":
SELECT Count(Jobs.JobID) AS CountOfRejected, Jobs.EstimatorAssigned, Sum(Jobs.EstimatedPrice) AS SumOfRejectedEstimatedPric
FROM Jobs
WHERE (((Jobs.EstimateStatus)="R
GROUP BY Jobs.EstimatorAssigned;
And then the query that puts them all together "FinalQuery"
SELECT TOTALSUMS.UserFirstName, TOTALSUMS.TotalEstimatesHa
FROM ((TOTALSUMS LEFT JOIN Estimated ON TOTALSUMS.EstimatorAssigne
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. ;)
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
The last line there should be corrected to:
ON TOTALSUMS1.EstimatorAssign ed = Accepted2.EstimatorAssigne d
ON TOTALSUMS1.EstimatorAssign
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks - it'll take me a while to get my head around this - I'll keep you posted...
Bill
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.
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.
ASKER
Thanks!
SELECT COUNT(field1) FROM table1 WHERE date1 < #1/1/2008#