如何在同一个表上内部连接3个请求

时间:2016-11-15 09:37:16

标签: sql oracle-sqldeveloper

我正在尝试在同一张桌子上加入3个不同请求的3个结果:

我的表:

TB_DW_CA_DETAIL_AGG
    DATE_VOL
    NUMERO_VOL
    CODE_CLIENT
    SITE_PROD
    CODE_CLASSE
    STATUT_FIAB
    CENTRE_PROFIT
    TYPE_CA
    MONTANT_QUOD
    MONTANT_RISTOURNE
    MONTANT_AN
    MONTANT_TOT_QUOD
    MONTANT_TOT_MENS
    MONTANT_TOT_AN
    DATE_CHGT 

我的要求:

SELECT * FROM (

        SELECT
          TB_DW_CA_DETAIL_AGG.DATE_VOL AS DATE_VOL,
          TB_DW_CA_DETAIL_AGG.NUMERO_VOL,
          TB_DW_CA_DETAIL_AGG.CODE_CLIENT,
          TB_DW_CA_DETAIL_AGG.SITE_PROD,
          TB_DW_CA_DETAIL_AGG.CODE_CLASSE,
          TB_DW_CA_DETAIL_AGG.CENTRE_PROFIT,
          sum(TB_DW_CA_DETAIL_AGG.MONTANT_QUOD),
          TB_DW_CA_DETAIL_AGG.STATUT_FIAB,
          sum(TB_DW_CA_DETAIL_AGG.MONTANT_RISTOURNE)
        FROM
          TB_DW_CA_DETAIL_AGG
        WHERE
          (
           TB_DW_CA_DETAIL_AGG.DATE_VOL  <=  trunc(sysdate)-2
           AND
           TO_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'MM'))  =  CASE WHEN TO_NUMBER(TO_CHAR(sysdate,'DD')) = '1' OR TO_NUMBER(TO_CHAR(sysdate,'DD')) = '2' THEN TO_NUMBER(TO_CHAR(sysdate,'MM'))-1
        ELSE TO_NUMBER(TO_CHAR(sysdate,'MM'))
        END
           AND
           To_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'SYYYY'))  =  CASE WHEN TO_NUMBER(TO_CHAR(sysdate,'MM')) = '1' THEN TO_NUMBER(TO_CHAR(sysdate,'SYYYY'))-1
        ELSE TO_NUMBER(TO_CHAR(sysdate,'SYYYY'))
        END
           AND
           TB_DW_CA_DETAIL_AGG.SITE_PROD  <>  'LAT'
          )
        GROUP BY
          TB_DW_CA_DETAIL_AGG.DATE_VOL, TB_DW_CA_DETAIL_AGG.NUMERO_VOL, TB_DW_CA_DETAIL_AGG.CODE_CLIENT, TB_DW_CA_DETAIL_AGG.SITE_PROD, TB_DW_CA_DETAIL_AGG.CODE_CLASSE, 
        TB_DW_CA_DETAIL_AGG.CENTRE_PROFIT, TB_DW_CA_DETAIL_AGG.STATUT_FIAB
        ORDER BY   TB_DW_CA_DETAIL_AGG.DATE_VOL ASC
)  CTE1
INNER JOIN (

    select *  FROM
    (

                      SELECT
                       -- TO_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'MM')),
                        TB_DW_CA_DETAIL_AGG.DATE_VOL AS DATE_VOL,
                        TB_DW_CA_DETAIL_AGG.NUMERO_VOL,
                        TB_DW_CA_DETAIL_AGG.CODE_CLIENT,
                        TB_DW_CA_DETAIL_AGG.SITE_PROD,
                        TB_DW_CA_DETAIL_AGG.CODE_CLASSE,
                        TB_DW_CA_DETAIL_AGG.CENTRE_PROFIT,
                        sum(TB_DW_CA_DETAIL_AGG.MONTANT_QUOD),
                        TB_DW_CA_DETAIL_AGG.STATUT_FIAB
                      FROM
                        TB_DW_CA_DETAIL_AGG
                      WHERE
                      ( TB_DW_CA_DETAIL_AGG.STATUT_FIAB=2  )
                        AND  
                        (
                         TB_DW_CA_DETAIL_AGG.DATE_VOL  <=  trunc(sysdate)-2
                         AND
                         TO_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'MM'))  =  CASE WHEN TO_NUMBER(TO_CHAR(sysdate,'DD')) = '1' OR TO_NUMBER(TO_CHAR(sysdate,'DD')) = '2' THEN TO_NUMBER(TO_CHAR(sysdate,'MM'))-1
                      ELSE TO_NUMBER(TO_CHAR(sysdate,'MM'))
                      END
                         AND
                         To_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'SYYYY'))  =  CASE WHEN TO_NUMBER(TO_CHAR(sysdate,'MM')) = '1' THEN TO_NUMBER(TO_CHAR(sysdate,'SYYYY'))-1
                      ELSE TO_NUMBER(TO_CHAR(sysdate,'SYYYY'))
                      END
                         AND
                         TB_DW_CA_DETAIL_AGG.SITE_PROD  <>  'LAT'
                        )
                      GROUP BY
                       -- TO_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'MM')), 
                        TB_DW_CA_DETAIL_AGG.DATE_VOL, 
                        TB_DW_CA_DETAIL_AGG.NUMERO_VOL, 
                        TB_DW_CA_DETAIL_AGG.CODE_CLIENT, 
                        TB_DW_CA_DETAIL_AGG.SITE_PROD, 
                        TB_DW_CA_DETAIL_AGG.CODE_CLASSE, 
                        TB_DW_CA_DETAIL_AGG.CENTRE_PROFIT, 
                        TB_DW_CA_DETAIL_AGG.STATUT_FIAB
                                   )CTE2
                    INNER JOIN( 
                     SELECT
                        TB_DW_CA_DETAIL_AGG.DATE_VOL AS DATE_VOL,
                        TB_DW_CA_DETAIL_AGG.NUMERO_VOL,
                        TB_DW_CA_DETAIL_AGG.CODE_CLIENT,
                        TB_DW_CA_DETAIL_AGG.SITE_PROD,
                        TB_DW_CA_DETAIL_AGG.CODE_CLASSE,
                        TB_DW_CA_DETAIL_AGG.CENTRE_PROFIT,
                        sum(TB_DW_CA_DETAIL_AGG.MONTANT_QUOD),
                        TB_DW_CA_DETAIL_AGG.STATUT_FIAB
                      FROM
                        TB_DW_CA_DETAIL_AGG
                      WHERE
                      ( TB_DW_CA_DETAIL_AGG.STATUT_FIAB=1  )
                        AND  
                        (
                         TB_DW_CA_DETAIL_AGG.DATE_VOL  <=  trunc(sysdate)-2
                         AND
                         TO_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'MM'))  =  CASE WHEN TO_NUMBER(TO_CHAR(sysdate,'DD')) = '1' OR TO_NUMBER(TO_CHAR(sysdate,'DD')) = '2' THEN TO_NUMBER(TO_CHAR(sysdate,'MM'))-1
                      ELSE TO_NUMBER(TO_CHAR(sysdate,'MM'))
                      END
                         AND
                         To_NUMBER(TO_CHAR(TB_DW_CA_DETAIL_AGG.DATE_VOL,'SYYYY'))  =  CASE WHEN TO_NUMBER(TO_CHAR(sysdate,'MM')) = '1' THEN TO_NUMBER(TO_CHAR(sysdate,'SYYYY'))-1
                      ELSE TO_NUMBER(TO_CHAR(sysdate,'SYYYY'))
                      END
                         AND
                         TB_DW_CA_DETAIL_AGG.SITE_PROD  <>  'LAT'


                        )
                      GROUP BY
                        TB_DW_CA_DETAIL_AGG.DATE_VOL, TB_DW_CA_DETAIL_AGG.NUMERO_VOL, TB_DW_CA_DETAIL_AGG.CODE_CLIENT, TB_DW_CA_DETAIL_AGG.SITE_PROD, TB_DW_CA_DETAIL_AGG.CODE_CLASSE, 
                      TB_DW_CA_DETAIL_AGG.CENTRE_PROFIT, TB_DW_CA_DETAIL_AGG.STATUT_FIAB
                      ORDER BY 
                      TB_DW_CA_DETAIL_AGG.DATE_VOL) CTE3
                      ON CTE2.DATE_VOL = CTE3.DATE_VOL ) CTE4
 ON CTE1.DATE_VOL=CTE4.DATE_VOL;

问题是:我不知道如何加入我的两个小组CTE1&amp; CTE4。 (在我的请求结束时)

 ON CTE2.DATE_VOL = CTE3.DATE_VOL ) CTE4
     ON CTE1.DATE_VOL=CTE4.DATE_VOL; 

更多信息

这三个要求之间的唯一区别是:
 WHERE条件中的( TB_DW_CA_DETAIL_AGG.STATUT_FIAB=2 ),谁给了我不同的结果:sum(TB_DW_CA_DETAIL_AGG.MONTANT_QUOD)

我想加入同样的人:

DATE_VOL
NUMERO_VOL
CODE_CLIENT
SITE_PROD
CODE_CLASSE
STATUT_FIAB
CENTRE_PROFIT

sum(TB_DW_CA_DETAIL_AGG.MONTANT_QUOD)

的结果

( TB_DW_CA_DETAIL_AGG.STATUT_FIAB=2 ) AND (TB_DW_CA_DETAIL_AGG.STATUT_FIAB=1 ) AND 没有这个条件。

  • CTE1是我最大的要求(获得所有航班)
  • CTE4 = CTE2&amp; CTE3
  • CTE1 = STATUT_FIAB上没有条件
  • CTE2 =(TB_DW_CA_DETAIL_AGG.STATUT_FIAB = 2)
  • CTE3 =(TB_DW_CA_DETAIL_AGG.STATUT_FIAB = 1)

非常感谢您提供任何信息。

1 个答案:

答案 0 :(得分:0)

只是一个想法,你真的需要加入吗?或者你可以用这样的子查询来计算总和:

layer {
  name: "fc1"
  type: "InnerProduct"
  bottom: "conv"
  top: "fc1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 49152 # 128 x 128 x 3
     ...
  }
}

layer {
  name: "result"
  type: "SoftmaxWithLoss"
  bottom: "fc1"
  bottom: "label"
  top: "result"
}