-1

I got the following error:

Multiple attributes named business_id in reportAttributeReferenceAmbiguous; p.business_id, s.business_id

from the SPARK/HIVE query:

SELECT
    business_id,
    p.p1,
    p.p2,
    s.s1,
    s.s2
FROM p_table p
FULL OUTER JOIN s_table s
    ON p.business_id = s.business_id

Since I am using FULL OUTER JOIN, business_id could be either p.business_id or s.business_id, so I didn't specify which one to use. (I thought some rows might have only p.business_id while some other rows might have only s.business_id, do I understand this wrong?)

So I am wondering what's the proper way to select business_id in the above query? Thanks!

leftjoin
  • 36,950
  • 8
  • 57
  • 116
Edamame
  • 23,718
  • 73
  • 186
  • 320

2 Answers2

2

Use

coalesce(p.business_id, s.business_id) as business_id

Or

nvl(p.business_id, s.business_id) as business_id

Or CASE statements like in this answer: https://stackoverflow.com/a/37744071/2700344

leftjoin
  • 36,950
  • 8
  • 57
  • 116
0

you have business_id in both table in your query you select business_id and didn't mention from which table.

SELECT
    business_id,
    p.p1,
    p.p2,
    s.s1,
    s.s2
FROM p_table p
FULL OUTER JOIN s_table s
    ON p.business_id = s.business_id

above query should change to

SELECT
    p.business_id AS pBI, s.business_id AS sBI,
    p.p1,
    p.p2,
    s.s1,
    s.s2
FROM p_table p
FULL OUTER JOIN s_table s
    ON p.business_id = s.business_id

I didn't check but you can try .

ash
  • 456
  • 1
  • 3
  • 9