SELECT
*
FROM
(
SELECT
ordr.program,
ordr.order_id,
ordr.part_no,
ordr.order_no,
ordr.actual_start_date,
ser.serial_no,
oper.asgnd_machine_id,
oper.time_stamp,
oper.updt_userid,
oper.oper_status,
oper.oper_no ,
CASE
WHEN oper.oper_no IN ('1234') THEN 'paint_bike'
END AS oper_type
FROM
sfmfg.sfwid_order_desc ordr
LEFT JOIN sfmfg.sfwid_serial_desc ser
ON ordr.order_id = ser.order_id
LEFT JOIN sfmfg.sfwid_oper_desc oper
ON ordr.order_id = oper.order_id
AND oper.step_key = -1
AND ordr.program IN ('bike')
AND oper.oper_no IN ('1234')
WHERE
ordr.actual_start_date > TO_DATE('08/01/2023', 'MM/DD/YYYY')
AND ordr.part_no LIKE '123'
AND ser.serial_no LIKE '123')
我想将此枢轴添加到它的末尾。
PIVOT (
MAX(asgnd_machine_id) AS asgnd_machine_id,
MAX(time_stamp) AS time_stamp,
MAX(updt_userid) AS updt_userid,
MAX(oper_status) AS oper_status FOR oper_type IN ('bike_Cutting' AS bike_Cutting, 'bike_Prep' AS bike_Prep,
)
我试图让枢轴工作,但当我为第一个 select 语句添加这样的别名时,我的第一个 select 语句出现了列定义不明确的错误
SELECT
a1.program,
a1.order_id,
a1.customer_description,
a1.stiffener_type,
a1.mold_tool_no,
a1.part_no,
a1.order_no,
a1.actual_start_date,
a1.serial_no,
a1.asgnd_machine_id,
a1.time_stamp,
a1.updt_userid,
a1.oper_status,
a1.plan_title,
a1.oper_type
它给了我无效的标识符错误
当您
PIVOT
列时,您需要组合 FOR
子句中的别名和 PIVOT
的聚合子句:
SELECT a1.program,
a1.order_id,
-- a1.customer_description, -- These 4 columns are not in your query.
-- a1.stiffener_type,
-- a1.mold_tool_no,
--a1.plan_title,
a1.part_no,
a1.order_no,
a1.actual_start_date,
a1.serial_no,
a1.bike_cutting_asgnd_machine_id, -- combine the aliases for these columns
a1.bike_cutting_time_stamp,
a1.bike_cutting_updt_userid,
a1.bike_cutting_oper_status,
a1.bike_prep_asgnd_machine_id,
a1.bike_prep_time_stamp,
a1.bike_prep_updt_userid,
a1.bike_prep_oper_status
FROM (
SELECT ordr.program,
ordr.order_id,
ordr.part_no,
ordr.order_no,
ordr.actual_start_date,
ser.serial_no,
oper.oper_no,
-- pivot
oper.asgnd_machine_id,
oper.time_stamp,
oper.updt_userid,
oper.oper_status,
CASE
WHEN oper.oper_no IN ('1234') THEN 'paint_bike'
WHEN oper.oper_no IN ('5678') THEN 'bike_Cutting'
WHEN oper.oper_no IN ('9999') THEN 'bike_Prep'
END AS oper_type
FROM /*sfmfg.*/sfwid_order_desc ordr
LEFT JOIN /*sfmfg.*/sfwid_serial_desc ser
ON ordr.order_id = ser.order_id
LEFT JOIN /*sfmfg.*/sfwid_oper_desc oper
ON ordr.order_id = oper.order_id
AND oper.step_key = -1
AND ordr.program IN ('bike')
AND oper.oper_no IN ('1234')
WHERE ordr.actual_start_date > TO_DATE('08/01/2023', 'MM/DD/YYYY')
AND ordr.part_no LIKE '123'
AND ser.serial_no LIKE '123'
)
PIVOT (
MAX(asgnd_machine_id) AS asgnd_machine_id,
MAX(time_stamp) AS time_stamp,
MAX(updt_userid) AS updt_userid,
MAX(oper_status) AS oper_status
FOR oper_type IN (
'bike_Cutting' AS bike_Cutting,
'bike_Prep' AS bike_Prep
)
) a1