Hello,
I am having a hard time getting a few fairly straightforward SQL statements to combine into a single nested statement, and came here seeking some help.
I have a single table with Subscription ID's and a secondary field containing either N or R. There are also product ID's that can be 1 - 6. The subscription ID's are occasionally duplicated within the listing.
The end result that I need is all unique Subscription ID's with the designation N, in one field, and for the second field to show 1 if there is an R Sub ID that matches that N ID, otherwise zero. From either designation, the product ID has to be 1 3 or 5.
I have been able to do this with three queries, one creates a list of all unique R ID's, the next creates a list of all N IDs (these will be unique by nature), and the third does a left join from the other 2 queries with an iif statement to obtain the final value.
I have attempted to take these 3 queries and merge them into a single nested query, but I have very little experience with this and am not sure if I am running up against nested limitations, or if the query is just written incorrectly.
Following are the three queries that I created, which work, and lastly the attempt to nest them, which does not work.
(Main table here listed as [a])
Query A:
Code:
SELECT DISTINCT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="R") AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5))
Query B:
Code:
SELECT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="n") AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5));
Query C:
Code:
SELECT [Ind New Subs].[Sub ID], IIf([Ind R Subs].[Sub ID]>0,1,0) AS Duplicates
FROM [Ind New Subs] LEFT JOIN [Ind R Subs] ON [Ind New Subs].[Sub ID] = [Ind R Subs].[Sub ID];
Mangled Attempt at the Nested combination:
Code:
SELECT
(SELECT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="n")
AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5)))
As Expr1,
IIf(
(SELECT DISTINCT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="R")
AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5))
) >0,1,0) AS Duplicates
FROM
(SELECT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="n")
AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5)))
LEFT JOIN
(SELECT DISTINCT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="R")
AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5)))
ON
(SELECT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="n")
AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5)))
=
(SELECT DISTINCT [a].[Sub ID]
FROM [a]
WHERE ((([a].[N/R])="R")
AND (([a].[a])=1 Or ([a].[a])=3 Or ([a].[a])=5)));
Any help would be much appreciated.