1

I have four different tables:

TABLE_A
   field_1
   field_2
   field_3

TABLE_B
   field_4
   field_5
   field_6

TABLE_C
   field_7
   field_8

TABLE_D
   field_9
   field_10
   field_11

I'm joining three of them something like this:

SELECT a."field_1"
     , a."field_2"
     , a."field_3"
     , c."field_7"
     , c."field_8"
FROM 
           TABLE_A a
INNER JOIN TABLE_B b
        ON b."field_4" = a."field_1"
LEFT OUTER JOIN TABLE_C c
        ON c."field_7" = b."field_5"
WHERE 
     a."field_1" IN (list of values)

I want to add a conditional JOIN to TABLE_D in order to get the values of "field_9" and "field_11".

If b."field_6" is not empty then there should be a LEFT OUTER JOIN with TABLE_D on d."field_9" = b."field_6".

Else there should be a LEFT OUTER JOIN to an UNIQUE entry in TABLE_D where d."field_10" = b."field_4" and d."field_11" = 1.

I'm not sure if this could be achieved with a CASE... any ideas out there?

Thanks

1 Answer 1

2

The correct term for what you are looking for is "conditional join". In SAP HANA the term "dynamic join" is used very differently.

I edited your question accordingly and added some formatting as well.

What's missing from your question is a minimal example to work against. This would include CREATE TABLE statements for the tables and INSERT commands for the data.

Without those, it's a lot more work for everyone to work through your requirements and see if the solution is working correctly.

This time I put in time and effort myself to provide a solution that (hopefully) does what you want. For further questions, please make it easier for the folks who like to help you and do that yourself.

select current_timestamp, * from m_database;
/*
CURRENT_TIMESTAMP       SYSTEM_ID   DATABASE_NAME   HOST    START_TIME              VERSION                 USAGE      
2019-03-21 01:05:19.827 HXE         HXE             hxehost 2019-03-21 00:18:01.659 2.00.035.00.1545187853  DEVELOPMENT
*/

create column table tab_a (f1 integer, f2 integer, f3 integer);
create column table tab_b (f4 integer, f5 integer, f6 integer);
create column table tab_c (f7 integer, f8 integer);
create column table tab_d (f9 integer, f10 integer, f11 integer);

-- A
insert into tab_a values (10, 12, 13);
insert into tab_a values (20, 22, 23);
insert into tab_a values (30, 22, 23);
insert into tab_a values (50, 52, 53);

select * from tab_a;
/*
F1  F2  F3
10  12  13
20  22  23
30  22  23
50  52  53
*/

-- B
insert into tab_b values (10, 120, 130);
insert into tab_b values (20, 220, 230);
insert into tab_b values (30, 230, 230);

insert into tab_b values (50, 500, NULL); // <- join case with f6 IS NULL

select * from tab_b;
/*
F4  F5  F6 
10  120 130
20  220 230
30  230 230
50  500 ?  
*/

-- C
insert into tab_c values (120, 1200);
insert into tab_c values (220, 2200);
insert into tab_c values (230, 2200);

select * from tab_c;

/*
F7  F8  
120 1200
220 2200
230 2200
*/

-- D
insert into tab_d values (3001, 50, 1);
insert into tab_d values (20, 2200, 999 );
insert into tab_d values (30, 2200, 999 );

insert into tab_d values (230, 2200, 999 );
insert into tab_d values (130, 1200, 999 );

select * from tab_d;
/*
F9      F10     F11
3001    50      1  
20      2200    999
30      2200    999
230     2200    999
130     1200    999
*/ 


-- orig
SELECT a.f1
     , a.f2
     , a.f3
     , c.f7
     , c.f8
FROM 
           tab_a a
INNER JOIN tab_b b
        ON b.f4 = a.f1
LEFT OUTER JOIN tab_c c
        ON c.f7 = b.f5
WHERE 
     a.f1 IN (10, 20, 30, 50);

/*
F1  F2  F3  F7  F8  
10  12  13  120 1200
20  22  23  220 2200
30  22  23  230 2200
50  52  53  ?   ?   
*/

Up to here the code just reproduces your example and the SQL you posted. Note, how the "special" case with "f1" = 50 stands out with the NULLs in F7 and F8.

Now, while SAP HANA supports a feature called "case"-join (which can be used to conditionally join to different tables), the straight forward approach for your requirement is to perform two OUTER JOIN.

This works well as your condition results in two mutually exclusive cases: either TABLE_B."f6" IS NULL or not.

To map the two sets of resulting columns into a single set in the projection we can use either COALESCE or IFNULL functions to get the correct set of columns.

-- new
SELECT a.f1
     , a.f2
     , a.f3
     , c.f7
     , c.f8   
     , coalesce(d1.f9, d2.f9)  f9_cond
     , coalesce(d1.f11, d2.f11) f11_cond 

FROM 
           tab_a a
INNER JOIN tab_b b
        ON b.f4 = a.f1

LEFT OUTER JOIN tab_c c
        ON c.f7 = b.f5

LEFT OUTER JOIN tab_d d1
        ON   b.f6 IS NOT NULL 
        AND  b.f6 = d1.f9

LEFT OUTER JOIN tab_d d2
        ON   b.f6 IS NULL 
        AND  d2.f11 = 1
        AND  b.f4 = d2.f10

WHERE 
     a.f1 IN (10, 20, 30, 50);  

/*
F1  F2  F3  F7  F8      F9_COND F11_COND
10  12  13  120 1200    130     999     
20  22  23  220 2200    230     999     
30  22  23  230 2200    230     999     
50  52  53  ?   ?       3001    1       
*/

Note that this is standard SQL and not related to how SAP HANA works. A quick search with the correct term "conditional join" would have delivered plenty of results for this.

3
  • Hi Lars. wow! thanks a lot for the formatting of the question (in the benefit of anyone who's landing here), the clarification of rules and how things work over here and specially the answer. I'm just a rookie starting on the field so your comments encourage me to keep on learning and doing things right :) Thanks a lot! Mar 21, 2019 at 7:54
  • Lars... I have an additional related question. Can the same solution be achieved using a CASE in the select? let's say I don't use the two left outer join to tab_d but I have a CASE for f9_cond and another for f11_cond where I check the IS/ISNOT NULL condition for b.f6 and then select the corresponding value from tab_d? if it works, what would have a better performance? Thanks again Mar 21, 2019 at 8:41
  • You can use CASE in the projection list instead ofCOALESCE/IFNULL, sure. For the join, however, there’s no way to avoid to do two joins; it’s a matter of how you write it (you could also use two separate statements and UNION the result set). About performance: you will have to actually measure both variants as it also depends on your data. Personally, I would prefer a solution that is easier to understand.
    – Lars Br.
    Mar 24, 2019 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.