但我想在termination_call_dtls 中连接这些列!!
正如@APC 所提到的,问题在于您试图为WITH 块设置别名。不清楚您是否需要子查询分解;你可以这样做:
select tsk.task_number || ' / '|| tsk.task_status|| ' / '|| tsk.summary AS termination_call_dtls,
CSI.creation_date AS creation_date
FROM csf_ct_task_assignments tsa,CSF_DEBRIEF_HEADERS db, csf_ct_tasks tsk ,CSI_ITEM_INSTANCES csi
where 1=1
and tsk.customer_product_id = csi.instance_id
and tsk.INCIDENT_CUSTOMER_ID = csi.OWNER_PARTY_ID
and tsa.Task_Assignment_Id=db.Task_Assignment_Id(+)
and tsk.task_id = tsa.task_id (+)
and tsk.task_type like '%Termination%'
and tsk.task_status_id<>7
and (SELECT actual_shipment_date
FROM oe_order_lines_all
WHERE line_id= csi.last_oe_order_line_id)
<= tsk.creation_date
and rownum=1
在第一行使用termination_call_dtls 作为列 别名。
正如 cmets 中也提到的,您应该考虑使用现代连接语法,而不是您现在拥有的旧的和仅适用于 Oracle 的语法;并且子查询作为另一个连接可能会更好,例如:
select tsk.task_number || ' / '|| tsk.task_status|| ' / '|| tsk.summary as termination_call_dtls,
csi.creation_date
from csf_ct_tasks tsk
join csi_item_instances csi on csi.instance_id = tsk.customer_product_id
and csi.owner_party_id = tsk.incident_customer_id
join oe_order_lines_all oola on oola.line_id = csi.last_oe_order_line_id
and ools.actual_shipment_date <= tsk.creation_date
left join csf_ct_task_assignments tsa on tsa.task_id = tsk.task_id
left join csf_debrief_headers db on db.task_assignment_id = tsa.task_assignment_id
where tsk.task_type like '%Termination%'
and tsk.task_status_id != 7
and rownum = 1
如果需要,无论哪种方式,您仍然可以将查询用作 CTE - 如果它是比您展示的更大、更复杂的查询的一部分。
不清楚为什么你有csf_ct_task_assignments 或csf_debrief_headers 的(外部)连接,因为无论如何你都不使用这些表中的任何列。这些连接可能只是被删除。
您还应该知道and rownum = 1 将返回一个不确定的行,假设您在没有它的情况下获得多行。通常你会有一个包含order by 子句的内联视图,然后应用rownum 过滤器来限制结果,例如(猜你想要最早的创建日期):
select termination_call_dtls, creation_date
from (
select tsk.task_number || ' / '|| tsk.task_status|| ' / '|| tsk.summary as termination_call_dtls,
csi.creation_date
from csf_ct_tasks tsk
join csi_item_instances csi on csi.instance_id = tsk.customer_product_id
and csi.owner_party_id = tsk.incident_customer_id
join oe_order_lines_all oola on oola.line_id = csi.last_oe_order_line_id
and ools.actual_shipment_date <= tsk.creation_date
where tsk.task_type like '%Termination%'
and tsk.task_status_id != 7
order by csi.creation_date
)
where rownum = 1
从 12c 开始,还有其他机制可以稍微简化这一点。或者使用聚合:
select tsk.task_number || ' / '|| tsk.task_status|| ' / '|| tsk.summary as termination_call_dtls,
min(csi.creation_date) as creation_date
from csf_ct_tasks tsk
join csi_item_instances csi on csi.instance_id = tsk.customer_product_id
and csi.owner_party_id = tsk.incident_customer_id
join oe_order_lines_all oola on oola.line_id = csi.last_oe_order_line_id
and ools.actual_shipment_date <= tsk.creation_date
where tsk.task_type like '%Termination%'
and tsk.task_status_id != 7
group by tsk.task_number, tsk.task_status, tsk.summary