How to LEFT OUTER JOIN on specific column values in HIVEQL? - hadoop

When I run the following HiveQL code I get the error:
Execute error: Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 1:2112 Invalid table alias or
column reference 'T3'
SELECT *
FROM CC_CLAIM_EXT T1
INNER JOIN CC_EXPOSURE_EXT T2 ON (T1.ID = T2.CLAIMID)
LEFT OUTER JOIN CC_POLICY_EXT T3 ON (T1.POLICYID = T3.ID)
LEFT OUTER JOIN CC_COVERAGE_EXT T4 ON (T2.COVERAGEID = T4.ID)
LEFT OUTER JOIN CC_TRANSACTION_EXT T5 ON (T2.ID = T5.EXPOSUREID)
LEFT OUTER JOIN CC_TRANSACTIONSET_EXT T6 ON (T5.TRANSACTIONSETID = T6.ID)
LEFT OUTER JOIN CC_TRANSACTIONLINEITEM_EXT T7 ON (T5.ID = T7.TRANSACTIONID)
LEFT OUTER JOIN CC_RISKUNIT_EXT T12 ON (T4.RISKUNITID = T12.ID)
LEFT OUTER JOIN CC_CLASSCODE_EXT T13 ON (T12.CLASSCODEID = T13.ID)
LEFT OUTER JOIN (SELECT TT12.CLAIMID
,CASE WHEN COUNT(TT13.PRIMARYBODYPART) > 1 THEN 10010 ELSE MAX(TT13.PRIMARYBODYPART) END AS PRIMARYBODYPART
,CASE WHEN COUNT(TT13.DETAILEDBODYPART) > 1 THEN 10010 ELSE MAX(TT13.DETAILEDBODYPART) END AS DETAILEDBODYPART
FROM CC_INCIDENT_EXT TT12
LEFT OUTER JOIN CC_BODYPART_EXT TT13 ON (TT12.ID = TT13.INCIDENTID)
GROUP BY TT12.CLAIMID) T14
ON (T1.ID = T14.CLAIMID AND T3.POLICYTYPE IN(10022,10023))
WHERE T1.STATE IN(2,3)
AND T2.STATE IN(2,3)
AND T6.APPROVALSTATUS = 1
AND T7.RETIRED = 0
ORDER BY CLAIMNUMBER
,EXPOSUREID
,TRANSACTIONID
I've narrowed it down to the line:
ON (T1.ID = T14.CLAIMID AND T3.POLICYTYPE IN(10022,10023))
If I delete:
AND T3.POLICYTYPE IN(10022,10023)
The code runs fine. Is there a better way to limit this join in HiveQL?

The error is because you put a reference on the left join between T1 and T14 BUT you put T3 on the condition. To limit your query to your specified T3.policy_id, you should put in along the line where you left join T1 and T3. See below in line 4:
SELECT *
FROM CC_CLAIM_EXT T1
INNER JOIN CC_EXPOSURE_EXT T2 ON (T1.ID = T2.CLAIMID)
LEFT OUTER JOIN CC_POLICY_EXT T3 ON (T1.POLICYID = T3.ID AND T3.POLICYTYPE IN(10022,10023))
LEFT OUTER JOIN CC_COVERAGE_EXT T4 ON (T2.COVERAGEID = T4.ID)
LEFT OUTER JOIN CC_TRANSACTION_EXT T5 ON (T2.ID = T5.EXPOSUREID)
LEFT OUTER JOIN CC_TRANSACTIONSET_EXT T6 ON (T5.TRANSACTIONSETID = T6.ID)
LEFT OUTER JOIN CC_TRANSACTIONLINEITEM_EXT T7 ON (T5.ID = T7.TRANSACTIONID)
LEFT OUTER JOIN CC_RISKUNIT_EXT T12 ON (T4.RISKUNITID = T12.ID)
LEFT OUTER JOIN CC_CLASSCODE_EXT T13 ON (T12.CLASSCODEID = T13.ID)
LEFT OUTER JOIN (SELECT TT12.CLAIMID
,CASE WHEN COUNT(TT13.PRIMARYBODYPART) > 1 THEN 10010 ELSE MAX(TT13.PRIMARYBODYPART) END AS PRIMARYBODYPART
,CASE WHEN COUNT(TT13.DETAILEDBODYPART) > 1 THEN 10010 ELSE MAX(TT13.DETAILEDBODYPART) END AS DETAILEDBODYPART
FROM CC_INCIDENT_EXT TT12
LEFT OUTER JOIN CC_BODYPART_EXT TT13 ON (TT12.ID = TT13.INCIDENTID)
GROUP BY TT12.CLAIMID) T14
ON T1.ID = T14.CLAIMID
WHERE T1.STATE IN(2,3)
AND T2.STATE IN(2,3)
AND T6.APPROVALSTATUS = 1
AND T7.RETIRED = 0
ORDER BY CLAIMNUMBER
,EXPOSUREID
,TRANSACTIONID

Related

What is the purpose of (+) operator in a where clause, other than outer joins, in Oracle SQL?

I have some very old Oracle SQL code I need to review, as per below and am trying to understand what the (+) operator is doing in the where clause after the first use of it
select *
from table_a a,
table b b
where
a.id = b.id (+)
and b.seq_nb (+) = 1
and b.type_cd (+) = 'DOLLR'
I thought (+) was a outer join equivalent, so
from table_a a,
table b b
where
a.id = b.id (+)
would be the same as
from table a a left outer join table b b on a.id=b.id
so how can you have outer joins to hard coded variables as below?
b.seq_nb (+) = 1
and b.type_cd (+) = 'DOLLR'
Any help would be greatly appreciated, thank you!
It's the same as:
select *
from table_a a
left outer join table_b b
on a.id = b.id
and b.type_cd = 'DOLLR'
and b.seq_nb = 1
Sometimes also referred to as a "filtered outer join".
It is equivalent to an outer join with a derived table:
select *
from table_a a
left outer join (
select *
from table_b
where b.type_cd = 'DOLLR'
and b.seq_nb = 1
) b on a.id = b.id

Getting ORA-00918: column ambiguously defined error on 10g but works in 11g

select distinct p.id,
format_name(p.fname, p.lname,'local000000000000001') full_name,
p.username,
p.person_no,
jobinfo.name,
company.name2,
company.name,
dom.name,
per_mgr.username,
a.name
from cmt_person p
inner join fgt_locale loc on loc.id='local000000000000001'
left outer join tpt_company company on p.company_id = company.id
left outer join tpt_ext_job_type jobinfo on p.jobtype_id = jobinfo.id AND jobinfo.locale_id=loc.id
inner join fgt_domain dom on p.split = dom.id
left outer join fgt_gen gen on gen.id1 = p.id and gen.TYPE = 301 and gen.str2 IS NULL
left outer join fgt_ext_admin_managed_lov a on a.id = gen.id2 and a.locale_id = loc.id
left outer join cmt_person per_mgr on per_mgr.id = p.manager_id
inner join fgt_address a on a.owner_id = p.id
where ( p.terminated_on is null or p.terminated_on >= sysdate)
and( (p.type = 100 and p.split in ('domin000000000000007','domin000000000001107','domin000000000001108','domin000000000001109','domin000000000001104','domin000000000001103','domin000000000001106','domin000000000001105','domin000000000000001','domin000000000001102','domin000000000001110'))
or (p.type = 200 and p.split in ('domin000000000000007','domin000000000001107','domin000000000001108','domin000000000001109','domin000000000001104','domin000000000001103','domin000000000001106','domin000000000001105','domin000000000000001','domin000000000001102','domin000000000001110')) )
AND 1=1
AND lower(p.status) = lower('A')
AND lower(a.country) like lower('USA' || '%')
AND p.type in (decode('1','1',100,'2',200,'0',p.type))
AND p.manager_id in ('emplo000000000034578')
ORDER BY 2,3,4,5,6,7,8,9,10
This is the query I have defined table alias for all tables
Do I need column aliases for following columns for 10g?
jobinfo.name ,
company.name2 ,
company.name ,
dom.name
Two tables share the same alias ("a"): fgt_ext_admin_managed_lov and fgt_address. Change it, as that won't work:
SQL> select a.deptno
2 from dept a, emp a;
select a.deptno
*
ERROR at line 1:
ORA-00918: column ambiguously defined
SQL>
Look at below code :-
left outer join fgt_ext_admin_managed_lov a <<<
..
..
inner join fgt_address a <<<<
You have used same alias name for both tables. Change it to any other alias name.

Doing a Left outer join in a linq query

I have the query below that works in sqllite with a left outer join
select * from customer cu
inner join contract cnt on cu.customerId = cnt.customerid
inner join address addy on cu.addressid = addy.addressId
inner join csrAssoc cassc on cu.customerid = cassc.customerId
left outer join CustomerServiceRepresentative csrr on cassc.csrid = csrr.customerservicerepresentativeId
inner join customerServiceManager csmm on cassc.csmid = csmm.customerservicemanagerId
where cu.customernumber = '22222234'
I want to be able to apply a left outer join on this line in the linq query below
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId
VisitRepData = (from cu in objCustomer.AsEnumerable()
join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId
join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId
join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId
join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId
where cu.CustomerNumber == (customernbr)
How can I do a left outer join in a linq query?
Here is my comment after adjusting and running the code. The other section is also added. All am getting is object is not set to an instance of an object.
var VisitRepData = from cu in objCustomer.AsEnumerable()
join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId
join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId
join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId into temp
from tempItem in temp.DefaultIfEmpty()
join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId
where cu.CustomerNumber == (customernbr)
select new
{
cu.customerId,
cu.CustomerNumber,
cu.customerName,
cu.dateActive,
cnt.contractExpirationDate,
addy.street,
addy.street2,
addy.city,
addy.state,
addy.zipcode,
cu.EMail,
cu.phoneNo,
cu.faxNumber,
csmm.customerServiceManagerName,
tempItem.CustomerServiceRepresentativeName,
};
foreach (var item in VisitRepData)
{
var one = item.customerId;
var two = item.CustomerNumber;
}
Per documentation:
A left outer join is a join in which each element of the first collection is returned, regardless of whether it has any correlated elements in the second collection. You can use LINQ to perform a left outer join by calling the DefaultIfEmpty method on the results of a group join
(emphasis mine)
Based on MSDN link above, and if I understood your requirements correctly, query should look like this:
VisitRepData = from cu in objCustomer.AsEnumerable()
join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId
join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId
join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId into temp
from tempItem in temp.DefaultIfEmpty()
join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId
where cu.CustomerNumber == (customernbr)
Specifically, the left outer join is performed with this code:
join csrr in objCsrCustServRep.AsEnumerable()
on cassc.CsrId equals csrr.CustomerServiceRepresentativeId
into temp
from tempItem in temp.DefaultIfEmpty()

LEFT OUTER JOIN WHEN IT HAS MULTIPLE TABLE SELECT QUERY

Currently I have joined two tables using inner join , like following
SELECT A.*,B.*
FROM A,B
WHERE A.COLUMN_A = B.COLUMN_B
now I want to join Left outer join to above results , lets say I want to join Table C
So I did like following
SELECT A.*,B.*
FROM A,B
LEFT OUTER JOIN C ON B.COLUMN_X = C.COLUMN_X
WHERE A.COLUMN_A = B.COLUMN_B
this is executing without errors in SQL navigator, But in this result I cannot see any output.
anything wrong in this query , please advise
Change it to have proper join syntax like
SELECT A.*,B.*
FROM A
INNER JOIN B ON A.COLUMN_A = B.COLUMN_B
LEFT OUTER JOIN C ON B.COLUMN_X = C.COLUMN_X;
Better change all to outer join
SELECT A.*,B.*
FROM A
LEFT JOIN B ON A.COLUMN_A = B.COLUMN_B
LEFT OUTER JOIN C ON B.COLUMN_X = C.COLUMN_X;
Use this
SELECT A.*,B.*,C.*
FROM A
INNER JOIN B
ON A.COLUMN_A = B.COLUMN_B
LEFT OUTER JOIN C
ON B.COLUMN_X = C.COLUMN_X
If you absolutely have to use legacy syntax, then use this. But I won't recommend it.
SELECT A.*,B.*,C.*
FROM A,B,C
where A.COLUMN_A = B.COLUMN_B
AND
B.COLUMN_X = C.COLUMN_X (+)

how to do left join in lambda

var item= (from t1 in _dbEntities.PurchaseSales
join t2 in _dbEntities.ItemTypes on t1.ItemTypeID equals t2.ID
where t2.ID.Equals(null)
select t2).ToList();
how to do left join in it
Use DefaultIfEmpty:
var item= (from t1 in _dbEntities.PurchaseSales
join t2 in _dbEntities.ItemTypes on t1.ItemTypeID equals t2.ID into t
from l in t.DefaultIfEmpty()
where l == null
select t1).ToList();

Resources