4

I have an Access database that tracks machines on a factory shop floor. There are two tables I'm using:

  1. marstable - A list of operations each machine has completed
  2. statustable - A list of status changes to the machines

marstable has about 100,000 entries in it, while statustable has less than 100.

I need to look up one machine at a time to find one of three things:

  1. If the machine has entries in both tables, return the data from both.
  2. If the machine only has entries in marstable, return the data from that table.
  3. If the machine only has entries in statustable, return the data from that one.

How do I do the SQL query to accomplish this? Right now, using a left join, it works EXCEPT in the case where there's only data in the statustable (the 'right' part of the left join).

marstable is a chronological list of transactions, which looks like this:

+--------+----------+-----------+----------+-----------------------+
| order  | machine  | operation | quantity |      insertdate       |
+--------+----------+-----------+----------+-----------------------+
|  12345 | A10      |       110 |     2339 | 11/16/2012 9:57:15 AM |
|  67890 | B11      |       450 |    10330 | 11/16/2012 9:15:03 AM |
+--------+----------+-----------+----------+-----------------------+

statustable is a chronological list of machine status changes. It looks like:

+---------+--------+-----------------------+--------+
| machine | status |      inserttime       |  note  |
+---------+--------+-----------------------+--------+
| A10     |      6 | 11/5/2012 10:52:22 AM | broken |
| J90     |      7 | 11/9/2012 12:48:46 PM | robot  |
+---------+--------+-----------------------+--------+

The SQL query I'm using looks like this:

SELECT TOP 1 [machine], [quantity], [order], [operation], [insertdate], [status], [inserttime], [note]
FROM [marstable] LEFT JOIN [statustable] ON [marstable].[machine] = [statustable].[machine]
WHERE [marstable].[machine] = '<<machine I want get's passed in here>>'
ORDER BY [marstable].[insertdate] DESC, [statustable].[inserttime] DESC

The problem is that when no entry exists in the marstable, but there is one in the statustable, I still don't get anything.

Thanks for your help in advance. Also, I don't have any control over what the tables look like.

Finally, please pardon my lack of ascii-art skillz for the tables above.

UPDATE #1:

I'm trying to implement @Olivier's subquery solution from one of his answers below. However, when I do that, I get an error for too few parameters. Expected 2.

My implementation differs from him in that I pass the machine ID into a function that generates SQL, instead of asking for a parameter.

The SQL gets generated like this

MarsSQL = _
    "SELECT M.msg_machine, M.msg_qtyorstatus, M.msg_entity, M.msg_operation, M.msg_creator, M.msg_insertdate, " & _
        "S.Machine, S.Status, S.InsertTime, S.Note, S.UserID " & _
    "FROM " & _
        "(SELECT TOP 1 msg_machine, msg_qtyorstatus, msg_entity, msg_operation, msg_creator " & _
        "FROM MARSTable WHERE msg_machine = '" & "00" & fMachine & "' ORDER BY msg_insertdate DESC" & _
        ") M, " & _
        "(SELECT TOP 1 Machine, Status, InsertTime, Note " & _
        "FROM StatusTable WHERE Machine = '" & fMachine & "' ORDER BY InsertTime DESC" & _
        ") S;"

fMachine is a string that holds the machine we're currently looking for. The end result is that the SQL looks like this (for machine A10):

    SELECT M.msg_machine, M.msg_qtyorstatus, M.msg_entity, M.msg_operation, M.msg_creator, M.msg_insertdate,
        S.Machine, S.Status, S.InsertTime, S.Note, S.UserID
    FROM
        (SELECT TOP 1 msg_machine, msg_qtyorstatus, msg_entity, msg_operation, msg_creator
        FROM MARSTable WHERE msg_machine = '00A10' ORDER BY msg_insertdate DESC
        ) M,
        (SELECT TOP 1 Machine, Status, InsertTime, Note
        FROM StatusTable WHERE Machine = 'A10' ORDER BY InsertTime DESC
        ) S;

Note: My initial examples used a simplified version of the tables, but this SQL is the actual one that I generate. Also, I've tried this with and without the [square brackets] around each field/table, and it doesn't make a difference.

Update #2: I missed a field in the 2nd subquery. I added it in, and now I get the same error, except it says I'm missing 1 parameter, not 2. Now the SQL looks like this:

SELECT M.msg_machine, M.msg_qtyorstatus, M.msg_entity, M.msg_operation, M.msg_creator, M.msg_insertdate,
    S.Machine, S.Status, S.InsertTime, S.Note, S.UserID
FROM
    (SELECT TOP 1 msg_machine, msg_qtyorstatus, msg_entity, msg_operation, msg_creator
    FROM MARSTable WHERE msg_machine = '00A10' ORDER BY msg_insertdate DESC
    ) M,
    (SELECT TOP 1 Machine, Status, InsertTime, Note, UserID
    FROM StatusTable WHERE Machine = 'A10' ORDER BY InsertTime DESC
    ) S;

UserID is the field I was missing from the second subquery.

SOLVED: I was missing another field from the first subquery. Once I cleared that up, the solution worked. The SQL now looks like this:

SELECT M.msg_machine, M.msg_qtyorstatus, M.msg_entity, M.msg_operation, M.msg_creator, M.msg_insertdate,
    S.Machine, S.Status, S.InsertTime, S.Note, S.UserID
FROM
    (SELECT TOP 1 msg_machine, msg_qtyorstatus, msg_entity, msg_operation, msg_creator, msg_insertdate 
    FROM MARSTable WHERE msg_machine = '00A10' ORDER BY msg_insertdate DESC
    ) M,
    (SELECT TOP 1 Machine, Status, InsertTime, Note
    FROM StatusTable WHERE Machine = 'A10' ORDER BY InsertTime DESC
    ) S;

Thanks to everyone who helped out here.

1

2 Answers 2

2

Since you want only one row, you can do it with two subqueries returning both one row and don't need to join them at all

PARAMETERS param Text(50);
SELECT
    [param] AS machine,
    M.quantity, M.[order], M.operation, M.insertdate,
    S.status, S.inserttime, S.note
FROM
    ( SELECT TOP 1 machine, quantity, [order], operation, insertdate
      FROM marstable
      WHERE machine = [param]
      ORDER BY insertdate DESC
    ) M,
    ( SELECT TOP 1 machine, status, inserttime, note
      FROM statustable
      WHERE machine = [param]
      ORDER BY inserttime DESC
    ) S
3
  • Thanks for both of your answers. I'm trying to implement your subquery approach, but am running into difficulties. So, Access is executing these SQL queries from VBA, and I loop over a list of machines. While doing this, I pass the machine we want into a function that generates an SQL string. When I do this with your solution above, I get an error for too few parameters. Expected 2. Any thoughts? I'll post the actual SQL I'm generating as an update to my question.
    – ahuth
    Nov 20, 2012 at 15:36
  • Oops, I thought the other answer was from you, too, but that one is from sebt. Thanks for this one, though.
    – ahuth
    Nov 20, 2012 at 15:55
  • I fixed my typos in my implementation of your solution, and it worked beautifully. Thanks for your help with this.
    – ahuth
    Nov 20, 2012 at 16:11
2

I doubt Access does FULL OUTER JOINS. May have improved in the latest version(s), but I can't remember ever seeing this in Access. Here's an Access solution:

  1. Create a query that returns all Machine IDs that are in either table:

    SELECT DISTINCT machine FROM marstable UNION SELECT DISTINCT machine FROM statustable

  2. Save that query as, for example QRY_MachinesAnywhere

  3. Create a second query that LEFT JOINs this query to both of the tables:

    SELECT anywhere.machine[, other stuff] FROM QRY_MachinesAnywhere anywhere LEFT JOIN statustable ON anywhere.machine=statustable.machine LEFT JOIN marstable ON anywhere.machine=marstable.machine

(exact syntax may differ slightly - Access seems to like brackets in JOINed FROM expressions, and only joining two things at a time - and may need the explicit AS between "QRY_MachinesAnywhere" and "anywhere": but if you add Qry_MachinesAnywhere and both the original tables into the query using the UI Designer, joining on machine, you should be fine).

NB that you'll need to go into SQL view to create the original UNION query. Can't do this in the designer.

2
  • You can drop the DISTINCT since UNION automatically implies it unless you are using UNION ALL. Nov 16, 2012 at 16:56
  • Thank you so much for your answer. I ended up going with @Olivier's, but I'm sure we could've gotten your answer to work, too.
    – ahuth
    Nov 20, 2012 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.