2017-09-24 10 views
1

我正在使用SQL Server查詢設計器嘗試並形成一個外部查詢,該查詢將返回每個被保險人的家庭策略和沒有策略的全名和地址。我創建語句如下:SQL外部加入 - 加入需要3個表

CREATE TABLE Address (
    AddressID integer NOT NULL, 
    HouseNumber Integer NOT NULL, 
    Street varchar(20) NOT NULL, 
    CityCounty varchar(20) NOT NULL, 
    StateAbb char(2), 
    CountryAbb char(2) NOT NULL, 
    Zip char(5) NOT NULL, 
    LastUpdatedBy varchar(20) NOT NULL, 
    LastUpdated date NOT NULL, 
    CONSTRAINT PK_Address PRIMARY KEY (AddressID)); 

    CREATE TABLE Insured(
    InsuredID integer NOT NULL, 
    FirstName varchar(15) NOT NULL, 
    LastName varchar(15) NOT NULL, 
    MI char(1), 
    DateOfBirth date NOT NULL, 
    CreditScore integer NOT NULL, 
    AddressID integer NOT NULL, 
    DriversLicenseNumber varchar(35), 
    LastUpdatedBy varchar(20) NOT NULL, 
    LastUpdated date NOT NULL, 
    CONSTRAINT PK_Insured PRIMARY KEY (InsuredID), 
    CONSTRAINT FK_InsuredAddress FOREIGN KEY (AddressID) references Address); 

    CREATE TABLE Policy(
    PolicyID integer NOT NULL, 
    EffectiveDate date NOT NULL, 
    TerminationDate date NOT NULL, 
    Amount Numeric (8,2) NOT NULL, 
    PolicyYear integer NOT NULL, 
    PolicyType char(1) NOT NULL, 
    InsuredID integer NOT NULL, 
    AddressID integer NOT NULL, 
    LastUpdatedBy varchar(20) NOT NULL, 
    LastUpdated date NOT NULL, 
    CONSTRAINT PK_Policy PRIMARY KEY (PolicyID), 
    CONSTRAINT FK_PolicyAddress FOREIGN KEY (AddressID) references Address, 
    CONSTRAINT FK_PolicyInsured FOREIGN KEY (InsuredID) references Insured); 

    CREATE TABLE Home(
    PolicyID integer NOT NULL, 
    ExteriorType varchar(30) NOT NULL, 
    Alarm char(3) NOT NULL, 
    DistanceToFireStation integer NOT NULL, 
    LastUpdatedBy varchar(20) NOT NULL, 
    LastUpdated date NOT NULL, 
    CONSTRAINT PK_Home PRIMARY KEY (PolicyID), 
    CONSTRAINT FK_HomePolicy FOREIGN KEY (PolicyID) references Policy); 

    CREATE TABLE Auto(
    PolicyID integer NOT NULL, 
    VinNumber varchar(30) NOT NULL, 
    Make varchar(15) NOT NULL, 
    Model varchar(20) NOT NULL, 
    MilesPerYear integer NOT NULL, 
    LastUpdatedBy varchar(20) NOT NULL, 
    LastUpdated date NOT NULL, 
    CONSTRAINT PK_Auto PRIMARY KEY (PolicyID), 
    CONSTRAINT FK_AutoPolicy FOREIGN KEY (PolicyID) references Policy); 

我相信,查詢需要的表處理,保險,政策和外部右側或左側加入,但我不能讓SQL服務器認識到這一點,因爲它使形成內連接和交叉加入。我需要什麼樣的查詢才能確保家庭政策和他們的地址,並確保沒有政策和他們的地址?

我試過到目前爲止:

SELECT Insured.InsuredID, Insured.FirstName, 
     Insured.LastName, Address.HouseNumber, 
     Policy.PolicyID 
FROM Address RIGHT JOIN Policy 
ON Address.AddressID = Policy.AddressID 
RIGHT JOIN Insured ON Policy.AddressID = Insured.AddressID 
ORDER BY Insured.InsuredID 

這是最近的查詢,返回什麼,我需要與家庭政策,被保險人但沒有一個政策,我得到空的地址被保險人。

SELECT i.InsuredID, i.FirstName, i.MI, i.LastName,    
a.HouseNumber, a.Street, a.CityCounty, a.StateAbb, a.CountryAbb, a.Zip 
FROM INSURED i 
LEFT JOIN (SELECT * FROM Policy WHERE PolicyType = 'H') HomePolicy on  
i.InsuredID = HomePolicy.InsuredID 
LEFT JOIN Address a on HomePolicy.AddressID = a.AddressID; 
+0

向我們展示您的工作。 – nicomp

+0

我已經得到的最接近的:SELECT Insured.InsuredID,Insured.FirstName,Insured.LastName,Address.HouseNumber,Policy.PolicyID 發件人地址 RIGHT JOIN策略在Address.AddressID = Policy.AddressID 右連接投保Policy.AddressID = Insured.AddressID ORDER BY Insured.InsuredID; – nammrick

+0

我會添加到您原來的問題 - 請在將來這樣做 –

回答

0

你能嘗試此查詢:

SELECT i.InsuredID, 
i.FirstName, 
i.LastName, 
a.HouseNumber, 
p.PolicyID 
FROM insured i 
LEFT JOIN policy p ON i.AddressID = p.AddressID AND p.PolicyType = 'H' 
LEFT JOIN address a ON i.AddressID = a.AddressID 
ORDER BY i.InsuredID; 

我想加入是在錯誤的順序。這是否給你你需要的東西? 更新:將受保人表加入地址表將顯示地址,而不管他們是否有策略。

+0

我改變了你發佈的一些信息,並將其返回投保人的住房政策,但對於沒有保單的投保人,他們的地址返回爲空,所以我肯定會越來越近。 – nammrick

+0

啊,明白了。我已經改變了一點,現在可以嗎? – bbrumm

+0

這絕對更接近。現在唯一的問題是,查詢返回所有的保險(住房政策,汽車政策,沒有政策)不僅保證了住房政策和保險沒有政策。 – nammrick

0

數據庫設計看起來不錯。我認爲我們對「PolicyType」列略有疑問。它所持有的價值以及本專欄的目的是什麼。

PolicyType='H'這意味着它是家政策。或者其他找到相同查詢的方式是檢查home表中是否存在policyid

這是正確的嗎?

主查詢,

我需要爲與家庭政策 和他們的地址和被保險人沒有政策及其地址返回被保險人查詢什麼?

檢查這個腳本,

--insureds with home policies and their addresses 
select i.InsuredID, 
i.FirstName, 
i.LastName 
A.HouseNumber 
,1 INDICATE 
from Insured i 
INNER JOIN policy p ON i.InsuredID = p.InsuredID 
INNER JOIN [Address] A ON A.ADDRESSID=I.ADDRESSID 
WHERE EXISTS(SELECT PolicyID FROM Home H WHERE h.PolicyID=P.PolicyID) 
AND NOT EXISTS(SELECT PolicyID FROM [Auto] a WHERE A.PolicyID=P.PolicyID) 

UNION ALL 
--insureds with no policy and their addresses 
select i.InsuredID, 
i.FirstName, 
i.LastName 
,A.HouseNumber 
,0 INDICATE 
from Insured i 
INNER JOIN [Address] A ON A.ADDRESSID=I.ADDRESSID 
WHERE EXISTS(SELECT InsuredID FROM policy p WHERE i.InsuredID = p.InsuredID) 

我有使用「EXISTS條款」,因爲表列未在您需要的輸出。