PROBLEM: Need the query to return the MONTH_NAME and YIELD for b.Monthly_Yield even when the Month_Name is NOT found in a.Month. For some reason, if the Month is not found in a.Month, even when I add to the selection b.month, the query still will not return the b.Monthly_Yield value. The way I have it now, if the MONTH is NOT found in table “a” but the month is found in table “b” the result will NOT return. I need the result to return for “b” even if that month is not found in “a”.
Note: “Month” is the Month Number
DATA RESULTS WITH LEFT OUTER JOIN:
Month Month_Name Yield_1 Yield_0
------------------------------------
2 Febrero 11.44 14
3 Marzo NULL 3.21
4 Abril NULL 14.24
7 Julio NULL 10.36
8 Agosto NULL 0
9 Septiembre NULL -9.6
10 Octubre NULL 10.35
11 Noviembre NULL 1.4
12 Diciembre 11.44 -1.18
DATA RESULTS WITH RIGHT OUTER JOIN:
Month Month_Name Yield_1 Yield_0
------------------------------------
NULL NULL 11.44 NULL
2 Febrero 11.44 14
12 Diciembre 11.44 -1.18
Query:
SET @ID_CARTERA = 8;
select
a.Month,
a.Month_Name,
a.Monthly_Yield,
b.Monthly_Yield
from
( select
RIGHT(A.F_ANOMES, 2) Month,
IF(RIGHT(A.F_ANOMES, 2)=01,'Enero',
IF(RIGHT(A.F_ANOMES, 2)=02,'Febrero',
IF(RIGHT(A.F_ANOMES, 2)=03,'Marzo',
IF(RIGHT(A.F_ANOMES, 2)=04,'Abril',
IF(RIGHT(A.F_ANOMES, 2)=05,'Mayo',
IF(RIGHT(A.F_ANOMES, 2)=06,'Junio',
IF(RIGHT(A.F_ANOMES, 2)=07,'Julio',
IF(RIGHT(A.F_ANOMES, 2)=08,'Agosto',
IF(RIGHT(A.F_ANOMES, 2)=09,'Septiembre',
IF(RIGHT(A.F_ANOMES, 2)=10,'Octubre',
IF(RIGHT(A.F_ANOMES, 2)=11,'Noviembre',
IF(RIGHT(A.F_ANOMES, 2)=12,'Diciembre',
'')
))))))))))) Month_Name,
ROUND(A.POR_RENTABILIDAD, 2) Monthly_Yield
from dr_rent_carteras_meses A
where A.ID_CARTERA = @ID_CARTERA
And A.IND_RENTABILIDAD = 1
And LEFT(A.F_ANOMES, 4) = ( select MAX(left(F_ANOMES, 4 ) ) - 0
from dr_rent_carteras_meses
where ID_CARTERA = @ID_CARTERA ) ) a
left outer join
( select
RIGHT(A.F_ANOMES, 2) Month,
IF(RIGHT(A.F_ANOMES, 2)=01,'Enero',
IF(RIGHT(A.F_ANOMES, 2)=02,'Febrero',
IF(RIGHT(A.F_ANOMES, 2)=03,'Marzo',
IF(RIGHT(A.F_ANOMES, 2)=04,'Abril',
IF(RIGHT(A.F_ANOMES, 2)=05,'Mayo',
IF(RIGHT(A.F_ANOMES, 2)=06,'Junio',
IF(RIGHT(A.F_ANOMES, 2)=07,'Julio',
IF(RIGHT(A.F_ANOMES, 2)=08,'Agosto',
IF(RIGHT(A.F_ANOMES, 2)=09,'Septiembre',
IF(RIGHT(A.F_ANOMES, 2)=10,'Octubre',
IF(RIGHT(A.F_ANOMES, 2)=11,'Noviembre',
IF(RIGHT(A.F_ANOMES, 2)=12,'Diciembre',
'')
))))))))))) Month_Name,
ROUND(A.POR_RENTABILIDAD, 2) Monthly_Yield
from dr_rent_carteras_meses A
where A.ID_CARTERA = @ID_CARTERA
And A.IND_RENTABILIDAD = 1
And LEFT(A.F_ANOMES, 4) = ( select MAX(left(F_ANOMES, 4 ) ) - 1
from dr_rent_carteras_meses
where ID_CARTERA = @ID_CARTERA ) ) b on ( a.Month = b.Month )
Change LEFT OUTER JOIN to RIGHT OUTER JOIN.
@ypercube is correct. Use FULL OUTER JOIN.
The reason why you are not getting the month name is that there is now value in the a table. So you need to get it from the b table.
Try
Also, please take the advice of the comments. Use the correct data type functions. Also look into CASE statements.
You also might want to query all the info in the join and then do the data conversion (month name) on the outside query. that way you don’t have to do it twice.