I have some problems with a SQL statistics page, hope you can help me.
I have two tables. Table one: Customers (Sites) with their name and the machines they have. Looks like: Custname, MachineA, MachineB. Custname is text, MachineA and MachineB is a checkbox (Yes/No)
Table two: Visits (Visits) where we can add each visit we make to the customer for a machine. Looks Like: Date, Custname, Machine. Date is date of the visit, Custname the name, and in Machine we can choose A or B.
Now I want to make a query for some visit statistics per Machine (user can choose by combobox on a form). For example when I want to see statistics for machine A:
- I always want to see all the customers who has machine A
- For each customer I want to see the date of last visit for machine A and total visits for machine A. If there are no visits for machine A at that customer, it still has to show up (witch gives total visits for that customer: 0)
I already tried to make something like this (in this example for machine A):
SELECT Sites.Custname, Sites.MachineA, Sites.MachineB, Max(Visits.Date) AS MaxOfDate, Visits.Machine, Count(Visits.Machine) AS CountOfMachine
FROM Visits RIGHT JOIN Sites ON Visits.Custname = Sites.Custname
GROUP BY Sites.Custname, Sites.MachineA, Sites.MachineB, Visits.Machine
HAVING (((Sites.MachineA)=Yes) AND ((Visits.Machine) Is Null Or (Visits.Machine)='A'));
The problem with this query: When a customer has machine A AND B, and they have had a visit for machine B but not for machine A, it wont show up anymore in the statistics of machine A, because of the last part of the SQL:
HAVING (((Sites.MachineA)=Yes) AND ((Visits.Machine) Is Null Or (Visits.Machine)='A'));
They have Machine A, but Visits.Machine is not empty (its B)
I cant figure out how to solve this!
Oh I Use Access 2003 mode (mdb)
Hopefully you understand my issue and have some good SQL or VBA tag :)
Thanks in advance!!