SQL - n层Join语句

时间:2015-06-24 19:21:09

标签: sql

我正在将一个祖先表加入到一个查询中,该查询给出了预期的结果,但在少数情况下,它会为每个客户端返回多行。我只需要返回' MAX'从列中输入。

SELECT      C.FULL_NAME_ALTERNATE || ' (' || C.CLIENT_NUMBER || ')' AS  CLIENT,
            SA.ORGANIZATION_ID                                  AS  SA_ORG,
            SA.PROGRAM_NAME                                     AS  PROGRAM,
            CPA.PROCEDURES                                      AS  PROCEDURE,
            SUM(SA.ACTIVITY_UNIT)                               AS  USED_UNITS,
            AP.DEFAULT_PROCEDURE_FEE                            AS  FEE,
            (SUM(SA.ACTIVITY_UNIT) * AP.DEFAULT_PROCEDURE_FEE)  AS  USED_DOLLARS,
            AP.ORGANIZATION_ID                                  AS  AP_ORG

FROM        RPT_CLIENT          C
INNER JOIN  RPT_CLIENT_PAYER_AUTH   CPA ON  C.CLIENT_ID = CPA.CLIENT_ID
INNER JOIN  RPT_SCHEDULED_ACTIVITIES    SA  ON  SA.ACTIVITY_DETAIL_ID = CPA.ACTIVITY_DETAIL_ID
INNER JOIN RPT_ORGANIZATION_RELATIVE    R   ON  R.DESCENDANT_ORG_ID = CPA.ORGANIZATION_ID
INNER JOIN  RPT_ADMIN_PROCEDURE     AP  ON  CPA.PROCEDURES =  AP.PROCEDURE_CODE                                                         
                                    AND (AP.END_DATE > CURRENT_DATE OR AP.END_DATE IS NULL)
                                    AND AP.ORGANIZATION_ID = R.ANCESTOR_ORG_ID

GROUP BY    C.FULL_NAME_ALTERNATE || ' (' || C.CLIENT_NUMBER || ')',
            SA.ORGANIZATION_ID,
            SA.PROGRAM_NAME,
            CPA.PROCEDURES,
            AP.DEFAULT_PROCEDURE_FEE,
            AP.ORGANIZATION_ID

返回如下内容:

CL    ID   PROG  PROC   UNIT    DOLLARS     FEE    SA_ID
Bob  1029   ACH W7075   4069    37272.04    9.16    1029
Bob  1142   RCH W6099   344     5149.68     14.97   1142
Bob  1142   RCH W6099   344     5772.32     16.78   1137
Jen  1029   ACH W7074   3898    17541       4.5     1029
Jen  1024   BEH W7095   55      1068.1      19.42   1024
Jen  1142   RES W6099   343     5134.71     14.97   1142
Jen  1142   RES W6099   343     5755.54     16.78   1137
Dave 1186   RES W6098   350     64004.5     182.87  1137
Dave 1186   RES W6098   350     64004.5     182.87  1186
Dave 1186   RES W6099   350     5873        16.78   1137
Dave 1186   RES W6099   350     5873        16.78   1186

我需要为每个客户端,ID,程序和过程返回1行。当存在倍数时,需要使用具有max(sa_id)的行 * sa_id和id并不总是匹配,但是当它们这样做时,它们将被选中。

1 个答案:

答案 0 :(得分:0)

关键是添加一个语句,过滤掉不是join子句的max sa记录的sa记录,如下所示:

SELECT      C.FULL_NAME_ALTERNATE || ' (' || C.CLIENT_NUMBER || ')' AS  CLIENT,
            SA.ORGANIZATION_ID                                  AS  SA_ORG,
            SA.PROGRAM_NAME                                     AS  PROGRAM,
            CPA.PROCEDURES                                      AS  PROCEDURE,
            SUM(SA.ACTIVITY_UNIT)                               AS  USED_UNITS,
            AP.DEFAULT_PROCEDURE_FEE                            AS  FEE,
            (SUM(SA.ACTIVITY_UNIT) * AP.DEFAULT_PROCEDURE_FEE)  AS  USED_DOLLARS,
            AP.ORGANIZATION_ID                                  AS  AP_ORG

FROM        RPT_CLIENT          C
INNER JOIN  RPT_CLIENT_PAYER_AUTH   CPA ON  C.CLIENT_ID = CPA.CLIENT_ID
INNER JOIN  RPT_SCHEDULED_ACTIVITIES    SA  ON  SA.ACTIVITY_DETAIL_ID = CPA.ACTIVITY_DETAIL_ID
INNER JOIN RPT_ORGANIZATION_RELATIVE    R   ON  R.DESCENDANT_ORG_ID = CPA.ORGANIZATION_ID
INNER JOIN  RPT_ADMIN_PROCEDURE     AP  ON  CPA.PROCEDURES =  AP.PROCEDURE_CODE                                                         
                                    AND (AP.END_DATE > CURRENT_DATE OR AP.END_DATE IS NULL)
                                    AND AP.ORGANIZATION_ID = R.ANCESTOR_ORG_ID
and sa_id = (select max(sa_id) from RPT_SCHEDULED_ACTIVITIES x where x.ACTIVITY_DETAIL_ID = CPA.ACTIVITY_DETAIL_ID)

GROUP BY    C.FULL_NAME_ALTERNATE || ' (' || C.CLIENT_NUMBER || ')',
            SA.ORGANIZATION_ID,
            SA.PROGRAM_NAME,
            CPA.PROCEDURES,
            AP.DEFAULT_PROCEDURE_FEE,
            AP.ORGANIZATION_ID