Hi, In teradata, I am getting target row updated by multiple source rows error in this…
- User
Hi, In teradata, I am getting target row updated by multiple source rows error in this query, please modify it using qualify or inner join so that I do not encounter error. Update TE1 from TRAFFIC_EVENTS TE1 ,TRAFFIC_EVENTS TE2,LOCATION L1, LOCATION L2 set INTRA_MAIL_FLG=1 Where TE1.Src_piece_id=TE2.Src_piece_id and TE1.Source_system='PFWW' and TE2.Source_system='PFWW'
and TE1.Event_Actual_dttm > TE2.Event_Actual_dttm and TE1.Event_Actual_dt between date-5 and date and TE2.Event_Actual_dt between date-10 and date-5 AND TE1.Event_Location_Id=L1.LOCATION_ID AND TE2.Event_Location_Id=L2.LOCATION_ID AND L1.LOCATION_SUB_TYPE_ID='1000051' AND L2.LOCATION_SUB_TYPE_ID='1000051' AND TE1.Event_Location_Id<>TE2.Event_Location_Id ; - Assistant
To avoid the "target row updated by multiple source rows" error in Teradata, you can modify your query using the
QUALIFYclause to ensure that each row in the target table (TE1) is only updated by a single source row (TE2). This is typically done by using a window function to rank or identify the desired row we want to keep for the update.Here’s a modified version of your query using to achieve that:
- User
Please do for this query also Update TE1 from TRAFFIC_EVENTS TE1 ,TRAFFIC_EVENTS TE2,LOCATION L1, LOCATION L2 set INTRA_MAIL_FLG=2 Where TE1.Src_piece_id=TE2.Src_piece_id and TE1.Source_system='PFWW' and TE2.Source_system='PFWW'
and TE1.Event_Actual_dttm > TE2.Event_Actual_dttm and TE1.Event_Actual_dt between date-5 and date and TE2.Event_Actual_dt between date-10 and date-5 AND TE1.Event_Location_Id=L1.LOCATION_ID AND TE2.Event_Location_Id=L2.LOCATION_ID AND L1.LOCATION_SUB_TYPE_ID='1000051' AND L2.LOCATION_SUB_TYPE_ID not in ('1000050', '1000011','1000086','1000036','1000013') AND TE2.INTRA_MAIL_FLG<>1 NEED TO REVIEW ; - Assistant
To modify the given update query in Teradata and avoid the "target row updated by multiple source rows" error, we can employ a similar approach with the
ROW_NUMBER()window function, ensuring that each row in the target table () is updated by only one matching row from the source table (). - User
I am getting illegal use of alias name in the query you have provided UPDATE TRAFFIC_EVENTS TE1 FROM ( SELECT TE2.*, ROW_NUMBER() OVER (PARTITION BY TE2.Src_piece_id ORDER BY TE2.Event_Actual_dttm DESC) AS rn FROM TRAFFIC_EVENTS TE2 JOIN LOCATION L2 ON TE2.Event_Location_Id = L2.LOCATION_ID WHERE TE2.Source_system = 'PFWW' AND TE2.Event_Actual_dt BETWEEN DATE - 10 AND DATE - 5 AND L2.LOCATION_SUB_TYPE_ID = '1000051' ) TE2 JOIN LOCATION L1 ON TE1.Event_Location_Id = L1.LOCATION_ID SET INTRA_MAIL_FLG = 1 WHERE TE1.Src_piece_id = TE2.Src_piece_id AND TE1.Source_system = 'PFWW' AND TE1.Event_Actual_dttm > TE2.Event_Actual_dttm AND TE1.Event_Actual_dt BETWEEN DATE - 5 AND DATE AND TE1.Event_Location_Id <> TE2.Event_Location_Id AND L1.LOCATION_SUB_TYPE_ID = '1000051' AND TE2.rn = 1; -- Ensure only the row with the highest Event_Actual_dttm is updated
- Assistant
The error "illegal use of alias name" in Teradata occurs because, in an
UPDATEquery, you cannot directly reference an alias from theFROMclause within theWHEREclause. To address this, we'll have to use an alternative approach, such as performing the by using a and moving the subquery logic directly into the condition.