ORA-00904 on join - sql

I am trying to create the following view:
CREATE OR REPLACE VIEW AlbumDistribution AS
SELECT Album.Album_ID,
Album.title,
HasTrack.tracked,
FinishedTrack.released_title,
SUBSTR(Album.Album_ID, -1) is_distributed_as
FROM Album A
JOIN HasTrack HT
ON HT.Album_ID = A.Album_ID
JOIN FinishedTrack FT
ON HasTrack.OriginatesFrom = FT.OriginatesFrom
AND HasTrack.tracked = FT.version;
but I get the ORA-00904 error:
ERROR at line 6:
ORA-00904: "HASTRACK"."TRACKED": invalid identifier
Which I find very confusing, as I reference HasTrack.tracked before and there's no error. If I change the order of the statements, putting HasTrack.OriginatesFrom = FT.OriginatesFrom last then I get the same error but for HasTrack.OriginatesFrom.

You define an alias for this and other tables. You need to use the alias throughout the query:
CREATE OR REPLACE VIEW AlbumDistribution AS
SELECT A.Album_ID, A.title, HT.tracked,
FT.released_title, SUBSTR(A.Album_ID, -1) is_distributed_as
FROM Album A JOIN
HasTrack HT
ON HT.Album_ID = A.Album_ID JOIN
FinishedTrack FT
ON HT.OriginatesFrom = FT.OriginatesFrom AND
HT.tracked = FT.version;

Related

Oracle database column ambiguously defined / invalid identifier

I've tried to write a SQL statement to select from some tables. But when I run it, I get an error, and I don't know how to fix it.
When I just select from vertrag and join pgrdat, abrkreis, mandant and komm_dat, everything works fine.
However, when I try to add literal into the joins, I get the following error:
ORA-00904: "VERTRAG"."MAN" invalid identifier
on (left join literal on literal.lit_kzl = pgrdat.beruftit and literal.man = vertrag.man)
or
ORA-00918: column ambiguously defined
on (left join Literal on Literal.LIT_KZL=pgrdat.BERUFTIT and Literal.man=man)
literal.man and vertrag.man both exist.
Here's my SQL:
SELECT man,
ak,
pnr,
vertrag.vertnr,
vertrag.eintrt2,
vertrag.ma_ab,
vertrag.verbegin,
vertrag.ver_ab,
vertrag.ver_bis,
vertrag.verende,
vertrag.enlogru,
pgrdat.spr,
pgrdat.anrede,
pgrdat.auswnr,
pgrdat.beruftit,
pgrdat.daschudat,
pgrdat.fax,
pgrdat.gebdat,
pgrdat.gebname,
pgrdat.gebort,
pgrdat.geschl,
pgrdat.lnd,
pgrdat.miname,
pgrdat.namevor,
pgrdat.namezus,
pgrdat.naname,
pgrdat.ort,
pgrdat.plz,
pgrdat.plzfach,
pgrdat.postfach,
pgrdat.pst_ab,
pgrdat.pst_bis,
pgrdat.staat,
pgrdat.staat2,
pgrdat.strasse,
pgrdat.telgesch,
pgrdat.telprivat,
pgrdat.titel,
pgrdat.vorname,
pgrdat.empfaenger,
pgrdat.taetint,
pgrdat.zimmer,
pgrdat.sachbegrp,
pgrdat.logasach,
pgrdat.stellung,
pgrdat.logasach2,
pgrdat.sachbegrp2,
abrkreis.ak_bez,
abrkreis.ak_kurz,
abrkreis.ak_ort,
abrkreis.ak_plz,
abrkreis.ak_fax,
abrkreis.ak_plzfach,
abrkreis.ak_postfach,
abrkreis.ak_strasse,
abrkreis.ak_telefon,
abrkreis.ak_text,
mandant.man_bez,
mandant.man_fax,
mandant.man_firma,
mandant.man_kurz,
mandant.man_ort,
mandant.man_plzfach,
mandant.man_plzstr,
mandant.man_postfach,
mandant.man_st_nr,
mandant.man_strasse,
mandant.man_telefon,
komm_dat.km_art,
komm_dat.km_ab,
komm_dat.km_bis,
komm_dat.km_bem,
literal.lit_txt
FROM vertrag
JOIN pgrdat
USING (man, ak, pnr)
JOIN abrkreis
USING (man, ak)
JOIN mandant
USING (man)
LEFT JOIN komm_dat
USING (man, ak, pnr)
LEFT JOIN literal
ON literal.lit_kzl = pgrdat.beruftit
AND literal.man = man
WHERE superman IN ('41900', '41901', '41902', '41903')
AND literal.lit_art = 'BERUFTIT'
AND ver_ab <= trunc(SYSDATE)
AND pgrdat.pst_ab <= trunc(SYSDATE)
AND ((ver_bis >= trunc(SYSDATE) AND pgrdat.pst_bis >= trunc(SYSDATE)) OR (ver_bis IS NULL AND pgrdat.pst_bis IS NULL));
The problem is with mixing using and on join syntax. There is a third variant you didn't try:
left join Literal on Literal.LIT_KZL=pgrdat.BERUFTIT and Literal.man=komm_dat.man
which gets
ORA-25154: column part of USING clause cannot have qualifier
You can't use the unqualified man because it's ambiguous (appearing in several tables); you can't use a qualifier because it's been used in earlier using() clauses.
If it was an inner join you could change that Literal join to using (man) and move the lit_kzl check to the where clause, but as it's an outer join that won't work (or at least, would force it back to being an inner join).
You probably need to change all the other using clauses to on, unfortunately:
...
from vertrag
join pgrdat on pgrdat.man = vertrag.man and pgrdat.ak = vertrag.ak and pgrdat.pnr = vertrag.pnr
join abrkreis on abrkreis.man = vertrag.man and abrkreis.ak = vertrag.ak
join mandant on mandant.man = vertrag.man
left join komm_dat on komm_dat.man = vertrag.man and komm_dat.ak = vertrag.ak and komm_dat.pnr = vertrag.pnr
left join Literal on Literal.LIT_KZL=pgrdat.BERUFTIT and Literal.man=vertrag.man
and Literal.LIT_ART='BERUFTIT'
where ...
I've moved and Literal.LIT_ART='BERUFTIT' from the where clause into the join condition, as that would also have forced that outer join to become an inner join again.

Sql correct syntax for raw query in laravel

I have tested this query in mysql and it works.
it doesn't work in mine controller. Can anyone correct it please?
$albi = DB::table(DB::raw("(SELECT
albi.id,
albi.CoverAlbo,
albi.Nazione,
albi.Editore,
albi.NomeCollanaUfficiale,
albi.AnnoPubblicazione
FROM albi AS a LEFT JOIN albi_user AS au ON a.id = au.albi_id
AND au.user_id = $user_id
WHERE au.albi_id IS NULL)"));
this is the error:
SQLSTATE[42000]: Syntax error or access violation: 1248 Every derived table must have its own alias
please help me!!!
This is because you're using DB::table() and then placing a raw query inside it (creating a sub query) and then not giving it an alias.
To get your above code to work you should just need to change the last line to something like:
WHERE au.albi_id IS NULL) as t1")); //notice the "t1" alias
Or you could DB::select():
$albi = DB::select("SELECT
a.id,
a.CoverAlbo,
a.Nazione,
a.Editore,
a.NomeCollanaUfficiale,
a.AnnoPubblicazione
FROM albi AS a LEFT JOIN albi_user AS au ON a.id = au.albi_id
AND au.user_id = $user_id
WHERE au.albi_id IS NULL");
Please note this is not the same select() as DB::table()->select()
Or you could just use the the Query Builder:
$albi = DB::table('albi')
->select('id', 'CoverAlbo', 'Nazione', 'Editore', 'NomeCollanaUfficiale', 'AnnoPubblicazione')
->leftJoin('albi_user', 'albi.id', 'albi_user.alibi_id')
->where('albi_user.user_id', $user_id)
->whereNull('albi_user.albi_id')
->get();
Hope this helps!
You should use alias instead of table name. So try this:
$albi = DB::table(DB::raw("(SELECT
a.id,
a.CoverAlbo,
a.Nazione,
a.Editore,
a.NomeCollanaUfficiale,
a.AnnoPubblicazione
FROM albi AS a LEFT JOIN albi_user AS au ON a.id = au.albi_id
AND au.user_id = $user_id
WHERE au.albi_id IS NULL)"));

Invalid Column after Renaming

I have a nested SQL statement. In the inner SQL statement I rename a column like this: b."MANDT" b_MANDT. In the outer statement I try something similar: a."b_MANDT" a_b_MANDT.
But I'm getting the error message that this is an invalid column name, why?
SAP DBTech JDBC: [260]: invalid column name: A.b_MANDT: line 1 col 43 (at pos 42)
Original SQL Statement:
SELECT a."MANDT", a."VBELN", a."POSNR", a."b_MANDT" a_b_MANDT, a."b_VBELN" a_b_VBELN, a."VPOSN" a_VPOSN, b."MANDT" b_MANDT, b."VBELN" b_VBELN, b."VPOSN"
FROM (
SELECT a."MANDT", a."VBELN", a."POSNR", b."MANDT" b_MANDT, b."VBELN" b_VBELN, b."VPOSN"
FROM "SAP_ECC".VBAP a
LEFT JOIN "SAP_ECC".VEDA b ON a.MANDT = b.MANDT AND a.VBELN = b.VBELN AND a.POSNR = b.VPOSN
) a
LEFT JOIN "SAP_ECC".VEDA b ON a.MANDT = b.MANDT AND a.VBELN = b.VBELN AND a.VPOSN = b.VPOSN
Try changing b."MANDT" b_MANDT to b."MANDT" "b_MANDT".
Not sure what DB you are using, but typically, unless quoted, the alias defaults to upper-case yet you are trying to reference it as mixed-case.

Why am I getting an "ambiguous column name" error in SQLite?

I can't quite figure out why I'm getting the error. The call worked fine until I added the second where clause.
ambiguous column name: nanoProd.name:
SELECT
nanoProd.name AS prodName,
nanoProd.intro AS prodIntro,
nanoProd.prodText AS nanoText,
nanoFiles.fileLoc AS nanoFile
FROM nanoProd
LEFT JOIN nanoRelFiles on nanoFiles.rid = nanoRelFiles.file_id
LEFT JOIN nanoProd on nanoProd.rid = nanoRelFiles.item_id
WHERE nanoRelFiles.scr_type = 'prod' AND nanoRelFiles.fileUse = 'list'

ERROR at line 1: ORA-00933: SQL command not properly ended

i'm working on a request ,and i think i missed a clause because i having this error
ERROR at line 1:
ORA-00933: SQL command not properly ended
the request :
echo "update account_balances_t set credit_limit='51200' inner join account_t on account_t.poid_id0=account_balances_t.obj_id0 where access_code1 in (SELECT DISTINCT ACCESS_CODE1,REC_ID FROM ACCOUNT_T A, ACCOUNT_PRODUCTS_T AP WHERE A.STATUS != 10103 AND A.ACCESS_CODE1 IS NOT NULL AND A.POID_ID0 = AP.OBJ_ID0 AND AP.PRODUCT_OBJ_ID0 = (SELECT POID_ID0 FROM PRODUCT_T WHERE NAME = 'IEW - Europe Daily Plan 1')); "|sqlplus -s `whoami`/`whoami`#$ORACLE_SID
the error :
SELECT DISTINCT ACCESS_CODE1,REC_ID FROM ACCOUNT_T A, ACCOUNT_PRODUCTS_T AP WHERE A.STATUS != 10103 AND A.ACCESS_CODE1 IS NOT NULL AND A.POID_ID0 = AP.OBJ_ID0 AND AP.PRODUCT_OBJ_ID0 = (SELECT POID_ID0 FROM PRODUCT_T WHERE NAME = 'IEW - Europe Daily Plan 1'))
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
You basically have this:
UPDATE account_balances_t
SET credit_limit='51200'
INNER JOIN account_t ON account_t.poid_id0=account_balances_t.obj_id0
WHERE access_code1 IN (...);
I can't find anything similar among the available syntax variations for UPDATE.
Also, you're attempting to match column access_code1 with a subquery that returns two columns.
UPDATE PEOPLE a
SET a.SURNAME = (
select b.SURNAME
from PEOPLE b
where b.NI.NO = a.NI_NUMBER
)
update account__balances_t set credit_limit='51200' inner join account_t on .....
Syntax is bad - oracle doesn't support 'inner join' here. Correct syntax is:
UPDATE table SET colum=expression [,column = expression ... ]
WHERE condition<br>
Read here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm