Can't identify reason for "column ambiguously defined" error [duplicate] - sql

This question already has answers here:
ORA-00918: column ambiguously defined in SELECT *
(4 answers)
Closed 8 years ago.
Here is the SQL:
SELECT alloc.oa_id
FROM qdod.qtran_owner_allocation alloc
INNER JOIN
(SELECT h.oa_id, h.div_ord_no, h.process_queue_id, h.from_ba_no,
h.from_ba_suf, h.from_interest_type_cd, h.from_interest_type_cd, h.from_div_ord_grp,
h.transfer_percent, h2.original_net_amount, h2.new_net_amount
FROM qdod.qtran_fund_transfer_hist h
INNER JOIN
(SELECT DISTINCT h0.oa_id, h0.original_net_amount, h1.new_net_amount
FROM qdod.qtran_fund_transfer_hist h0
INNER JOIN
(SELECT h4.oa_id, SUM (h4.new_net_amount) AS new_net_amount
FROM qdod.qtran_fund_transfer_hist h4
GROUP BY h4.oa_id) h1
ON h0.oa_id = h1.oa_id
WHERE h0.original_net_amount <> h1.new_net_amount AND h0.oa_id >= 100000000) h2
ON h.oa_id = h2.oa_id) h3
ON alloc.oa_id = h3.oa_id;
Every column has it's table defined. The main inner join (the one after the alloc table) runs fine when ran by itself. Any ideas why this is not working? This is being executed against an Oracle 10.2.0.4 database (I have also tried it against an 11.2.0.1 database thinking if it was an Oracle bug it would be resolved in 11.2, but it failed there as well).

Field duplicated in the statement, might have something to do with it
h.from_interest_type_cd, h.from_interest_type_cd,

You seem to be selecting a lot of columns you don't really need as you're not using them anywhere. The query could probably be simplified to:
SELECT alloc.oa_id
FROM qdod.qtran_owner_allocation alloc
INNER JOIN
(SELECT h.oa_id
FROM qdod.qtran_fund_transfer_hist h
INNER JOIN
(SELECT DISTINCT h0.oa_id
FROM qdod.qtran_fund_transfer_hist h0
INNER JOIN
(SELECT h4.oa_id, SUM (h4.new_net_amount) AS new_net_amount
FROM qdod.qtran_fund_transfer_hist h4
GROUP BY h4.oa_id) h1
ON h0.oa_id = h1.oa_id
WHERE h0.original_net_amount <> h1.new_net_amount AND h0.oa_id >= 100000000) h2
ON h.oa_id = h2.oa_id) h3
ON alloc.oa_id = h3.oa_id;

Related

How to fix SQL query to Left Join a subquery with Where clause?

I'm new to SQL and I'm not certain why I am getting this error. I am trying to left join a sub-query to another query in sql developer.
This is the first query,
SELECT DISTINCT
tl.species,
ag.age
FROM
age_list ag,
tree_list tl
WHERE
ag.tree_id = tl.tree_id
And then the sub-query I would like to left join where the tree_id = tree_number is,
SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE
sl.tree_spon < 10
When trying to do this I've tried to use,
SELECT DISTINCT
tl.species,
ag.age,
q1.tree_spon
FROM
age_list ag,
tree_list tl
LEFT OUTER JOIN (SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE sl.tree_spon < 10) q1 on q1.tree_number = tree_list.tree_id
WHERE
ag.tree_id = tl.tree_id
Whatever I change in terms of the alias' for the columns and tables I always get the error, "ORA-00904: invalid identifier error", and that "tree_list.tree_id is invalid identifier", though separately the queries run fine.
Can anyone help, is it an issue with both queries joining on the tl.tree_id?
You can use the ANSI join syntax throughout (rather than mixing in legacy comma joins), joining on ag.tree_id = sl.tree_number (or tl.tree_id = sl.tree_number but they're both equal given the previous join) and putting the filter on sl.tree_spon < 10 into the ON clause as well:
SELECT DISTINCT
tl.species,
ag.age,
sl.tree_spon,
sl.tree_number
FROM age_list ag
INNER JOIN tree_list tl
ON (ag.tree_id = tl.tree_id)
LEFT OUTER JOIN spon_list sl
ON (ag.tree_id = sl.tree_number AND sl.tree_spon < 10)
Change tree_list.tree_id to tl.tree_id

ORA-00907: missing right parenthesis error [duplicate]

This question already has answers here:
SQL Oracle LEFT JOIN and SUBQUERY error: ORA-00905: missing keyword
(2 answers)
Closed 5 years ago.
select buses.bus_no,
buses.bus_name
from buses
join
(
select sc1.bus_no
from schedule as sc1
join schedule as sc2 on sc1.source = sc2.destination
and sc1.destination = sc2.source
) as s1 on buses.bus_no = s1.bus_no;
Oracle does not support as for table aliases. So, you can write this as:
select b.bus_no, b.bus_name
from buses b join
(select sc1.bus_no
from schedule sc1 join
schedule sc2
on sc1.source = sc2.destination and sc1.destination = sc2.source
) s1
on b.bus_no = s1.bus_no;

how save a table with help of inner join [duplicate]

This question already has answers here:
How do I create a table based on another table [duplicate]
(2 answers)
Closed 6 years ago.
everyone! I would like to save the table after I used INNER JOIN.
CREATE TABLE result AS (
SELECT measures.Day,
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure)
But I have an error "Invalid syntax next to ( ". Why?
Thank you in advance :)
You need to use the INTO clause, see example below:
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure

Can Derby handle scalar subqueries in SELECT clause?

I'm having trouble getting my query working. Could someone cast an experienced eye on it please? The table structure is simple (2 one-to-many relationships). The query is trying to work out for each sign, how many contributions there are at each unique "PositionLocation".
Sign <- Signifier (f_key sign_oid) <- Contribution (f_key signifier_oid)
I'm getting the following error:
Error: An ON clause associated with a JOIN operator is not valid.
SQLState: 42972
ErrorCode: -1
My query is:
select s.NAME, c.POSITIONLOCATION, count(*) as num_per_locn,
(
select count(*) from APP.CONTRIBUTION c2
inner join APP.SIGNIFIER si2 on si2.OID = c2.SIGNIFIER_OID
inner join APP.SIGN s2 on s2.OID = si2.SIGN_OID
and s2.OID = s.OID
) as num_per_sign
from APP.CONTRIBUTION c
inner join APP.SIGNIFIER si on si.OID = c.SIGNIFIER_OID
inner join APP.SIGN s on s.OID = si.SIGN_OID
group by s.NAME, c.POSITIONLOCATION

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.