Column found in more than one table error - sql

I'm getting the following error
Error while compiling statement:
FAILED: SemanticException Column episodeid Found in more than one tables/subqueries [SQL State=42000, DB Errorcode=40000]
Would anyone be able to help me reconfigure?
Thanks!
This is the code in question:
select
papid, count(distinct episodeid) as epicount,
episovcountcesarean, sum(episovspendcesarean),
count(claimnumber) as claimcount,
sum(allowedamount) as spend
from
3pdcoe_tc_dm_eoc_nep_peri.star_output_episodes e
join
3pdcoe_tc_dm_eoc_nep_peri.dbo_claimpatientservice cps on cps.episodeidd = e.episodeid
join
3pdcoe_tc_dm_eoc_nep_peri.star_output_aggregate a on a.episodeid = e.episodeid
where
aggtype = 'Window' and agggroup = 'Trigger'
and eeany = 0
and year(episodeend) = '2016'
and episovcountcesarean = 1
group by
papid, episovcountcesarean, episovspendcesarean;

In your select list you must specify your target table
select
papid, count(distinct a.episodeid) as epicount,
episovcountcesarean, sum(episovspendcesarean),
count(claimnumber) as claimcount,
sum(allowedamount) as spend
OR
select
papid, count(distinct e.episodeid) as epicount,
episovcountcesarean, sum(episovspendcesarean),
count(claimnumber) as claimcount,
sum(allowedamount) as spend

Related

ORA-00918: column ambiguous defined when joining subquery table

The following code works:
SELECT b.AAZ002
FROM
AE02 b
INNER JOIN t_aa10 aa10
ON (aa10.AAA100 = 'AA5515' AND b.AA5515 = aa10.AAA102)
But when I change b to a subquery table, it reports ambigious column error:
SELECT b.AAZ002
FROM
(
select count(1) count1, a.BAZ379 , max(a.AB0111) AB0111, max(a.AA5515) AA5515, max(a.AAE011) AAE011
, max(a.AAE035) AAE035, max(a.AAE036) AAE036, max(a.AAE012) AAE012, max(a.AAE012) AAE012
, max(a.ZA0100) ZA0100
from AE02 a
where a.BAE028 = '1'
group by a.BAZ379
) b
INNER JOIN t_aa10 aa10
ON (aa10.AAA100 = 'AA5515' AND b.AA5515 = aa10.AAA102)
Dbeaver reported these 3 errors:
Description Resource Path Location Type
SQL Error [918] [42000]: ORA-00918: column ambiguously defined
Script-8.sql /General/Scripts Unknown Database Script Problem
SQL Error [933] [42000]: ORA-00933: SQL command not properly ended
Script-8.sql /General/Scripts Unknown Database Script Problem
SQL Error [979] [42000]: ORA-00979: not a GROUP BY expression
Script-8.sql /General/Scripts Unknown Database Script Problem
I have prefix on every column, it should not be ambiguous. What could be the cause?
I have corrected the sql after astentx and YoYo pointed out errors. Here is the corrected sql:
SELECT b.AAZ002
FROM
(
select count(1) count1, a.BAZ379 , max(a.AB0111) AB0111, max(a.AA5515) AA5515, max(a.AAE011) AAE011
, max(a.AAE035) AAE035, max(a.AAE036) AAE036, max(a.AAE012) AAE012, max(AAZ002) AAZ002
, max(a.ZA0100) ZA0100
from AE02 a
where a.BAE028 = '1'
group by a.BAZ379
) b
INNER JOIN t_aa10 aa10
ON (aa10.AAA100 = 'AA5515' AND b.AA5515 = aa10.AAA102)
INNER JOIN AB01 ab
ON ab.ZA0100 = b.ZA0100

Case When + IN combination

I'm a bit stuck in my coding here... I have this extense and complex code, but I'm actually failling by the end of it. I'm getting the SQL Error [42804]: ERROR: argument of CASE/WHEN must be type boolean, not type character varying
The thing, is: when "bairro" matches a row from "SUB_COUNTRY_DB", get psd.name, else get z.name. Any tips on how I could accomplish this?
select distinct
aa.mes,
--aa.modalidade,
--to_date(aa.created_date, 'yyyy-mm-dd') data_captacao,
ucl.ja_comprou_lf as comprou_lf,
case when bairro in (select sub_country from sub_country_DB)
then psd.name
else z.name
end loja,
count (distinct aa.customer_uid) qtde_socios,
count (distinct aa.assinatura_uid) qtde_assinaturas
from assinaturas_ativas aa
left join ultima_compra_loja_fisica ucl on (aa.customer_uid = ucl.customer_uid
and aa.mes = ucl.mes)
left join zip_code z on (aa.customer_uid = z.customer_uid
and aa.mes = z.mes)
left join SUB_COUNTRY_DB psd
on (psd.district = aa.bairro)
group by 1,2,3--,4
Try variants like:
moving condition to an inner query
CASE WHEN EXISTS (SELECT DISTINCT sub_country FROM sub_country_DB WHERE sub_country = barrio)
ANY (PostgreSQL only)
CASE WHEN bairro = ANY(ARRAY_AGG(select sub_country from sub_country_DB))

Hive query : Ambiguous column reference acct_nbr in stage

I getting "Ambiguous column reference"
query :
SELECT stage.acct_nbr
FROM (SELECT *
FROM mem stage
JOIN (SELECT acct_nbr,
corp_ent_cd,
sub_seq_nbr,
mem_nbr,
Max(cdc_src_last_updt_ts) AS cdc_src_last_updt_ts
FROM mem
WHERE file_nm = 'DLTV.FULL.MES3191.D180423'
GROUP BY acct_nbr,
corp_ent_cd,
sub_seq_nbr,
mem_nbr) c
ON c.corp_ent_cd = stage.corp_ent_cd
AND c.acct_nbr = stage.acct_nbr
AND c.sub_seq_nbr = stage.sub_seq_nbr
AND c.mem_nbr = stage.mem_nbr
AND stage.cdc_src_last_updt_ts = c.cdc_src_last_updt_ts
WHERE stage.file_nm = 'DLTV.FULL.MES3191.D180423') stage;
Error Message:
Error: Error while compiling statement: FAILED: SemanticException [Error 10007]: Ambiguous column reference acct_nbr in stage (state=42000,code=10007)
Whereas if I do select * instead of select stage.acct_nbr, it is executing without error.
Could somebody solve my issue?
The problem is on SELECT *.
There are two column at subquery c.acct_nbr ,stage.acct_nbr ,so outer SELECT can't stage.acct_nbr distinguish which colunm you want to get.
So you can choose c.acct_nbr or stage.acct_nbr in your select subquery
You can try this.
SELECT stage.acct_nbr
FROM (SELECT c.acct_nbr
FROM mem stage
JOIN (SELECT acct_nbr,
corp_ent_cd,
sub_seq_nbr,
mem_nbr,
Max(cdc_src_last_updt_ts) AS cdc_src_last_updt_ts
FROM mem
WHERE file_nm = 'DLTV.FULL.MES3191.D180423'
GROUP BY acct_nbr,
corp_ent_cd,
sub_seq_nbr,
mem_nbr) c
ON c.corp_ent_cd = stage.corp_ent_cd
AND c.acct_nbr = stage.acct_nbr
AND c.sub_seq_nbr = stage.sub_seq_nbr
AND c.mem_nbr = stage.mem_nbr
AND stage.cdc_src_last_updt_ts = c.cdc_src_last_updt_ts
WHERE stage.file_nm = 'DLTV.FULL.MES3191.D180423') stage;

how to use more than 1 sub query in hive

I am executing the below query getting the error.
FAILED: SemanticException [Error 10249]: Line 13:15 Unsupported SubQuery Expression 'master_cd': Only 1 SubQuery expression is supported.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM cust_focus cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
WHERE cfs.roll_no NOT IN (SELECT roll_no FROM class_hist)
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
AND (cfs.master_cd IN (SELECT msk5.msk5_master_cd from msk5_mst_tbl as msk5 WHERE cfs.master_cd=msk5.msk5_master_cd and msk5_m_code=9)
OR cfs.master_cd IS NULL)
group by cfs.roll_no;
Could you please help me how to resolve this error.
Thanks in Advance.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM(select cf.* from cust_focus cf
join class_hist ch on cf.roll_no!=ch.roll_no
join msk5_mst_tbl msk5 on cf.master_cd = msk5.msk5_master_cd where
msk5_m_code=9))cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
OR cfs.master_cd IS NULL
These many joins would impact the performance though!!
Only multiple join subqueries are supported.
below query works without any issue.
select * from (select id from test where id>10) a
join (select id from test where id>20) b on a.id=b.id;
In your case ,both filters are being used against same table(cust_focus) only otherwise you could have applied filters on different tables like above example.

Joining 3 tables in Google bigquery

The example below stops at the first JOIN with an error message
Encountered " "JOIN" "JOIN "" at line 13, column 4. Was expecting: ")"
Am I missing something obvious with multiple joins in Bigquery?
SELECT type.CourseType AS CourseType,
SUM(joined.assign.StudentCount) AS StudentN
FROM
(
SELECT assign.StateCourseCode,
assign.StateCourseName,
assign.MatchType,
assign.Term,
assign.StudentCount
FROM [Assignment.AssignmentExtract5] AS assign
JOIN SELECT wgt.Term,
wgt.Weight
FROM [Crosswalk.TermWeights] AS wgt
ON wgt.Term = assign.Term
) AS joined
JOIN SELECT type.CourseCode,
type.CourseDescription,
type.CourseType,
type.CourseCategory
FROM [Crosswalk.CourseTypeDescription] AS type
ON joined.assign.StateCourseCode = type.CourseCode
GROUP BY CourseType
Thanks Ryan, your help was much appreciated. For anyone who might be interested, here is a query that worked.
SELECT type.CourseCategory AS CourseCategory,
SUM(joined.assign.StudentCount) AS StudentN
FROM
(
SELECT assign.StateCourseCode,
assign.StateCourseName,
assign.MatchType,
assign.Term,
assign.StudentCount
FROM [Assignment.AssignmentExtract5] AS assign
JOIN (SELECT Term,
Weight
FROM [Crosswalk.TermWeights]) AS wgt
ON wgt.Term = assign.Term
) AS joined
JOIN (SELECT CourseCode,
CourseDescription,
CourseType,
CourseCategory
FROM [Crosswalk.CourseTypeDescription]) AS type
ON (joined.assign.StateCourseCode = type.CourseCode)
GROUP BY CourseCategory;
I think you're just missing a parenthesis on line 13.
This:
JOIN SELECT wgt.Term,
wgt.Weight
FROM [Crosswalk.TermWeights] AS wgt
ON wgt.Term = assign.Term
Should be:
JOIN (SELECT wgt.Term,
wgt.Weight
FROM [Crosswalk.TermWeights]) AS wgt
ON wgt.Term = assign.Term
More info:
https://developers.google.com/bigquery/docs/query-reference#multiplejoinsexample
FYI - JOINs are not as fast as we'd like yet. We're working on improving the performance.