如何通过一个具有公共值的表链接两个不相关的表?

时间:2019-03-24 19:12:33

标签: sql oracle-sqldeveloper

我要将佣金百分比列添加到现有查询中。但是,佣金数据位于与查询中使用的主表(A)不相关的表中。但是,这两个表与表B具有相同的列/值。

我下面有三个表A,B和C

Table A     
Reference   Value_Name  Renewal_Code
1           A           N
2           A           R
3           B           N
4           A           R
4           A           N

Table B     
Reference   Value_Name  Prod_Code
1           A           0016
2           A           0027
4           A           0032
4           A           0032

Table C     
A_Prod_Code      A_Tans_Code    **Commission_Percent**
0016             Renewal        5
0027             Renewal        5
0032             New            10
0032             Renewal        5

我需要从Commission_Percent获取Table C,该Renewal_Code与来自Table A的相应A_Tans_Code有关。这与Table C中的Table C相同,除了Renewal拼写出NewTable AR仅使用NCommission_Percent

通过使用Table B作为公用值,我已经能够将NULL列拉到输出中,但是所有值都显示为Renewal_code/A_Trans_Code

我还尝试了使用解码语句来链接Tables A and C中的( SELECT distinct c.commision_percent FROM TableA a JOIN TableB b ON a.reference = b.reference AND b.value_name = 'A' JOIN TableC c ON b.prod_code = c.a_prod_code AND b.value_name = 'A' JOIN TableC c ON a.renewal_code = decode(c.a_trans_code, 'Rewnal','R','New','N') ) Commission_Percent 列。

commission_percent

对于每个参考,我都需要RenewalNew正确的NULLs业务。到目前为止,由于很难链接Tables AC's Renewal_codeA_prod_code列,我只能得到int l1; //123456 for example scanf("%d",&l1); char s[sizeof(l1)]; sprintf(s,"%5d",l1);' //This will give you separate digits of the number in char format inside s[0],s[1] //and so on. //If you want them in int format, declare a int array say int i[sizeof(l1)] and add //the following code for(int c=1;c<=sizeof(l1);c++){ i[c] = s[c] - '0'; } //Now i[0], i[1] etc will have the digits in int format

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

这通过清理临时表并重新插入数据来创建可重现的测试。 并可能成为其他解决方案的模型。

SQL'Select ...'连接到一个具有打开条件的两个部分的单个TableC-prod_code和renewal_code(而不是两个连接)。只需删除“ a。*”即可在您的sql中使用它。 (将解码功能更改为使用A_Trans_Code的第一个字符的下标)。

IF OBJECT_ID('tempdb..#TableA') IS NOT NULL DROP TABLE #TableA
GO
CREATE TABLE #TableA
    (     Reference          INTEGER 
        , Value_Name         VARCHAR(10)
        , Renewal_Code       VARCHAR(10)    )

    INSERT INTO #TableA VALUES( 1, 'A', 'N'   );
    INSERT INTO #TableA VALUES( 2, 'A', 'R'   );
    INSERT INTO #TableA VALUES( 3, 'B', 'N'   );
    INSERT INTO #TableA VALUES( 4, 'A', 'R'   );
    INSERT INTO #TableA VALUES( 4, 'A', 'N'   );

IF OBJECT_ID('tempdb..#TableB') IS NOT NULL DROP TABLE #TableB
GO
CREATE TABLE #TableB
    (     Reference          INTEGER 
        , Value_Name         VARCHAR(10)
        , Prod_Code          VARCHAR(10)    )

    INSERT INTO #TableB VALUES( 1, 'A', '0016'   );
    INSERT INTO #TableB VALUES( 2, 'A', '0027'   );
    INSERT INTO #TableB VALUES( 4, 'A', '0032'   );
    INSERT INTO #TableB VALUES( 4, 'A', '0032'   );

IF OBJECT_ID('tempdb..#TableC') IS NOT NULL DROP TABLE #TableC
GO
CREATE TABLE #TableC
    (     Prod_Code          VARCHAR(10)
        , A_Trans_Code        VARCHAR(10)
        , Commission_Percent INTEGER    )

    INSERT INTO #TableC VALUES( '0016', 'Renewal', 5   );
    INSERT INTO #TableC VALUES( '0027', 'Renewal', 5   );
    INSERT INTO #TableC VALUES( '0032', 'New',     10   );
    INSERT INTO #TableC VALUES( '0032', 'Renewal', 5   );



SELECT distinct a.*,  c.commission_percent 
          FROM #TableA a
          JOIN #TableB b ON a.reference = b.reference 
            AND a.value_name = b.value_name 
          JOIN #TableC c ON b.prod_code = c.prod_code 
           AND a.renewal_code = SUBSTRING(c.a_trans_code,1,1) 

结果是-

Reference   Value_Name  Renewal_Code    commission_percent
2           A           R               5
4           A           N               10
4           A           R               5 

要放入您的sql中的代码

   ( SELECT distinct  c.commission_percent 
              FROM TableA a
              JOIN TableB b ON a.reference = b.reference 
                AND a.value_name = b.value_name 
              JOIN TableC c ON b.prod_code = c.prod_code 
               AND a.renewal_code = SUBSTRING(c.a_trans_code,1,1) 
    ) Commission_Percent

答案 1 :(得分:0)

 WITH table_a AS (

      SELECT 1 AS reference, 'A' AS value_name, 'N' AS renewal_code FROM DUAL UNION ALL
      SELECT 2 AS reference, 'A' AS value_name, 'R' AS renewal_code FROM DUAL UNION ALL
      SELECT 3 AS reference, 'B' AS value_name, 'N' AS renewal_code FROM DUAL UNION ALL
      SELECT 4 AS reference, 'A' AS value_name, 'R' AS renewal_code FROM DUAL UNION ALL
      SELECT 4 AS reference, 'A' AS value_name, 'N' AS renewal_code FROM DUAL

      ),
      table_b AS (

      SELECT 1 AS reference, 'A' AS value_name, '0016' AS prod_code FROM DUAL UNION ALL
      SELECT 2 AS reference, 'A' AS value_name, '0027' AS prod_code FROM DUAL UNION ALL
 /*     SELECT 4 AS reference, 'A' AS value_name, '0032' AS prod_code FROM DUAL UNION ALL duplicate row excluded */
      SELECT 4 AS reference, 'A' AS value_name, '0032' AS prod_code FROM DUAL

      ),
      table_c AS (

      SELECT '0016' AS a_prod_code, 'Renewal'   AS a_tans_code,  5 AS commission_percent FROM DUAL UNION ALL
      SELECT '0027' AS a_prod_code, 'Renewal'   AS a_tans_code,  5 AS commission_percent FROM DUAL UNION ALL
      SELECT '0032' AS a_prod_code, 'New'       AS a_tans_code, 10 AS commission_percent FROM DUAL UNION ALL
      SELECT '0032' AS a_prod_code, 'Renewal'   AS a_tans_code,  5 AS commission_percent FROM DUAL

      )

          SELECT a.reference AS ref_a,
                 a.value_name AS value_name_a,
                 b.reference AS ref_b,
                 b.value_name AS value_name_b,
                 a.renewal_code,
                 b.prod_code,
                 c.commission_percent
            FROM table_a a
 LEFT OUTER JOIN table_b b
                  ON a.value_name = b.value_name
                 AND a.reference = b.reference
 LEFT OUTER JOIN table_c c
                  ON SUBSTR(c.a_tans_code, 1, 1) = a.renewal_code
                 AND c.a_prod_code = b.prod_code
 ;

公用表表达式(CTE)只是用于创建与发布相同的值(CTE是使用WITH构造的部分)。

结果:

      REF_A VALUE_NAME_A         REF_B VALUE_NAME_B    RENEWAL_CODE    PROD_CODE    COMMISSION_PERCENT
 ---------- --------------- ---------- --------------- --------------- ---------- --------------------
          2 A                        2 A               R               0027                          5
          4 A                        4 A               N               0032                         10
          4 A                        4 A               R               0032                          5
          1 A                        1 A               N               0016                           
          3 B                                          N                                              

您可能必须检查发布的行中不导致联接的值。这还假定没有code:code_name关系违反代码名= code的首字母规则。