Getting error: "Column ambiguously defined " - sql

I am running the following query and getting column ambiguously defined :
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;

You have used aliase-R twice, that is why the error,
You dont need to include a table twice into JOINs for adding another condition(M.FLEETID = R.FLEETID),
You can give the corresponding condition in the first occurce itselt using AND operator.
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID AND M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
Hope this helps.

Related

Comparing values in two rows and getting ORA-00918: column ambiguously defined

I am trying to compare values from one column in two different rows using CASE statement. Inner SELECT statements are identical and working OK giving me fields that I need. Then I inner joined them on one of the key fields (KYCID). That's where I get "column ambiguously defined" error. I tried to remove duplicate rows by using DISTINCT - still the same error.
SELECT DISTINCT e.KYCID,
CASE WHEN e.HRAC_FLAG <> f.HRAC_FLAG THEN 'FALSE' ELSE 'TRUE' END AS FLAG_COMPARISON
FROM
(SELECT gt.task_id, gt.work_item_status, gt.work_item_type, gt.component_id, xref.HRAC_FLAG,
xref.case_nbr, xref.task_id, d.kycid, d.core_component_state
FROM kyc_gbl_main.global_task gt
inner join KYC_RGN_NAM_MAIN.CASE_HRAC_XREF xref on gt.component_id = xref.component_id
inner join kyc_rgn_nam_main.account a on xref.accountid = a.accountid
inner join kyc_rgn_nam_main.country_appx_account_xref b on a.accountid = b.accountid
inner join kyc_rgn_nam_main.country_appx c on c.cntry_appx_id = b.cntry_appx_id and
c.country_appx_state = b.country_appx_state
inner join kyc_rgn_nam_main.kyc_main d on d.kycid = c.kycid
where gt.work_item_type = 'HRAC_OVERLAY'
and gt.work_item_status = 'Completed') e
INNER JOIN
(select gt.task_id, gt.work_item_status, gt.work_item_type, gt.component_id, xref.HRAC_FLAG,
xref.case_nbr, xref.task_id, d.kycid, d.core_component_state
from kyc_gbl_main.global_task gt
inner join KYC_RGN_NAM_MAIN.CASE_HRAC_XREF xref on gt.component_id = xref.component_id
inner join kyc_rgn_nam_main.account a on xref.accountid = a.accountid
inner join kyc_rgn_nam_main.country_appx_account_xref b on a.accountid = b.accountid
inner join kyc_rgn_nam_main.country_appx c on c.cntry_appx_id = b.cntry_appx_id and
c.country_appx_state = b.country_appx_state
inner join kyc_rgn_nam_main.kyc_main d on d.kycid = c.kycid
where gt.work_item_type = 'HRAC_OVERLAY'
and gt.work_item_status = 'Completed') f
ON e.KYCID = f.KYCID
WHERE e.core_component_state ='ACTIVE'
AND f.core_component_state = 'IN_PROGRESS';
As determined with process of elimination, you reference the same named column in a SELECT query. To avoid this name collision, consider aliasing those particular columns.
Additionally, to avoid repetition, consider using a CTE via WITH and avoid using table aliases like (a, b, c) or (t1, t2, t3). Finally, move WHERE conditions to ON to filter before combining all data sources.
WITH sub AS (
SELECT gt.task_id AS gt_task_id -- RENAMED TO AVOID COLLISION
, gt.work_item_status
, gt.work_item_type
, gt.component_id
, xref.HRAC_FLAG
, xref.case_nbr
, xref.task_id AS x_ref_task_id -- RENAMED TO AVOID COLLISION
, k.kycid
, k.core_component_state
FROM kyc_gbl_main.global_task gt
INNER JOIN KYC_RGN_NAM_MAIN.CASE_HRAC_XREF xref
ON gt.component_id = xref.component_id
AND gt.work_item_type = 'HRAC_OVERLAY' -- MOVED FROM WHERE
AND gt.work_item_status = 'Completed' -- MOVED FROM WHERE
INNER JOIN kyc_rgn_nam_main.account acc
ON xref.accountid = acc.accountid
INNER JOIN kyc_rgn_nam_main.country_appx_account_xref cax
ON acc.accountid = cax.accountid
INNER JOIN kyc_rgn_nam_main.country_appx ca
ON ca.cntry_appx_id = cax.cntry_appx_id
AND ca.country_appx_state = cax.country_appx_state
INNER JOIN kyc_rgn_nam_main.kyc_main k
ON k.kycid = ca.kycid
)
SELECT DISTINCT
e.KYCID
, CASE
WHEN e.HRAC_FLAG <> f.HRAC_FLAG
THEN 'FALSE'
ELSE 'TRUE'
END AS FLAG_COMPARISON
FROM sub e
INNER JOIN sub f
ON e.KYCID = f.KYCID
AND e.core_component_state = 'ACTIVE' -- MOVED FROM WHERE
AND f.core_component_state = 'IN_PROGRESS' -- MOVED FROM WHERE

SQL Multiple inner joins with max() for latest recorded entry

Attempting to build SQL with INNER JOIN's. The INNER JOIN's work ok, now I need to add the MAX() function for limiting the rows to just most recent. Added this INNER JOIN client_diagnosis_record ON SELECT cr.PATID, cr.date_of_diagnosis, cr.most_recent_diagnosis...
Received this SQL code error, need some help, I'm sure it a simple oversight but my eyes are getting dim from looking so long...
Syntax error: [SQLCODE: <-4>:
SQLCODE: <-4>:<A term expected, beginning with one of the following: identifier, constant, aggregate, %ALPHAUP, %EXACT, %MVR, %SQLSTRING, %
[%msg: < The SELECT list of the subquery
SELECT pd.patient_name,
cr.PATID,
cr.date_of_diagnosis,
cr.EPISODE_NUMBER,
ce.diagnosing_clinician_value,
ce.data_entry_user_name,
most_recent_diagnosis
FROM client_diagnosis_record cr
INNER JOIN patient_current_demographics pd ON cr.patid = pd.patid
INNER JOIN client_diagnosis_entry ce ON ce.patid = pd.patid
AND cr.ID = ce.DiagnosisRecord
INNER JOIN client_diagnosis_record ON (SELECT cr.PATID,
cr.date_of_diagnosis,
cr.most_recent_diagnosis
FROM ( SELECT patid,
date_of_diagnosis,
MAX(ID) AS most_recent_diagnosis
FROM client_diagnosis_record) cr
INNER JOIN RADplus_users ru ON ru.staff_member_id = ce.diagnosing_clinician_code
WHERE cr.PATID <> '1'
AND ce.diagnosis_status_value ='Active'
AND (ru.user_description LIKE '%SOA' OR ru.user_description LIKE '%OA')
GROUP BY cr.PATID
I tried to re-format you query and it seems your query syntax is not correct. You may try below query -
SELECT pd.patient_name,
cr.PATID,
cr.date_of_diagnosis,
cr.EPISODE_NUMBER,
ce.diagnosing_clinician_value,
ce.data_entry_user_name,
most_recent_diagnosis
FROM client_diagnosis_record cr
INNER JOIN (SELECT patid,
date_of_diagnosis,
MAX(ID) AS most_recent_diagnosis
FROM client_diagnosis_record
GROUP BY patid,
date_of_diagnosis) cr2 ON cr.PATID = cr2.PATID
AND cr.date_of_diagnosis = cr2.date_of_diagnosis
AND cr.ID = cr2.most_recent_diagnosis
INNER JOIN patient_current_demographics pd ON cr.patid = pd.patid
INNER JOIN client_diagnosis_entry ce ON ce.patid = pd.patid
AND cr.ID = ce.DiagnosisRecord
INNER JOIN RADplus_users ru ON ru.staff_member_id = ce.diagnosing_clinician_code
WHERE cr.PATID <> '1'
AND ce.diagnosis_status_value ='Active'
AND (ru.user_description LIKE '%SOA' OR ru.user_description LIKE '%OA')
GROUP BY cr.PATID

How to group when using aggregates in the select statement

I am trying to run the following code, but receive an error because of the last field in the SELECT statement that contains both an aggregate condition and a normal condition
trunc(d.ead_date)- trunc(max(d.pickup_date_ts)) AS diff_ead_cpt_max
The Error is basically saying aggregates not allowed in GROUP BY clause. If I remove the max, then it would work but then I would not get the right results.
SELECT
w1.physical_country origin_country,
a.leg_warehouse_id lm_warehouse_id,
b.leg_warehouse_id fl_warehouse_id,
c.plane_name,
k.leg_ware,
k.last_ds,
trunc(a.ead_date) ead_date,
max(d.pickup_date_ts) max_cpt,
to_char(max(d.pickup_date_ts),'HH24:MI') max_cpt_time,
trunc(max(d.pickup_date_ts)) max_cpt_date,
trunc(d.ead_date)- trunc(max(d.pickup_date_ts)) AS diff_ead_cpt_max
from
final_leg a
inner join dest_leg b
on a.shipment_id = b.shipment_id and a.route_id = b.route_id
inner join sc_execution_eu.o_detailed_routes_v2 d
on a.shipment_id = d.shipment_id and a.route_id = d.route_id and d.leg_sequence_id = 0
left join plane_leg c
on a.shipment_id = c.shipment_id and a.route_id = c.route_id
left join warehouse_attributes w1
on a.route_warehouse_id = w1.warehouse_id
left join warehouse_attributes w2
on b.leg_warehouse_id = w2.warehouse_id
RIGHT JOIN list_legs_ds k
on a.route_warehouse_id = k.leg_ware AND a.leg_warehouse_id = k.last_ds
group by
1,2,3,4,5,6,7,11
GROUP BY the non-aggregated values and use the column names:
SELECT
w1.physical_country origin_country,
a.leg_warehouse_id lm_warehouse_id,
b.leg_warehouse_id fl_warehouse_id,
c.plane_name,
k.leg_ware,
k.last_ds,
trunc(a.ead_date) ead_date,
max(d.pickup_date_ts) max_cpt,
to_char(max(d.pickup_date_ts),'HH24:MI') max_cpt_time,
trunc(max(d.pickup_date_ts)) max_cpt_date,
trunc(d.ead_date)- trunc(max(d.pickup_date_ts)) AS diff_ead_cpt_max
from
final_leg a
inner join dest_leg b
on a.shipment_id = b.shipment_id and a.route_id = b.route_id
inner join sc_execution_eu.o_detailed_routes_v2 d
on a.shipment_id = d.shipment_id and a.route_id = d.route_id and d.leg_sequence_id = 0
left join plane_leg c
on a.shipment_id = c.shipment_id and a.route_id = c.route_id
left join warehouse_attributes w1
on a.route_warehouse_id = w1.warehouse_id
left join warehouse_attributes w2
on b.leg_warehouse_id = w2.warehouse_id
RIGHT JOIN list_legs_ds k
on a.route_warehouse_id = k.leg_ware AND a.leg_warehouse_id = k.last_ds
group by
w1.physical_country origin_country,
a.leg_warehouse_id lm_warehouse_id,
b.leg_warehouse_id fl_warehouse_id,
c.plane_name,
k.leg_ware,
k.last_ds,
trunc(a.ead_date),
trunc(d.ead_date)
You cannot remove max as you already said, since pickup_date_ts,as being a non-aggregated column, not-listed in the GROUP BY list.
Try trunc( max(trunc(d.ead_date) - d.pickup_date_ts)).
Btw, it's not suggested to use column order numbers in the GROUP BY expression, some changes in the SELECT list may have an advers impact on them, rather write columns' names explicitly.

oracle ora-00904 with symfony2 and doctrine2

I have a doctrine DQL that transforms into this SQL:
SELECT o0_.id AS ID0,
o0_.pmn AS PMN1,
o0_.neteo AS NETEO2,
sum(o1_.monto_total) AS SCLR3,
o1_.periodo AS PERIODO4,
o1_.pagado AS PAGADO5
FROM operador o0_
LEFT JOIN operador_hub o2_ ON o0_.id = o2_.operador_id
LEFT JOIN outcollect o1_ ON o0_.id = o1_.operador_id
WHERE o0_.ishub = 0
AND o0_.neteo = 1
AND o0_.incluirReportes = 1
AND o0_.pmn NOT IN ('CHLTM', 'CHLCM')
AND o0_.id NOT IN (SELECT o3_.id FROM operador o3_ LEFT JOIN operador_hub o4_ ON o3_.id = o4_.operador_id LEFT JOIN outcollect o5_ ON o3_.id = o5_.operador_id WHERE (o4_.desde <= o5_.periodo AND (o4_.hasta >= o5_.periodo OR o4_.hasta IS NULL)))
AND o0_.activo = 1
GROUP BY o0_.pmn, PERIODO4
ORDER BY o0_.pmn ASC
this gives an ORA-00904: "PERIODO4": identificador no vĂ¡lido
the error is directly related to the GROUP BY o0_.pmn, PERIODO4 line
It seems related to doctrine that cannot translate the query to an ORACLE syntax. I'm using OCI8 driver.
Any thoughts?
---- edit ----
here is the DQL:
SELECT partial operador.{id, pmn, neteo},
sum(outcollect.montoTotal) as montoOut,
outcollect.periodo as periodo,
outcollect.pagado as pagado
FROM RoamingOperadoresBundle:Operador operador
LEFT JOIN operador.operadorHub operadorHub
LEFT JOIN operador.outcollect outcollect
WHERE operador.ishub = 0
AND operador.neteo = 1
AND operador.incluirReportes = 1
AND operador.pmn not in('CHLTM', 'CHLCM')
AND operador.id not in(SELECT op.id
FROM RoamingOperadoresBundle:Operador op
LEFT JOIN op.operadorHub opHub
LEFT JOIN op.outcollect out
WHERE (opHub.desde <= out.periodo and (opHub.hasta >= out.periodo or opHub.hasta is null)))
AND operador.activo = 1
GROUP BY operador.pmn, periodo ORDER BY operador.pmn
I'm not sure how you are generating this table expression, but you are getting the error because you aren't including all non-aggregated columns in the GROUP BY clause.
Also note - you can't group by an alias. You must refer to the column by specifying it in table.column form.
It should look like this:
GROUP BY o0_.pmn, o0_.neteo, o1_.periodo, o1_.pagado

Left Join on Same table - Not recognizing nested SELECT statement

I am trying to pull two different values based on different criteria from the same table and in my Left Join of the same table it is not recognizing the SELECT statement.
The error is as follows:
Dynamic SQL Error
SQL error code = -104
Token unknown - line 7, char -1
SELECT.
The SQL Statement:
SELECT
b.dept,b.typ,c.brand,c.style,c.ext,c.description,
max(c.price),max(c.last_cost),sum(c.quan) "TOTAL INV",D.QUAN "WEB INV"
FROM
invt c
left outer join (
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
FROM invt WHERE store in ('997')
group by dept,typ,brand,style,ext,description) d
on (b.store = d.store and b.style = d.style and b.brand = d.brand)
LEFT OUTER JOIN
sku b
on c.style = b.style and c.brand = b.brand
where c.quan <> 0 or c.ord <> 0
GROUP BY
b.dept,b.typ,c.brand,c.style,c.ext,c.description
Try changing this line:
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
to this:
SELECT store,dept,typ,brand,style,ext,description,sum(quan) as quan
You do not need the d alias here.
UPDATE:
As #Jeremy Holovacs mentioned, you also seem to be using d.store for your join but it does not exist in your subquery.