How to link two unrelated tables, through one table with common values?Add a column with a default value to an existing table in SQL ServerFinding duplicate values in a SQL tableWhat are the options for storing hierarchical data in a relational database?About SQL join statementUpdate multiple rows of a table having specific value in particular column equal to value calculated from two other tablesHow NATURAL JOIN works with two tables when there are two or more common columns?How can I perform this join on a table with multiple records per key without returning multiple records per key?SQL Multiple joins with OR conditionUpdating New Created Tables From Two Tables - SQLpostgres delete from indirectly related tables

New pedal fell off maybe 50 miles after installation. Should I replace the entire crank, just the arm, or repair the thread?

Is using 'echo' to display attacker-controlled data on the terminal dangerous?

Why does logistic function use e rather than 2?

Why not invest in precious metals?

Longest bridge/tunnel that can be cycled over/through?

Fermat's statement about the ancients: How serious was he?

What ways have you found to get edits from non-LaTeX users?

Why can I traceroute to this IP address, but not ping?

Check if three arrays contains the same element

sed + add word before string only if not exists

Live action TV show where High school Kids go into the virtual world and have to clear levels

Wooden cooking layout

How do free-speech protections in the United States apply in public to corporate misrepresentations?

Should I ask for an extra raise?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Traversing Oceania: A Cryptic Journey

How to hide an urban landmark?

Why does the Mishnah use the terms poor person and homeowner when discussing carrying on Shabbat?

If I leave the US through an airport, do I have to return through the same airport?

Teaching a class likely meant to inflate the GPA of student athletes

Print lines between start & end pattern, but if end pattern does not exist, don't print

How can I end combat quickly when the outcome is inevitable?

Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?

How do you say "homebrewer" in Spanish?



How to link two unrelated tables, through one table with common values?


Add a column with a default value to an existing table in SQL ServerFinding duplicate values in a SQL tableWhat are the options for storing hierarchical data in a relational database?About SQL join statementUpdate multiple rows of a table having specific value in particular column equal to value calculated from two other tablesHow NATURAL JOIN works with two tables when there are two or more common columns?How can I perform this join on a table with multiple records per key without returning multiple records per key?SQL Multiple joins with OR conditionUpdating New Created Tables From Two Tables - SQLpostgres delete from indirectly related tables






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I am adding a commission percent column to an existing query. However, the commission data sits in a table unrelated to the main table (A) used within the query. However, these two tables have common columns/values with Table B.



I have three tables, A, B and C below



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


I need to get the Commission_Percent from Table C relating to the corresponding Renewal_Code from Table A. This is the same as A_Tans_Code from Table C except that Table C spells out Renewal or New and Table A only uses R or N.



I have been able to pull through the Commission_Percent column into the output by using Table B for common values, but all values show as NULL.



I have also tried using a decode statement in order to link the Renewal_code/A_Trans_Code columns from 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


I need the correct commission_percent for Renewal and New business to come through for each reference. So far, I am only getting NULLs as I am having a hard time linking Tables A and C's Renewal_code and A_prod_code columns.



Any help is greatly appreciated!










share|improve this question
























  • In your sample code at the bottom: If you are going to join table A to C directly, you can leave out the intermediate join with table B. However, in the code you have listed, you misspelled "Renewal" in your decode statement, and that's not gonna work. So, remove all references to table B, fix the typo in the decode, and see what happens?

    – TheMouseMaster
    Mar 24 at 21:54












  • Thank you TheMouseMaster. I have attempted leaving out TableB and I am getting the ORA-01427 error. I think where my issue also lies, is that within the script that I am amending, TableA's renewal_code is decoded to display as above. So essentially, I am trying to decode values from TableC to match the decoded values from TableA. Does that make sense? What makes it more complicated is that I need it to only pull data through where Value_name is A.

    – Codash
    Mar 24 at 23:25











  • try the following SQL, and tell me what the result is. SELECT distinct c.Commission_Percent from TableC c Join TableA a ON c.A_Tans_Code = Decode(a.Renewal_Code, "R", "Renewal", "N", "New", "OTHER"). Note: I copied and pasted all table and field names from your example; if there is a typo in them, adjust as necessary.

    – TheMouseMaster
    Mar 24 at 23:38











  • Thanks! This version runs without any errors, however, still pulling through NULL values. Back to the drawing board!

    – Codash
    Mar 25 at 0:09

















2















I am adding a commission percent column to an existing query. However, the commission data sits in a table unrelated to the main table (A) used within the query. However, these two tables have common columns/values with Table B.



I have three tables, A, B and C below



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


I need to get the Commission_Percent from Table C relating to the corresponding Renewal_Code from Table A. This is the same as A_Tans_Code from Table C except that Table C spells out Renewal or New and Table A only uses R or N.



I have been able to pull through the Commission_Percent column into the output by using Table B for common values, but all values show as NULL.



I have also tried using a decode statement in order to link the Renewal_code/A_Trans_Code columns from 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


I need the correct commission_percent for Renewal and New business to come through for each reference. So far, I am only getting NULLs as I am having a hard time linking Tables A and C's Renewal_code and A_prod_code columns.



Any help is greatly appreciated!










share|improve this question
























  • In your sample code at the bottom: If you are going to join table A to C directly, you can leave out the intermediate join with table B. However, in the code you have listed, you misspelled "Renewal" in your decode statement, and that's not gonna work. So, remove all references to table B, fix the typo in the decode, and see what happens?

    – TheMouseMaster
    Mar 24 at 21:54












  • Thank you TheMouseMaster. I have attempted leaving out TableB and I am getting the ORA-01427 error. I think where my issue also lies, is that within the script that I am amending, TableA's renewal_code is decoded to display as above. So essentially, I am trying to decode values from TableC to match the decoded values from TableA. Does that make sense? What makes it more complicated is that I need it to only pull data through where Value_name is A.

    – Codash
    Mar 24 at 23:25











  • try the following SQL, and tell me what the result is. SELECT distinct c.Commission_Percent from TableC c Join TableA a ON c.A_Tans_Code = Decode(a.Renewal_Code, "R", "Renewal", "N", "New", "OTHER"). Note: I copied and pasted all table and field names from your example; if there is a typo in them, adjust as necessary.

    – TheMouseMaster
    Mar 24 at 23:38











  • Thanks! This version runs without any errors, however, still pulling through NULL values. Back to the drawing board!

    – Codash
    Mar 25 at 0:09













2












2








2








I am adding a commission percent column to an existing query. However, the commission data sits in a table unrelated to the main table (A) used within the query. However, these two tables have common columns/values with Table B.



I have three tables, A, B and C below



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


I need to get the Commission_Percent from Table C relating to the corresponding Renewal_Code from Table A. This is the same as A_Tans_Code from Table C except that Table C spells out Renewal or New and Table A only uses R or N.



I have been able to pull through the Commission_Percent column into the output by using Table B for common values, but all values show as NULL.



I have also tried using a decode statement in order to link the Renewal_code/A_Trans_Code columns from 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


I need the correct commission_percent for Renewal and New business to come through for each reference. So far, I am only getting NULLs as I am having a hard time linking Tables A and C's Renewal_code and A_prod_code columns.



Any help is greatly appreciated!










share|improve this question
















I am adding a commission percent column to an existing query. However, the commission data sits in a table unrelated to the main table (A) used within the query. However, these two tables have common columns/values with Table B.



I have three tables, A, B and C below



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


I need to get the Commission_Percent from Table C relating to the corresponding Renewal_Code from Table A. This is the same as A_Tans_Code from Table C except that Table C spells out Renewal or New and Table A only uses R or N.



I have been able to pull through the Commission_Percent column into the output by using Table B for common values, but all values show as NULL.



I have also tried using a decode statement in order to link the Renewal_code/A_Trans_Code columns from 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


I need the correct commission_percent for Renewal and New business to come through for each reference. So far, I am only getting NULLs as I am having a hard time linking Tables A and C's Renewal_code and A_prod_code columns.



Any help is greatly appreciated!







sql oracle-sqldeveloper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 21:31









C. Peck

960324




960324










asked Mar 24 at 19:12









CodashCodash

163




163












  • In your sample code at the bottom: If you are going to join table A to C directly, you can leave out the intermediate join with table B. However, in the code you have listed, you misspelled "Renewal" in your decode statement, and that's not gonna work. So, remove all references to table B, fix the typo in the decode, and see what happens?

    – TheMouseMaster
    Mar 24 at 21:54












  • Thank you TheMouseMaster. I have attempted leaving out TableB and I am getting the ORA-01427 error. I think where my issue also lies, is that within the script that I am amending, TableA's renewal_code is decoded to display as above. So essentially, I am trying to decode values from TableC to match the decoded values from TableA. Does that make sense? What makes it more complicated is that I need it to only pull data through where Value_name is A.

    – Codash
    Mar 24 at 23:25











  • try the following SQL, and tell me what the result is. SELECT distinct c.Commission_Percent from TableC c Join TableA a ON c.A_Tans_Code = Decode(a.Renewal_Code, "R", "Renewal", "N", "New", "OTHER"). Note: I copied and pasted all table and field names from your example; if there is a typo in them, adjust as necessary.

    – TheMouseMaster
    Mar 24 at 23:38











  • Thanks! This version runs without any errors, however, still pulling through NULL values. Back to the drawing board!

    – Codash
    Mar 25 at 0:09

















  • In your sample code at the bottom: If you are going to join table A to C directly, you can leave out the intermediate join with table B. However, in the code you have listed, you misspelled "Renewal" in your decode statement, and that's not gonna work. So, remove all references to table B, fix the typo in the decode, and see what happens?

    – TheMouseMaster
    Mar 24 at 21:54












  • Thank you TheMouseMaster. I have attempted leaving out TableB and I am getting the ORA-01427 error. I think where my issue also lies, is that within the script that I am amending, TableA's renewal_code is decoded to display as above. So essentially, I am trying to decode values from TableC to match the decoded values from TableA. Does that make sense? What makes it more complicated is that I need it to only pull data through where Value_name is A.

    – Codash
    Mar 24 at 23:25











  • try the following SQL, and tell me what the result is. SELECT distinct c.Commission_Percent from TableC c Join TableA a ON c.A_Tans_Code = Decode(a.Renewal_Code, "R", "Renewal", "N", "New", "OTHER"). Note: I copied and pasted all table and field names from your example; if there is a typo in them, adjust as necessary.

    – TheMouseMaster
    Mar 24 at 23:38











  • Thanks! This version runs without any errors, however, still pulling through NULL values. Back to the drawing board!

    – Codash
    Mar 25 at 0:09
















In your sample code at the bottom: If you are going to join table A to C directly, you can leave out the intermediate join with table B. However, in the code you have listed, you misspelled "Renewal" in your decode statement, and that's not gonna work. So, remove all references to table B, fix the typo in the decode, and see what happens?

– TheMouseMaster
Mar 24 at 21:54






In your sample code at the bottom: If you are going to join table A to C directly, you can leave out the intermediate join with table B. However, in the code you have listed, you misspelled "Renewal" in your decode statement, and that's not gonna work. So, remove all references to table B, fix the typo in the decode, and see what happens?

– TheMouseMaster
Mar 24 at 21:54














Thank you TheMouseMaster. I have attempted leaving out TableB and I am getting the ORA-01427 error. I think where my issue also lies, is that within the script that I am amending, TableA's renewal_code is decoded to display as above. So essentially, I am trying to decode values from TableC to match the decoded values from TableA. Does that make sense? What makes it more complicated is that I need it to only pull data through where Value_name is A.

– Codash
Mar 24 at 23:25





Thank you TheMouseMaster. I have attempted leaving out TableB and I am getting the ORA-01427 error. I think where my issue also lies, is that within the script that I am amending, TableA's renewal_code is decoded to display as above. So essentially, I am trying to decode values from TableC to match the decoded values from TableA. Does that make sense? What makes it more complicated is that I need it to only pull data through where Value_name is A.

– Codash
Mar 24 at 23:25













try the following SQL, and tell me what the result is. SELECT distinct c.Commission_Percent from TableC c Join TableA a ON c.A_Tans_Code = Decode(a.Renewal_Code, "R", "Renewal", "N", "New", "OTHER"). Note: I copied and pasted all table and field names from your example; if there is a typo in them, adjust as necessary.

– TheMouseMaster
Mar 24 at 23:38





try the following SQL, and tell me what the result is. SELECT distinct c.Commission_Percent from TableC c Join TableA a ON c.A_Tans_Code = Decode(a.Renewal_Code, "R", "Renewal", "N", "New", "OTHER"). Note: I copied and pasted all table and field names from your example; if there is a typo in them, adjust as necessary.

– TheMouseMaster
Mar 24 at 23:38













Thanks! This version runs without any errors, however, still pulling through NULL values. Back to the drawing board!

– Codash
Mar 25 at 0:09





Thanks! This version runs without any errors, however, still pulling through NULL values. Back to the drawing board!

– Codash
Mar 25 at 0:09












2 Answers
2






active

oldest

votes


















2














This creates reproducible testing by cleaning up temp tables and re-inserting the data.
And could be a model for other solutions.



The SQL 'Select...' joins to a single TableC that has two parts to the ON condition-- prod_code and renewal_code (instead of two joins). Just remove the "a.*," to use it in your sql. (the decode function was changed to use a subscript of the first char of the 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)


Results are--



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


Code to put in your 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





share|improve this answer

























  • Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

    – Codash
    Mar 24 at 23:02












  • @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

    – donPablo
    Mar 25 at 0:14












  • I see what you are saying. Thanks again for this!

    – Codash
    Mar 27 at 15:15


















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
;


The common table expression (CTE) is just to create the same values you posted (CTE is the part using the WITH construct).



Results:



 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


You may have to check the values in the rows you posted that do not result in a join. This also assumes there are no codes:code_name relationships that violate the rule of first letter of code name = code.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327506%2fhow-to-link-two-unrelated-tables-through-one-table-with-common-values%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This creates reproducible testing by cleaning up temp tables and re-inserting the data.
    And could be a model for other solutions.



    The SQL 'Select...' joins to a single TableC that has two parts to the ON condition-- prod_code and renewal_code (instead of two joins). Just remove the "a.*," to use it in your sql. (the decode function was changed to use a subscript of the first char of the 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)


    Results are--



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


    Code to put in your 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





    share|improve this answer

























    • Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

      – Codash
      Mar 24 at 23:02












    • @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

      – donPablo
      Mar 25 at 0:14












    • I see what you are saying. Thanks again for this!

      – Codash
      Mar 27 at 15:15















    2














    This creates reproducible testing by cleaning up temp tables and re-inserting the data.
    And could be a model for other solutions.



    The SQL 'Select...' joins to a single TableC that has two parts to the ON condition-- prod_code and renewal_code (instead of two joins). Just remove the "a.*," to use it in your sql. (the decode function was changed to use a subscript of the first char of the 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)


    Results are--



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


    Code to put in your 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





    share|improve this answer

























    • Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

      – Codash
      Mar 24 at 23:02












    • @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

      – donPablo
      Mar 25 at 0:14












    • I see what you are saying. Thanks again for this!

      – Codash
      Mar 27 at 15:15













    2












    2








    2







    This creates reproducible testing by cleaning up temp tables and re-inserting the data.
    And could be a model for other solutions.



    The SQL 'Select...' joins to a single TableC that has two parts to the ON condition-- prod_code and renewal_code (instead of two joins). Just remove the "a.*," to use it in your sql. (the decode function was changed to use a subscript of the first char of the 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)


    Results are--



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


    Code to put in your 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





    share|improve this answer















    This creates reproducible testing by cleaning up temp tables and re-inserting the data.
    And could be a model for other solutions.



    The SQL 'Select...' joins to a single TableC that has two parts to the ON condition-- prod_code and renewal_code (instead of two joins). Just remove the "a.*," to use it in your sql. (the decode function was changed to use a subscript of the first char of the 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)


    Results are--



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


    Code to put in your 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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 25 at 17:24

























    answered Mar 24 at 21:20









    donPablodonPablo

    1,3221815




    1,3221815












    • Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

      – Codash
      Mar 24 at 23:02












    • @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

      – donPablo
      Mar 25 at 0:14












    • I see what you are saying. Thanks again for this!

      – Codash
      Mar 27 at 15:15

















    • Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

      – Codash
      Mar 24 at 23:02












    • @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

      – donPablo
      Mar 25 at 0:14












    • I see what you are saying. Thanks again for this!

      – Codash
      Mar 27 at 15:15
















    Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

    – Codash
    Mar 24 at 23:02






    Thank you donPablo. From my understanding, the above can be used in SQL, however, I am using Oracle Sql Developer. I am new to the latter. Also, the above tables were very miniscule examples of the large dataset that I am working with just for clarity of the output I am trying to achieve. I wouldn't need to pull through reference, value_name or renewal_code. Only need to add a new column (commussion_percent) within an already existing script.

    – Codash
    Mar 24 at 23:02














    @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

    – donPablo
    Mar 25 at 0:14






    @Codash as I said above, Use the last SELECT statement. "Just remove the "a.*," to use it in your sql. " Then enclose in parens and name it Commission_Percent, just like in your sample sql. I have added that at the end.

    – donPablo
    Mar 25 at 0:14














    I see what you are saying. Thanks again for this!

    – Codash
    Mar 27 at 15:15





    I see what you are saying. Thanks again for this!

    – Codash
    Mar 27 at 15:15













    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
    ;


    The common table expression (CTE) is just to create the same values you posted (CTE is the part using the WITH construct).



    Results:



     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


    You may have to check the values in the rows you posted that do not result in a join. This also assumes there are no codes:code_name relationships that violate the rule of first letter of code name = code.






    share|improve this answer



























      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
      ;


      The common table expression (CTE) is just to create the same values you posted (CTE is the part using the WITH construct).



      Results:



       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


      You may have to check the values in the rows you posted that do not result in a join. This also assumes there are no codes:code_name relationships that violate the rule of first letter of code name = code.






      share|improve this answer

























        0












        0








        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
        ;


        The common table expression (CTE) is just to create the same values you posted (CTE is the part using the WITH construct).



        Results:



         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


        You may have to check the values in the rows you posted that do not result in a join. This also assumes there are no codes:code_name relationships that violate the rule of first letter of code name = code.






        share|improve this answer













         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
        ;


        The common table expression (CTE) is just to create the same values you posted (CTE is the part using the WITH construct).



        Results:



         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


        You may have to check the values in the rows you posted that do not result in a join. This also assumes there are no codes:code_name relationships that violate the rule of first letter of code name = code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 18:02









        dreamworkdreamwork

        512




        512



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327506%2fhow-to-link-two-unrelated-tables-through-one-table-with-common-values%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript