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?

4

1 に答える 1

1

UNION を使用して、PDU スロットで使用されているすべてのデバイス ID を検索し、使用されているデバイスを除くデバイスのリストを返します。

ここに、開始するための疑似コードをいくつか示します。

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
)
于 2013-07-29T21:08:39.070 に答える