2016-07-06 13 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

相关问题