连接条件列sql

fhity93d  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(309)

我试图在sql中对已创建的列(y\u prcs\u new)执行左联接,但出现下一个错误:“无法解析列'y\u prcs\u new'”
以下是查询:

SELECT 
sh.i_rqst,
sh.y_rqst,
sh.c_close_reason,
CASE WHEN sh.y_prcs IS NULL THEN 3
     WHEN sh.y_prcs = 1 AND  sh.c_close_reason = 5 THEN 3
     WHEN (sh.y_prcs IS NOT NULL OR sh.y_prcs != 1) AND sh.c_close_reason != 5  THEN sh.y_prcs
END AS y_prcs_new,
fr.t_close_reason

FROM st_hist sh

LEFT JOIN
        (SELECT DISTINCT fr.c_close_reason, fr.t_close_reason, fr.y_prcs
        FROM sRsn fr) AS fr
        ON sh.c_close_reason = fr.c_close_reason 
        AND y_prcs_new = fr.y_prcs

你知道怎么修吗?

im9ewurl

im9ewurl1#

您的查询不正确,因为 join 语句先执行,然后执行 select 语句在查询的末尾,所以在join查询中找不到 y_prcs_new .

select   
  i_rqst,
  y_rqst,
  c_close_reason,
  y_prcs_new,  
  t_close_reason
from
(
  SELECT 
    i_rqst,
    y_rqst,
    c_close_reason,
    CASE WHEN y_prcs IS NULL THEN 3
         WHEN y_prcs = 1 AND  c_close_reason = 5 THEN 3
         WHEN (y_prcs IS NOT NULL OR y_prcs != 1) AND c_close_reason != 5  THEN y_prcs
    END AS y_prcs_new

  FROM st_hist
) sh
LEFT JOIN
( SELECT 
    DISTINCT c_close_reason, 
    t_close_reason, 
    y_prcs
  FROM sRsn
) fr
ON sh.c_close_reason = fr.c_close_reason 
AND sh.y_prcs_new = fr.y_prcs

相关问题