I have a table in Microsoft SQL called Account
Name AccountNumber IDNumber
Alpha 1 Z
Bravo 2 Y
Charlie 3 X
Delta 4 W
Echo 5 W <---- Echo has same IDNumber as Delta. This is allowed.
I have a list of unknown length of pairs of AccountNumber and IDNumber. Only accounts where the pair match should be returned.
Here is an example of such a list (AccountNumber, IDNumber)
(2,Y) <-- Valid
(4,W) <-- Valid
(1,X) <-- Invalid
(5,X) <-- Invalid
(5,W) <-- Valid
Is it possible to select only such valid pairs?
My attempt
For testing purposes I am using this table and data.
DROP TABLE #Account
CREATE TABLE #Account (
[Name] NVARCHAR(100),
[AccountNumber] NVARCHAR(10),
[IDNumber] NVARCHAR(10)
)
INSERT INTO #Account ([Name],[AccountNumber],[IDNumber]) VALUES ('Alpha' ,'1', 'Z')
INSERT INTO #Account ([Name],[AccountNumber],[IDNumber]) VALUES ('Bravo' ,'2', 'Y')
INSERT INTO #Account ([Name],[AccountNumber],[IDNumber]) VALUES ('Charlie' ,'3', 'X')
INSERT INTO #Account ([Name],[AccountNumber],[IDNumber]) VALUES ('Delta' ,'4', 'W')
INSERT INTO #Account ([Name],[AccountNumber],[IDNumber]) VALUES ('Echo' ,'5', 'W')
This correctly returns the accounts.
SELECT * FROM #Account
WHERE AccountNumber IN ('2','4','1') AND IDNumber IN ('Y','W','X')
Name AccountNumber IDNumber
Bravo 2 Y
Delta 4 W
But this is incorrect
SELECT * FROM #Account
WHERE AccountNumber IN ('4','5') AND IDNumber IN ('W','X')
Name AccountNumber IDNumber
Delta 4 W
Echo 5 W
I need 4-W paired and 5-X paired.
(My aim is to do this from C# using Linq. As a first attempt I would like to see if this is doable in SQL.)