Simple SQL query getting "ORA-00918: column ambiguously defined"? - sql

I'm just trying to get the sum of money from a column.
SELECT SUM(amount_usd)
FROM WIRE_MSTR, TRANS_MSTR
INNER JOIN WIRE_MSTR ON WIRE_MSTR.trans_id = TRANS_MSTR.trans_id
WHERE WIRE_MSTR.dest_cntry = 'CANADA' AND TRANS_MSTR.trans_yyyymm = '201510';
But on line 4 I get an error "ORA-00918: column ambiguously defined."
I've referenced everything, what could be the problem?

You are using implicit and explicit join syntax. You should remove the implicit syntax:
SELECT SUM(amount_usd)
FROM TRANS_MSTR
INNER JOIN WIRE_MSTR ON WIRE_MSTR.trans_id = TRANS_MSTR.trans_id
WHERE WIRE_MSTR.dest_cntry = 'CANADA' AND TRANS_MSTR.trans_yyyymm = '201510';
The problem was, you had WIRE_MSTR twice in your FROM clause.

Try this. You got WIRE_MSTR twice.
SELECT SUM(amount_usd)
FROM TRANS_MSTR
INNER JOIN WIRE_MSTR ON WIRE_MSTR.trans_id = TRANS_MSTR.trans_id
WHERE WIRE_MSTR.dest_cntry = 'CANADA' AND TRANS_MSTR.trans_yyyymm = '201510';

Related

Sql pgadmin confused

The following query gives me a syntax error:
Select
traseu_stud.An,
traseu_stud.CodSpec
from
traseu_stud
where
NumePren = "Popescu W.T. Vasile"
and AnUniv = "2012-2013"
inner join studenti on traseu_stud.matricol = studenti.matricol
inner join persoane on studenti.idPers = persoane.idPers
ERROR: syntax error at or near "inner"
LINE 3: ...Pren="Popescu W.T. Vasile" and AnUniv="2012-2013" inner join...
^
SQL state: 42601
Character: 122
You have to use subquery if you want use filter in this way:
select * from
(
Select traseu_stud.An,traseu_stud.CodSpec,matricol
from traseu_stud
where NumePren='Popescu W.T. Vasile' and AnUniv='2012-2013'
) a
inner join studenti on a.matricol=studenti.matricol
inner join persoane on studenti.idPers=persoane.idPers
otherwise you have to use filter below way
Select traseu_stud.An,traseu_stud.CodSpec from
traseu_stud inner join
studenti on traseu_stud.matricol=studenti.matricol inner join persoane on
studenti.idPers=persoane.idPers
where NumePren='Popescu W.T. Vasile' and AnUniv='2012-2013'
The JOIN go into the from clause.
Additionally: String constants need to be enclose in single quotes, double quotes are for identifiers:
Select
traseu_stud.An,
traseu_stud.CodSpec
from traseu_stud
inner join studenti on traseu_stud.matricol = studenti.matricol
inner join persoane on studenti.idPers = persoane.idPers
where NumePren = 'Popescu W.T. Vasile'
and AnUniv = '2012-2013'
thank you for your help, but I have same errors, I put here the image with tables and what I want to do: What Specialization(Specializare) and in what Study year(AnUniv) it's the Popescu W.T. Vasile in 2012-2013.
https://i.stack.imgur.com/e1iV1.jpg

Access Missing Operator on Query

I'm stuck on trying to figure out why I'm getting a missing operator error not his query.
The Query is as follows:
SELECT DISTINCT
FLEET.regno
, SUBMODEL.submodel
, FLEET.icao
, FLEET.startyr
, CARRIERS.sector
FROM (FLEET
INNER JOIN SUBMODEL
ON FLEET.[M/S/Variant] = SUBMODEL.[M/S/Variant]
INNER JOIN LOOKUP
ON
(SUBMODEL.SUBMODEL = LOOKUP.SUBMODEL
AND FLEET.ICAO = LOOKUP.ICAO)
INNER JOIN CARRIERS
ON FLEET.icao = CARRIERS.ICAO)
WHERE (
LOOKUP.[ASM/ac] is not null
OR LOOKUP.[ATM/ac] is not null
) AND FLEET.status = 'ACTIVE';
Access 2010 is throwing the following error:
Syntax error (missing operator in query expression
'FLEET.[M/S/Variant] = SUBMODEL.[M/S/Variant] INNER JOIN LOOKUP ON
(SUBMODEL.SUBMODEL = LOOKUP.SUBMODE'.
I've tried putting parentheses in different places but still running into issues. Is there something I'm missing here.
We fixed it:
SELECT DISTINCT
FLEET.regno
, SUBMODEL.submodel
, FLEET.icao
, FLEET.startyr
, CARRIERS.sector
FROM ((FLEET
INNER JOIN SUBMODEL
ON FLEET.[M/S/Variant] = SUBMODEL.[M/S/Variant])
INNER JOIN LOOKUP
ON
SUBMODEL.SUBMODEL = LOOKUP.SUBMODEL
AND FLEET.ICAO = LOOKUP.ICAO)
INNER JOIN CARRIERS
ON FLEET.icao = CARRIERS.ICAO
WHERE (
LOOKUP.[ASM/ac] is not null
OR LOOKUP.[ATM/ac] is not null
) AND FLEET.status = 'ACTIVE';

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.

SQL Query is invalid in the select list

I dont know why this is coming up as invalid and I can not figure it out. I was given a legacy database as my supervisor left and I am in charge until someone comes to replace him. I am trying to run this query...
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH INNER JOIN
tblM ON tblCH.guidMId = tblM.guidMId INNER JOIN
ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
The error I keep getting is...Msg 8120, Level 16, State 1, Line 1
Column 'tblM.guidRId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Why is this error occuring?
You are forgetting to group guidRId. (you are aggregating the data)
SELECT
tblM.guidRId,
SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE
tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId
Because you need a GROUP BY if you are going to use an aggregate functon like SUM() [or COUNT, AVG(), etc...] with another non-aggregate column:
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId;
Try:
SELECT
tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
GROUP BY tblM.guidRId