Question

I'm creating an Access 2010 database to replace an old Paradox one. Just now getting to queries, and there is no hiding that I am a new to SQL.

What I am trying to do is set up a query to be used by a graph. The graph's Y axis is to be a simple percentage passed, and the X axis is a certain day. The graph will be created on form load and subsequent new records entered with a date range of "Between Date() And Date()-30" (30 days, rolling).

The database I'm working with can have multiple inspections per day with multiple passes and multiple fails. Each inspection is a separate record.

For instance, on 11/26/2012 there were 7 inspections done; 5 passed and 2 failed, a 71% ((5/7)*100%) acceptance. The "11/26/2012" and "71%" represent a data point on the graph. On 11/27/2012 there were 8 inspections done; 4 passed and 4 failed, a 50% acceptance. Etc.

Here is an example of a query with fields "Date" and "Disposition" of date range "11/26/2012 - 11/27/2012:"

SELECT Inspection.Date, Inspection.Disposition
FROM Inspection
WHERE (((Inspection.Date) Between #11/26/2012# And #11/27/2012#) AND ((Inspection.Disposition)="PASS" Or (Inspection.Disposition)="FAIL"));

Date       | Disposition
11/26/2012 | PASS
11/26/2012 | FAIL
11/26/2012 | FAIL
11/26/2012 | PASS
11/26/2012 | PASS
11/26/2012 | PASS
11/26/2012 | PASS
11/27/2012 | PASS
11/27/2012 | PASS
11/27/2012 | FAIL
11/27/2012 | PASS
11/27/2012 | FAIL
11/27/2012 | PASS
11/27/2012 | FAIL
11/27/2012 | FAIL

*NOTE - The date field is of type "Date," and the Disposition field is of type "Text." There are days where no inspections are done, and these days are not to show up on the graph. The inspection disposition can also be listed as "NA," which refers to another type of inspection not to be graphed.

Here is the layout I want to create in another query (again, for brevity, only 2 days in range):

Date       | # Insp | # Passed | # Failed | % Acceptance
11/26/2012 | 7      | 5        | 2        | 71 
11/27/2012 | 8      | 4        | 4        | 50 

What I think needs to be done is some type of join on the record dates themselves and "calculated fields" in the rest of the query results. The problem is that I haven't found out how to "flatten" the records by date AND maintain a count of the number of inspections and the number passed/failed all in one query. Do I need multiple layered queries for this? I prefer not to store any of the queries as tables as the only use of these numbers is in graphical form.

I was thinking of making new columns in the database to get around the "Disposition" field being Textual by assigning a PASS "1" and a FAIL "0," but this seems like a cop-out. There has to be a way to make this work in SQL, just I haven't found applicable examples.

Thanks for your help! Any input or suggestions are appreciated! Example databases with forms, queries, and graphs are also helpful!

Was it helpful?

Solution

You could group by Date, and then use aggregates like sum and count to calculate statistics for that group:

select  Date
,       count(*) as [# Insp]
,       sum(iif(Disposition = 'PASS',1,0)) as [# Passed]
,       sum(iif(Disposition = 'FAIL',1,0)) as [# Failed]
,       100.0 * sum(iif(Disposition = 'PASS',1,0)) / count(*) as [% Acceptance]
from    YourTable
where   Disposition in ('PASS', 'FAIL')
group by
        Date
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top