views:

75

answers:

2

I am trying to create a query within a commercial ERP system that uses Access as its database, E2 Shop System. So it just runs the query through Access.

I want the query to tell me "how much time was logged on every machine on a prompted date, even if it is 0". I have figured out how to get it to output how much time is on a machine, but only for a machine that has time on it.

Here is the simplified example:

Table: M(machine)

Name  |   ID
------------
Saw   |   1
Drill |   2
Weld  |   3
Lathe |   4
Paint |   5

Table: T(time)

Hours   |  Date  |  Name
-------------------------
8       | 10-25  |  Weld
6       | 10-25  |  Saw
10      | 10-25  |  Lathe
2       | 10-01  |  Drill

The output I want from the query when prompted for Date 10-25 is to get:

Saw   |  6
Drill |  0
Weld  |  8
Lathe |  10
Paint |  0

All I can figure out is this, and it limits the output to only machines that have time against them

SELECT M.name, T.time
FROM M, T
WHERE T.Date = [ENTER DATE POSTED (MM/DD/YY):]))
ORDER BY M.ID ;

I'am thinking that CASE could help, but I can't get it to work.

+1  A: 

Use a left join:

SELECT M.Name, COALESCE(T.time, 0)
FROM M
LEFT JOIN T
ON M.Name = T.Name AND T.Date = ...
ORDER BY M.Id

I don't think Access has the COALESCE though... you may be able to use Nz instead.

Related:

Mark Byers
+1 you beat me :(
Abe Miessler
It works without COALESCE or Nz But I can't get it to work with either, and therefore I can't get it to show 0 on the other machines. Thanks a ton! though, you answered in like 3 minutes.
Harry Hood
I doubt Access understands CASE. You can try IIF(isnull([fieldname],0,[fieldnanm])
Tony Toews
Case and IIF don't work either. Is "IIF" supposed to be "IF", either way it doesn't work. Thanks everyone for the help, when I get home tonight I'll try running these ideas through Access, the E2 software doesn't like them.
Harry Hood
There is no If() function in Access or Jet/ACE SQL, but there is an IIf() function, so @Harry Hood, I think you're wrong.
David-W-Fenton
@Mark Byers: please don't post SQL that is incompatible with Access/Jet/ACE when the question is about Access/Jet/ACE. -1
David-W-Fenton
If you've explained why your answer is inapplicable, why not just edit your answer to make it apply to Access? Methinks thou dost protest too much.
David-W-Fenton
+1  A: 

The WHERE statement excludes records where the date is null, which is the case for the time table, so you need to add Or Is Null. This can get more complicated, depending on the records you want returned, you may need a sub-query.

SELECT M.Name, Nz([Hours],0) AS Hrs
FROM Machine AS M 
LEFT JOIN [Time] AS T 
ON M.Name = T.Name
WHERE T.Date=#10/25/2010# Or T.Date Is Null
ORDER BY M.ID;

I hope you do not really have fields called name and tables called time, because these are reserved words.

EDIT re comment

SELECT m.Name, IIf(IsNull(ts.Hours),0,ts.Hours) AS Hrs
FROM Machine AS m 
LEFT JOIN (
     SELECT t.Name,t.Date,t.Hours 
     FROM [Time] AS T 
     WHERE T.Date=#10/25/2010#) AS ts 
ON m.Name = ts.Name;
Remou
Your right,those aren't the real names, I just thought it might make it easy for anyone who has a similar problem. But the "T.Date IS NULL" only returns a machine that has never had time posted against it. And the Nz function isn't recognized, at least by the E2 Software that runs the access database.
Harry Hood
As I said, you may need a subquery, I have edited to show this. I have also adjusted for Nz.
Remou
I may have complicated it trying to make it easier, but this is what I am trying and it isn't working. Is this what you are saying to do?----------- SELECT m.ShortName, IIf(IsNull(ts.MachHrs),0,ts.MachHrs) AS Hrs FROM WorkCntr AS m LEFT JOIN ( Select t.WorkCntr, t.TicketDate, t.MachHrs FROM [TimeTicketDet] AS t WHERE T.TicketDate=DateValue(Now) AS ts ON m.WorkCntr = ts.WorkCntr;
Harry Hood
That seems okay at first glance, except for `TicketDate=DateValue(Now)` use `TicketDate=Date()` if you do not need a time value.
Remou
I just used the DateValue(Now) because I wanted to get the rest of the Query working first, and I know it works because the wizard generates it. When I get home I'll plug it into Access itself, to see if E2 is the one not allowing it.
Harry Hood