问题:即使在 a.Month 中找不到 Month_Name,也需要查询返回 b.Monthly_Yield 的 MONTH_NAME 和 YIELD。由于某种原因,如果在 a.Month 中找不到 Month,即使我添加到选择 b.month,查询仍然不会返回 b.Monthly_Yield 值。我现在的方式是,如果在表“a”中找不到月份,但在表“b”中找到月份,则结果将不会返回。即使在“a”中找不到那个月份,我也需要返回“b”的结果。
注:“月”为月数
左外连接的数据结果:
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
右外连接的数据结果:
Month Month_Name Yield_1 Yield_0
------------------------------------
NULL NULL 11.44 NULL
2 Febrero 11.44 14
12 Diciembre 11.44 -1.18
询问:
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 )