I have a logic problem that is as follows:
A: Main Key
B: Foreign key
C: Foreign keyColumn A = [A, B]
Column B = [B, C]
I need that when something from column A is equal in Column B, that relation returns me C. Thus, by making the link A -> C.
My code is below:
SELECT DISTINCT
incident_sla.dv_inc_number AS Number,
incident_sla.dv_inc_assignment_group AS Assignment_Group,
cmn_location.dv_u_jnj_unique_id AS Unique_ID,
cmn_location.dv_u_site AS Site,
cmn_location.dv_u_region AS CI_Region,
cmn_location.dv_u_country AS CI_Country,
cmn_location.dv_street AS Street,
incident_sla.dv_inc_opened_at AS Opened,
incident_sla.dv_inc_u_state AS State,
incident_sla.dv_inc_priority AS Priority,
incident_sla.dv_inc_assigned_to AS Assigned_to,
incident_sla.dv_inc_u_resolved_at AS Resolved,
--incident_sla.dv_taskslatable_stage AS Stage,
incident_sla.dv_inc_cmdb_ci AS Configuration_item,
netgear.device_type AS Device,
RELATION.dv_parent AS PARENT,
RELATION.dv_child AS PORT,
--circuit.name AS CIRCUIT,
--circuit.dv_u_ixc_provider_a AS Carrier,
--circuit.dv_operational_status,
incident_sla.dv_inc_location AS Caller_Region,
incident_sla.inc_contact_type AS Ticket_Source,
incident_sla.dv_inc_short_description AS Short_Description,
incident_sla.dv_inc_close_notes AS Resolution_Notes,
incident_sla.dv_inc_u_resolution_bucket,
incident_sla.dv_inc_u_resolution_category
FROM servicenow_stg.incident_sla incident_sla
LEFT JOIN servicenow_stg.cmdb_ci_netgear netgear ON netgear.sys_id = incident_sla.inc_cmdb_ci
LEFT JOIN servicenow_stg.cmdb_rel_ci RELATION ON RELATION.parent = netgear.sys_id
LEFT JOIN servicenow_stg.cmdb_rel_ci RELATION2 ON RELATION2.dv_child = RELATION.dv_parent
**--WHEN RELATION2.dv_parent = RELATION.dv_child RETURN(RELATION2.dv_child) -- ERRO NESSA FUNÇÃO**
circuit.dv_u_location_a_site_id = netgear.dv_location
LEFT JOIN servicenow_stg.cmn_location cmn_location ON cmn_location.sys_id = netgear.'location'
WHERE
cmn_location.dv_u_region IN ('LA', 'LATAM')
AND incident_sla.dv_inc_opened_at LIKE '2018%'
AND incident_sla.inc_contact_type = 'Event Management'
-- AND circuit.dv_subcategory = 'Data'
And netgear.device_type = 'router'
AND
(
incident_sla.dv_inc_assignment_group like 'CONTACT CENTER APPS TIER%'
OR incident_sla.dv_inc_assignment_group = 'IP ADDRESS MGT L1'
OR incident_sla.dv_inc_assignment_group like 'NETWORK%TIER%'
OR incident_sla.dv_inc_assignment_group like 'WIRELESS TIER%'
)