0

I have a table full of devices that I am trying to assigned to power sockets on PDUs.

I have a table called PDUs with slots, 1-16. Each slot is a field, that looks up a device ID in the devices table. I'd like to have my lookup only present the devices that haven't been assigned a PDU slot.

So far I can create a query that finds the device ids that don't exist in only one slot, but I'd like the query to extend to all the slots. So I'd like to have a query that shows all the un-assigned devices, and have it look at all the slots in the PDU table entries.

I tried to do something like this:

SELECT [Devices].Device
FROM [Devices] LEFT JOIN [PDUs] ON [Devices].[ID] = [PDUs].[Slot 1].[Value] OR [Devices].[ID] = [PDUs].[Slot 2].[Value] OR [Devices].[ID] = [PDUs].[Slot 3].[Value]
WHERE ((([PDUs].[Slot 1].Value) Is Null) OR (([PDUs].[Slot 2].Value) Is Null) OR (([PDUs].[Slot 3].Value) Is Null));

But no success.

Any suggestions?

1
  • Now I am stuck because the form I am using doesn't want to display the current value as it no longer exists in the query. Is there some way to display the current field value?
    – omegacore
    Jul 30, 2013 at 0:14

1 Answer 1

1

I would use UNIONs to find all device IDs that are in use in the PDU slots, then return the list of devices excluding those used.

Here is some pseudo-code to get you started.

SELECT *
FROM [Devices]
where [Devices].[ID] NOT IN
(
SELECT Slot1 FROM PDUs WHERE Slot1 IS NOT NULL
UNION ALL
SELECT Slot2 FROM PDUs WHERE Slot2 IS NOT NULL
UNION ALL
SELECT Slot3 FROM PDUs WHERE Slot3 IS NOT NULL
UNION ALL
SELECT Slot4 FROM PDUs WHERE Slot4 IS NOT NULL
UNION ALL
SELECT Slot5 FROM PDUs WHERE Slot5 IS NOT NULL
UNION ALL
SELECT Slot6 FROM PDUs WHERE Slot6 IS NOT NULL
)
4
  • It says that this operation is not allowed in subqueries?
    – omegacore
    Jul 29, 2013 at 21:23
  • Can you try this pattern SELECT * FROM [Devices] where [Devices].[ID] NOT IN ( SELECT Slot1 FROM PDUs WHERE Slot1 IS NOT NULL ) AND [Devices].[ID] NOT IN ( SELECT Slot2 FROM PDUs WHERE Slot2 IS NOT NULL )
    – Declan_K
    Jul 29, 2013 at 21:30
  • Argh, apparently I can't use this because the table is a part of the query. The message is: "No valid fields can be found in 'Query1'. You may have selected a query that uses the table you're adding the lookup table to, please select a new source.
    – omegacore
    Jul 29, 2013 at 21:57
  • I was using the wizard, entering this manually in design view works.
    – omegacore
    Jul 29, 2013 at 22:57

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.