Tom Moreau
3/30/2007 6:39:00 PM
Try:
select
p.*
from
#Policies p
where not exists
(
select
*
from
#AccountPolicies ap
join #Accounts a on ap.AccountID = a.AccountID
where
p.PolicyID = ap.PolicyID
and a.Active = 'Y'
)
go
BTW, Policy F does not exist.
--
Tom
----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
.
"Terri" <terri@cybernets.com> wrote in message
news:eujjvk$eqv$1@reader2.nmix.net...
How can I identify policies that do not have at least one associated account
that is active?
Desired results:
C-Only associated account is inactive
D-No associated accounts
F-Only associated account is inactive
CREATE TABLE #Accounts
(
AccountID int,
Active char (1)
)
CREATE TABLE #Policies
(
PolicyID char
)
CREATE TABLE #AccountPolicies
(
AccountID int,
PolicyID char
)
INSERT #Accounts (AccountID, Active) VALUES (1,'Y')
INSERT #Accounts (AccountID, Active) VALUES (2,'N')
INSERT #Accounts (AccountID, Active) VALUES (3,'Y')
INSERT #Accounts (AccountID, Active) VALUES (4,'N')
INSERT #Policies (PolicyID) VALUES ('A')
INSERT #Policies (PolicyID) VALUES ('B')
INSERT #Policies (PolicyID) VALUES ('C')
INSERT #Policies (PolicyID) VALUES ('D')
INSERT #Policies (PolicyID) VALUES ('E')
INSERT #AccountPolicies (AccountID,PolicyID) VALUES (1,'A')
INSERT #AccountPolicies (AccountID,PolicyID) VALUES (1,'B')
INSERT #AccountPolicies (AccountID,PolicyID) VALUES (2,'C')
INSERT #AccountPolicies (AccountID,PolicyID) VALUES (3,'A')
INSERT #AccountPolicies (AccountID,PolicyID) VALUES (3,'E')
INSERT #AccountPolicies (AccountID,PolicyID) VALUES (4,'A')
SELECT #Accounts.AccountID, #AccountPolicies.PolicyID FROM #Accounts INNER
JOIN #AccountPolicies ON #Accounts.AccountID = #AccountPolicies.AccountID
DROP TABLE #Accounts
DROP TABLE #Policies
DROP TABLE #AccountPolicies