我不确定如何在不引起混淆的情况下提出这个问题,但是让我尝试用我的代码的简化版本进行演示(因为我的代码太长了,很可能在这一点上使问题变得混乱)。
我有一个WITH
定义了几个CTE的语句,在上一个CTE中,我有一个CASE
采用较早CTE值的语句,但是由于某种原因,它引发了错误:
The multi-part identifier "RESULT1.total_revenue" could not be bound.
我的代码大致如下所示:
; with RESULT1 as (
some code here which returns the columns: client_id, employee_name, total_revenue
),
RESULT2 as (
some code here which uses the client_id and employee_name from RESULT1 to get employee_team_names from another table employee_teams via a join
),
RESULT3 as (
some code here which then uses RESULT2 to then get the client_names from another table for each of the client_id found above
),
RESULT4 as (
now the problem here. I want to then take the client_names found above and do a join in another table to find clean_client_names
however, if RESULT1.total_revenue is 0, then we can just put 'Not Needed' into the clean_client_name field
my attempted code below which is throwing the error shown above
select RESULT3.*,
(case when RESULT1.total_revenue = 0 then 'Not Needed' else clean_names_lookup_table.clean_client_name end) as clean_client_name
from RESULT3
left join clean_names_lookup_table
on RESULT3.client_name = clean_names_lookup_table.client_name
)
你需要JOIN
你的RESULT1
表中RESULT4
查询,否则RESULT1
是未知的。大概RESULT3
还有一个字段client_id
,在这种情况下,您将编写:
select RESULT3.*,
(case when RESULT1.total_revenue = 0 then 'Not Needed' else clean_names_lookup_table.clean_client_name end) as clean_client_name
from RESULT3
join RESULT1 on RESULT1.client_id = RESULT3.client_id
left join clean_names_lookup_table
on RESULT3.client_name = clean_names_lookup_table.client_name
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句