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.
Related
In myView in the database, I have:
SELECT orderhdr3_.id AS col_0_0_,
gnrlorg2_.id AS col_1_0_,
gnrlorg2_.name AS col_2_0_,
gnrlcustom1_.id AS col_3_0_,
gnrlcustom1_.name AS col_4_0_,
orderhdr3_.order_no AS col_5_0_,
orderhdr3_.cust_ref_no AS col_6_0_,
tripinfo4_.job_no AS col_7_0_,
orderhdr3_.order_type AS col_8_0_,
servicetyp5_.code AS col_9_0_,
tripinfo4_.service_provider_id AS col_10_0_,
tripinfo4_.service_personnel AS col_11_0_,
orderhdr3_.status AS col_12_0_,
orderhdr3_.completed_date AS col_13_0_,
orderhdr3_.reason_code AS col_14_0_,
orderhdr3_.remarks AS col_15_0_,
orderhdr3_.source_order_hdr_location_id AS col_16_0_,
orderhdrlo7_.unit_no AS col_17_0_,
orderhdrlo7_.name AS col_18_0_,
orderhdrlo7_.address AS col_19_0_,
orderhdrlo7_.postal_code AS col_20_0_,
orderhdrlo7_.zone AS col_21_0_,
orderhdrlo7_.contact AS col_22_0_,
orderhdrlo7_.phone AS col_23_0_,
orderhdrlo7_.email AS col_24_0_,
orderhdrlo7_.op_date AS col_25_0_,
orderhdrlo7_.instruction AS col_26_0_,
orderhdr3_.dest_order_hdr_location_id AS col_27_0_,
orderhdrlo6_.unit_no AS col_28_0_,
orderhdrlo6_.name AS col_29_0_,
orderhdrlo6_.address AS col_30_0_,
orderhdrlo6_.postal_code AS col_31_0_,
orderhdrlo6_.zone AS col_32_0_,
orderhdrlo6_.contact AS col_33_0_,
orderhdrlo6_.phone AS col_34_0_,
orderhdrlo6_.email AS col_35_0_,
orderhdrlo6_.op_date AS col_36_0_,
orderhdrlo6_.instruction AS col_37_0_,
orderdet11_.product_code AS col_39_0_,
orderdet11_.product_desc AS col_40_0_,
orderdet11_.order_qty AS col_41_0_,
orderdet11_.serviced_qty AS col_42_0_,
orderdet11_.reason_code AS col_43_0_,
orderhdr3_.active_ind AS col_44_0_,
tripinfo4_.active_ind AS col_45_0_,
gnrlorg2_.active_ind AS col_46_0_,
gnrlcustom1_.active_ind AS col_47_0_,
commusersc0_.comm_user_id AS col_48_0_,
orderhdr3_.org_id AS col_49_0_,
orderhdr3_.customer_id AS col_50_0_,
orderhdrlo6_.op_date AS col_51_0_,
orderhdrlo6_.op_date AS col_52_0_,
orderhdr3_.id AS col_53_0_,
orderdet11_.order_hdr_id AS col_54_0_,
orderhdrlo6_.op_date AS col_55_0_,
tripinfo4_.job_no AS col_56_0_,
orderhdr3_.seq AS col_57_0_,
orderhdr3_.order_no AS col_58_0_
FROM comm_users_customers commusersc0_
JOIN gnrl_customer gnrlcustom1_ ON commusersc0_.gnrl_customer_id = gnrlcustom1_.id
JOIN gnrl_org gnrlorg2_ ON gnrlcustom1_.gnrl_org_id = gnrlorg2_.id
CROSS JOIN order_det orderdet11_
JOIN order_hdr orderhdr3_ ON gnrlcustom1_.id = orderhdr3_.customer_id
JOIN trip_info tripinfo4_ ON orderhdr3_.trip_info_id = tripinfo4_.id
JOIN service_type servicetyp5_ ON orderhdr3_.service_type_id = servicetyp5_.id
JOIN order_hdr_location orderhdrlo6_ ON orderhdr3_.dest_order_hdr_location_id = orderhdrlo6_.id
JOIN order_hdr_location orderhdrlo7_ ON orderhdr3_.source_order_hdr_location_id = orderhdrlo7_.id
JOIN order_hdr_location_time_windows timewindow8_ ON timewindow8_.order_hdr_location_id = orderhdr3_.dest_order_hdr_location_id
JOIN order_hdr_location orderhdrlo9_ ON timewindow8_.order_hdr_location_id = orderhdrlo9_.id;
In my code, I would like to filter using WHERE
SELECT * FROM myView view WHERE view.order_no = ?
but Java/PostgreSQL give error:
ERROR: column view.order_no does not exist
Please help as this is quite complicated as it involves multiple tables and a lot of columns. I am not sure why view.order_no does not exist because it is selected in col_5_0
You can reference the view by its name:
SELECT * FROM myView WHERE id = ?
Also view is a keyword in sql.
You have renamed the column order_no as col_5_0_. So you need to use that:
SELECT *
FROM myView v
WHERE v.col_5_0_ = ?;
That is, you can only reference the columns that are returned by the view.
Renaming columns to meaningless strings seems like a really bad idea -- at least if the views are exposed to human beings. It is not clear to me why anyone would want to obfuscate them.
Also, don't use view as a table alias (or any other identifier). It is a SQL keyword, and it is best to avoid those as identifiers.
Create your view like this way and use view reference in where clause
CREATE VIEW view_name
AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
You can also filter rows during view creation.
SELECT *
FROM view_name
WHERE view_name.col_name = ?
I am new to oracle sql, this code would have worked in tsql, but does not seem to work in oracle sql. I get a
ORA-00918: column ambiguously defined
. I cant spot where the issue is, can you help ?
the problem seem to be in the join, because when i replace the join with a simple where clause, the error goes away
WITH cte1 AS
(select t1.CONTAINER_NUMBER as t1_CONTAINER_NUMBER,
t1.LAST_UPDATE_TIME as t1_LAST_UPDATE_TIME,
t1.TRANSACTION_TIME as t1_TRANSACTION_TIME,
t1.EVENT,
t1.LINE_OPERATOR,
t1.BOOKING_NUMBER,
t1.ISO_SIZE_TYPE,
t1.GROSS_WEIGHT,
t1.CATEGORY,
t1.STATUS,
t1.POS_MODE,
t1.ARR_MODE,
t1.ARR_CARRIER,
t1.ARR_VOYAGE,
t1.DEP_CARRIER,
t1.DEP_MODE,
t1.DEP_CARRIER,
t1.LOAD_PORT,
t1.DESTINATION_PORT,
t1.TRUCKER_LICENSE,
t1.FROM_RAILCAR_NAME,
t1.TO_RAILCAR_NAME,
t1.YARDARRIVAL_TIME,
t1.YARDDEPARTURE_TIME,
t1.CUSTOMER_REFERENCE,
t1.CONTAINER_LENGTH,
t1.CONTAINER_WIDTH,
t1.CONTAINER_HEIGHT,
t1.TERMINAL_ARRIVAL_TIME,
t1.TERMINAL_DEPARTURE_TIME
from CONTAINER_HISTORY_MVW t1
where t1.CONTAINER_NUMBER = 'BEAU2105501'
and t1.EVENT is not null),
cte2 AS
(select t2.CONTAINER_NUMBER as t2_CONTAINER_NUMBER,
t2.LAST_UPDATE_TIME as t2_LAST_UPDATE_TIME,
t2.TRANSACTION_TIME as t2_TRANSACTION_TIME,
t2.CREATION_TIME,
t2.EQUIPMENT_MOVE_TYPE,
t2.FROM_EQUIPMENT_NUMBER
from CONTAINER_HISTORY_MVW t2
where t2.CONTAINER_NUMBER = 'BEAU2105501'
and t2.EVENT is null)
SELECT cte1.t1_TRANSACTION_TIME,
cte1.t1_CONTAINER_NUMBER,
cte1.t1_LAST_UPDATE_TIME,
cte1.EVENT,
cte1.BOOKING_NUMBER,
cte2.t2_TRANSACTION_TIME,
cte2.CREATION_TIME,
cte2.EQUIPMENT_MOVE_TYPE,
cte2.FROM_EQUIPMENT_NUMBER
from cte1
inner join cte2
on cte1.t1_CONTAINER_NUMBER = cte2.t2_CONTAINER_NUMBER
and cte1.t1_LAST_UPDATE_TIME = cte2.t2_LAST_UPDATE_TIME
order by cte1.t1_TRANSACTION_TIME, cte2.t2_TRANSACTION_TIME
Most likely a cut and paste error but only select a column once or you have to alias the second to distinguish it from the first.
t1.DEP_CARRIER,
t1.DEP_MODE,
t1.DEP_CARRIER, -- duplicate column name in cte1
I would say it's because your columns you have put "as .."
try to format the column outside the WITH query.
I know that I have seen a couple of other questions about this error but I'm new to the sql JOIN so plz could you guy explain what I'm doing wrong.
Here's my query
SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs`
FROM `Klanten`, `kaart`
LEFT JOIN (`Intro`)
ON (Intro.KlantNummer = Klanten.Klantnummer)
WHERE kaart.KlantNummer = Klanten.Klantnummer
This is the Error I get like you have seen in the title
1054 - Unknown column 'Klanten.Klantnummer' in 'on clause'
And the db names are correct
Simple rule: Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. If you did that, you would not have an error:
SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs`
FROM `Klanten` JOIN
`kaart`
ON kaart.KlantNummer = Klanten.Klantnummer LEFT JOIN
`Intro`
ON Intro.KlantNummer = Klanten.Klantnummer ;
The problem is that the precedence of , and JOIN are different. Hence, the table before the comma is not known to the ON clause.
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.
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;