Skip to main content
Lennarts tip
Source Link

2 tables are given

Table T_PRICE

P_Price    P_Type    P_Route
15         red       long
10                   long
5                    small

Table T_ROUTE

R_Type     R_Route
blue       long
red        long
black      small

I need to display all Routes with prices. When there is no price for type, price should be from table T_PRICE without type.

My approach:

Select * from T_ROUTE
left outer join T_PRICE on P_Route = T_RouteR_Route 
and case when P_Type <> T_TypeR_Type then '' else R_Type end = T_TypeP_Type

Wrong Result:

R_Type R_Route  P_Price P_Type P_Route
blue   long     NULL    NULL   NULL
red    long     15      red    long
black  small    NULL    NULL   NULL

Right result should be:

R_Type R_Route  P_Price P_Type P_Route
blue   long     10             long
red    long     15      red    long
black  small    5              small

How do I get the conditional join to work? Thank you!..

2 tables are given

Table T_PRICE

P_Price    P_Type    P_Route
15         red       long
10                   long
5                    small

Table T_ROUTE

R_Type     R_Route
blue       long
red        long
black      small

I need to display all Routes with prices. When there is no price for type, price should be from table T_PRICE without type.

My approach:

Select * from T_ROUTE
left outer join T_PRICE on P_Route = T_Route 
and case when P_Type <> T_Type then '' else R_Type end = T_Type

Wrong Result:

R_Type R_Route  P_Price P_Type P_Route
blue   long     NULL    NULL   NULL
red    long     15      red    long
black  small    NULL    NULL   NULL

Right result should be:

R_Type R_Route  P_Price P_Type P_Route
blue   long     10             long
red    long     15      red    long
black  small    5              small

How do I get the conditional join to work? Thank you!..

2 tables are given

Table T_PRICE

P_Price    P_Type    P_Route
15         red       long
10                   long
5                    small

Table T_ROUTE

R_Type     R_Route
blue       long
red        long
black      small

I need to display all Routes with prices. When there is no price for type, price should be from table T_PRICE without type.

My approach:

Select * from T_ROUTE
left outer join T_PRICE on P_Route = R_Route 
and case when P_Type <> R_Type then '' else R_Type end = P_Type

Wrong Result:

R_Type R_Route  P_Price P_Type P_Route
blue   long     NULL    NULL   NULL
red    long     15      red    long
black  small    NULL    NULL   NULL

Right result should be:

R_Type R_Route  P_Price P_Type P_Route
blue   long     10             long
red    long     15      red    long
black  small    5              small

How do I get the conditional join to work? Thank you!..

Source Link

Conditional Join in DB2

2 tables are given

Table T_PRICE

P_Price    P_Type    P_Route
15         red       long
10                   long
5                    small

Table T_ROUTE

R_Type     R_Route
blue       long
red        long
black      small

I need to display all Routes with prices. When there is no price for type, price should be from table T_PRICE without type.

My approach:

Select * from T_ROUTE
left outer join T_PRICE on P_Route = T_Route 
and case when P_Type <> T_Type then '' else R_Type end = T_Type

Wrong Result:

R_Type R_Route  P_Price P_Type P_Route
blue   long     NULL    NULL   NULL
red    long     15      red    long
black  small    NULL    NULL   NULL

Right result should be:

R_Type R_Route  P_Price P_Type P_Route
blue   long     10             long
red    long     15      red    long
black  small    5              small

How do I get the conditional join to work? Thank you!..