如果我在查询下面运行查询:
SELECT day.M_CMP_TYPO AS M_CMP_TYPO,
day.M_SPTCV as M_SPTCV,
day.M_CNT_VS2 AS M_CNT_VS2,
day.M_CNT_ORG AS M_CNT_ORG,
day.M_PL_CGR2 AS M_PL_CGR2,
day.M_PL_CGU2 AS M_PL_CGU2,
day.M_PL_CSFI2 AS M_PL_CSFI2,
day.M_PL_FTFI2 AS M_PL_FTFI2,
day.M_PL_RVR2 AS M_PL_RVR2,
day.M_PL_RVU2 AS M_PL_RVU2,
day.M_TRN_FMLY AS M_TRN_FMLY,
day.M_TRN_GRP AS M_TRN_GRP,
day.M_TRN_TYPE AS M_TRN_TYPE,
day.M_CNT_VS2 AS M_CNT_VS2,
day.M_C_CUR_PL AS M_C_CUR_PL,
day.M_INSTRLABEL AS M_INSTRLABEL,
day.M_NB AS M_NB,
day.M_PL_INSCUR AS M_PL_INSCUR,
day.M_TP_CNTRPLB AS M_TP_CNTRPLB,
day.M_TP_PFOLIO AS M_TP_PFOLIO,
day.M_ECO_PL AS M_ECO_PL,
day.M_CNT_ID AS M_CNT_ID,
day.M_ECO_PL_USD AS M_ECO_PL_USD,
day.M_POS_CURR2 AS M_POS_CURR2,
day.M_CURR2 AS M_CURR2,
day.M_TP_QTYEQ AS M_TP_QTYEQ,
day.M_TP_UQTYEQ AS M_TP_UQTYEQ,
day.M_TP_LQTY32 AS M_TP_LQTY32,
day.M_TP_UQTY AS M_TP_UQTY,
--day.M_ECO_PL - daily.M_ECO_PL AS DAILY_VAR,
--day.M_ECO_PL - month.M_ECO_PL AS MTD,
day.M_ECO_PL - year.M_ECO_PL AS YTD,
--day.M_SPTCV * (day.M_ECO_PL - daily.M_ECO_PL) as DAILY_VAR_USD,
--day.M_SPTCV * (day.M_ECO_PL - month.M_ECO_PL) as MTD_USD,
day.M_SPTCV * (day.M_ECO_PL - year.M_ECO_PL) as YTD_USD
FROM RT_PLVAR_REP day
LEFT JOIN RT_PLVAR_REP year ON day.M_NB = year.M_NB
--LEFT JOIN RT_PLVAR_REP month ON day.M_NB = month.M_NB
--LEFT JOIN RT_PLVAR_REP daily ON day.M_NB = daily.M_NB
WHERE day.M_REF_DATA = 18
AND year.M_REF_DATA = 20
--AND month.M_REF_DATA = 0
--AND daily.M_REF_DATA = 0
它返回预期的行为,这意味着它返回27行,知道年份.M_REF_DATA = 20存在于DB中。否则,如果我运行这个,我没有价值观。 由于表中不存在M_REF_DATA = 0,我期望此查询返回27行,但相关列应返回null,但情况并非如此。
我试图用AND替换Where,但它也没有用。它返回了1728行,这是一个错误的答案,它应该返回11.我的问题是为什么左连接不能正常工作?
SELECT day.M_CMP_TYPO AS M_CMP_TYPO,
day.M_SPTCV as M_SPTCV,
day.M_CNT_VS2 AS M_CNT_VS2,
day.M_CNT_ORG AS M_CNT_ORG,
day.M_PL_CGR2 AS M_PL_CGR2,
day.M_PL_CGU2 AS M_PL_CGU2,
day.M_PL_CSFI2 AS M_PL_CSFI2,
day.M_PL_FTFI2 AS M_PL_FTFI2,
day.M_PL_RVR2 AS M_PL_RVR2,
day.M_PL_RVU2 AS M_PL_RVU2,
day.M_TRN_FMLY AS M_TRN_FMLY,
day.M_TRN_GRP AS M_TRN_GRP,
day.M_TRN_TYPE AS M_TRN_TYPE,
day.M_CNT_VS2 AS M_CNT_VS2,
day.M_C_CUR_PL AS M_C_CUR_PL,
day.M_INSTRLABEL AS M_INSTRLABEL,
day.M_NB AS M_NB,
day.M_PL_INSCUR AS M_PL_INSCUR,
day.M_TP_CNTRPLB AS M_TP_CNTRPLB,
day.M_TP_PFOLIO AS M_TP_PFOLIO,
day.M_ECO_PL AS M_ECO_PL,
day.M_CNT_ID AS M_CNT_ID,
day.M_ECO_PL_USD AS M_ECO_PL_USD,
day.M_POS_CURR2 AS M_POS_CURR2,
day.M_CURR2 AS M_CURR2,
day.M_TP_QTYEQ AS M_TP_QTYEQ,
day.M_TP_UQTYEQ AS M_TP_UQTYEQ,
day.M_TP_LQTY32 AS M_TP_LQTY32,
day.M_TP_UQTY AS M_TP_UQTY,
day.M_ECO_PL - daily.M_ECO_PL AS DAILY_VAR,
day.M_ECO_PL - month.M_ECO_PL AS MTD,
day.M_ECO_PL - year.M_ECO_PL AS YTD,
day.M_SPTCV * (day.M_ECO_PL - daily.M_ECO_PL) as DAILY_VAR_USD,
day.M_SPTCV * (day.M_ECO_PL - month.M_ECO_PL) as MTD_USD,
day.M_SPTCV * (day.M_ECO_PL - year.M_ECO_PL) as YTD_USD
FROM RT_PLVAR_REP day
LEFT JOIN RT_PLVAR_REP year ON day.M_NB = year.M_NB
LEFT JOIN RT_PLVAR_REP month ON day.M_NB = month.M_NB
LEFT JOIN RT_PLVAR_REP daily ON day.M_NB = daily.M_NB
WHERE day.M_REF_DATA = 18
AND year.M_REF_DATA = 0
AND month.M_REF_DATA = 0
AND daily.M_REF_DATA = 0
答案 0 :(得分:5)
这是第一个查询中的from
和where
子句:
FROM RT_PLVAR_REP day LEFT JOIN
RT_PLVAR_REP year
ON day.M_NB = year.M_NB
WHERE day.M_REF_DATA = 18 AND year.M_REF_DATA = 20
where
子句将LEFT JOIN
变为INNER JOIN
。 year.M_REF_DATA
的值为NULL,因此它不符合条件。
对于LEFT JOIN
,条件应该放在ON
子句中:
FROM RT_PLVAR_REP day LEFT JOIN
RT_PLVAR_REP year
ON day.M_NB = year.M_NB AND year.M_REF_DATA = 20
WHERE day.M_REF_DATA = 18
第一个表格中的条件应保留在WHERE
。
答案 1 :(得分:0)
对于外部联接,您需要将筛选条件移动到联接:
FROM RT_PLVAR_REP day
LEFT JOIN RT_PLVAR_REP year
ON day.M_NB = year.M_NB
AND year.M_REF_DATA = 0
LEFT JOIN RT_PLVAR_REP month
ON day.M_NB = month.M_NB
AND month.M_REF_DATA = 0
LEFT JOIN RT_PLVAR_REP daily
ON day.M_NB = daily.M_NB
AND daily.M_REF_DATA = 0
WHERE day.M_REF_DATA = 18
这告诉数据库您只想在存在行时强制执行该条件。