I am trying to look through two tables TableA and TableB get print out the TableA.ID of any that show more than 1 count. TableA looks like this:
ID | Code
------------
1 | A
2 | B
3 | C
Table B Looks like
ID | AID | EffectiveDate | ExpirationDate
------------------------------------------------
1 | 1 | 2012-01-01 | 2012-12-31
2 | 1 | 2012-01-01 | 2012-12-31
3 | 2 | 2012-01-01 | 2012-12-31
4 | 3 | 2012-01-01 | 2012-12-31
The Query I am using looks like this:
DECLARE @MoreThanOne varchar(250)
SET @MoreThanOne = ''
IF((SELECT COUNT(*) FROM TableA
WHERE EXISTS(
SELECT TableB.ID
,TableB.EffectiveDate
,TableB.ExpirationDate
FROM TableB
WHERE TableB.AID = TableA.ID
and GETDATE() Between TableB.EffectiveDate and TableB.ExpirationDate
)
GROUP BY TableA.Code) > 1)
BEGIN
--SET @MoreThanOne = @MoreThanOne + TableA.Code + CHAR(10)
END
PRINT @MoreThanOne
I know that my nested Query works when reworked it will print the counts for all in the unique codes in TableA.
I know that I can not use what I commented out because i don’t have access to TableA.Code.
My question is there another way to do this or how can I get access to TableA.Code for the Message MoreThanOne.
Thanks For the help!
This query will get you the codes for all AIDs that are duplicated in table B:
You may also wish to add the
WHEREcondition that have in your stored procedure to the inner select.