2016-07-06 12 views
0

在下面的SQL中,它只查看位置標識= 5的那些憑證。我該如何編碼,我只希望從僅訪問了location_ID 5的Patient_ID開始編碼?只需要在訪問一個位置時選擇

SELECT "Vouchers"."Patient_ID", "vwGenPatInfo"."Patient_Number", 
     "Practitioners"."Practitioner_ID", "Practitioners"."First_Name", 
     "Practitioners"."Last_Name", "vwGenPatInfo"."Patient_First_Name", 
     "vwGenPatInfo"."Patient_Last_Name", "vwGenPatInfo"."Patient_DOB", 
     "vwGenPatInfo"."Patient_Sex", "Vouchers"."Carrier_ID", 
     "Vouchers"."Billing_Date", "Vouchers"."Patient_Policy_ID", 
     "Vouchers"."Location_ID" 
FROM ("Ntier_70751"."PM"."vwGenPatInfo" "vwGenPatInfo" 
INNER JOIN "Ntier_70751"."PM"."Vouchers" "Vouchers" 
ON "vwGenPatInfo"."Account_ID"="Vouchers"."Account_ID") 
INNER JOIN "Ntier_70751"."PM"."Practitioners" "Practitioners" 
ON "Vouchers"."Actual_Prov_Practitioner_ID"="Practitioners"."Practitioner_ID" 
-- 
WHERE "Vouchers"."Location_ID"=5 
+2

請提供樣本數據和期望的結果。另外,學習使用表別名,以便您的查詢更易於編寫和閱讀。除非必須,否則不要轉義表和列名。 –

+0

通過'Patient_Id'對'Location_ID'組進行條件聚合。 –

回答

2

這裏有一種方法可以做到這一點。我也擺脫了所有不需要的雙引號,並使用了適當的別名。

SELECT V.Patient_ID 
    , gpi.Patient_Number 
    , P.Practitioner_ID 
    , P.First_Name 
    , P.Last_Name 
    , gpi.Patient_First_Name 
    , gpi.Patient_Last_Name 
    , gpi.Patient_DOB 
    , gpi.Patient_Sex 
    , V.Carrier_ID 
    , V.Billing_Date 
    , V.Patient_Policy_ID 
    , V.Location_ID 
FROM Ntier_70751.PM.vwGenPatInfo gpi 
INNER JOIN Ntier_70751.PM.Vouchers V ON gpi.Account_ID = V.Account_ID 
INNER JOIN Ntier_70751.PM.Practitioners P ON V.Actual_Prov_Practitioner_ID = P.Practitioner_ID 
cross apply 
(
    select V2.Account_ID 
    from Ntier_70751.PM.Vouchers V2 
    where V2.Account_ID = V.Account_ID 

    group by V2.Account_ID 
    HAVING MAX(Location_ID) = 5 
     AND MIN(Location_ID) = 5 
) x 
+0

你可以用獨特的查詢來做到嗎?這是我的想法 - 一個子查詢運行一個獨特的查詢位置只返回5? – MageeWorld

+0

@MageeWorld不確定你的意思。一個明確的查詢將限制我認爲在這種情況下不會有幫助的行數。 –

0

把條件說成是說;

WHERE "Vouchers"."Location_ID" = 5 
+1

這將返回任何至少有一次訪問5的人,OP要求只訪問過5的人。 –

0

我會去只使用條件「WHERE‘優惠券’。‘LOCATION_ID’= 5'will返回參觀了該位置至少一次,但不是所有的Patient_IDs不存在

SELECT "Vouchers"."Patient_ID", "vwGenPatInfo"."Patient_Number", 
     "Practitioners"."Practitioner_ID", "Practitioners"."First_Name", "Practitioners"."Last_Name", "vwGenPatInfo"."Patient_First_Name", "vwGenPatInfo"."Patient_Last_Name", "vwGenPatInfo"."Patient_DOB", "vwGenPatInfo"."Patient_Sex", "Vouchers"."Carrier_ID", "Vouchers"."Billing_Date", "Vouchers"."Patient_Policy_ID", "Vouchers"."Location_ID" 
FROM "Ntier_70751"."PM"."vwGenPatInfo" "vwGenPatInfo" INNER JOIN 
    "Ntier_70751"."PM"."Vouchers" "Vouchers" 
    ON "vwGenPatInfo"."Account_ID" = "Vouchers"."Account_ID" INNER JOIN 
     "Ntier_70751"."PM"."Practitioners" "Practitioners" 
    ON "Vouchers"."Actual_Prov_Practitioner_ID" = "Practitioners"."Practitioner_ID" 
WHERE "Vouchers"."Location_ID"=5 
    and not exists (select 1 
        FROM "Ntier_70751"."PM"."Vouchers" "Vouchers2" 
        WHERE "Vouchers2"."Patient_ID" = "Vouchers2"."Patient_ID" 
         AND "Vouchers2"."Location_ID"<>5) 
+0

這會產生所需的結果,但不可用。你的子查詢中有一個<>,所以它必須評估每一行。 –

+0

@SeanLange - 這取決於。我猜如果location_id = 5的患者數量很少,那麼只有那些患者將在子查詢上評估(如果patient_id已編入索引)。無論如何,沒有架構規範很難知道。 – vercelli

+0

它取決於優惠券中的行數。而且,無論發生什麼情況,當您在where子句中都有<>時,它會導致掃描。 –

0

只。有幾種方法可以做到,但最乾淨的是使用max(location_id)< 5和min(location_id)> 5

相關問題