I am trying to run the following code:
create or replace view StoreContactPurchaseHistoryView AS
select
STORE.StoreName,
StoreContactAndPhone(contact,phone),
PURCHASE_ITEM.PurchaseItemID,
PURCHASE_ITEM.PurchaseDate,
PURCHASE_ITEM.ItemDescription,
PURCHASE_ITEM.PriceUSD
from
store,
purchase_item
left join purchase_item on purchase_item.storeid=store.storeid
;
I keep getting this error:
Error report -
ORA-00904: "STORE"."STOREID": invalid identifier
00000 - "%s: invalid identifier"
Never use commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.
That said, you want something like:
select s.StoreName, StoreContactAndPhone(contact, phone) as col,
pi.PurchaseItemID, pi.PurchaseDate,
pi.ItemDescription, pi.PriceUSD
from store s join
purchase_item pi
on pi.storeid = s.storeid;
Note that I also added table aliases so the query is easier to write and to read.
You are mixing explicit and implicit joins, and seem to need only one join:
create or replace view StoreContactPurchaseHistoryView AS
select
s.StoreName,
StoreContactAndPhone(contact,phone),
p.PurchaseItemID,
p.PurchaseDate,
p.ItemDescription,
p.PriceUSD
from
store s
left join purchase_item p on p.storeid = s.storeid
;
I used table aliases to shorten the query.
Side note: it is unclear what expression StoreContactAndPhone(contact,phone) actually refers to; I left it as-is in the query.
Change it to this:
create or replace view StoreContactPurchaseHistoryView AS
select
STORE.StoreName,
StoreContactAndPhone(contact,phone),
PURCHASE_ITEM.PurchaseItemID,
PURCHASE_ITEM.PurchaseDate,
PURCHASE_ITEM.ItemDescription,
PURCHASE_ITEM.PriceUSD
from
store
left join purchase_item on purchase_item.storeid=store.storeid
;
Related
I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.
Hi I try to adapt this blog post to my needs using Oracle to model a Data Vault architecture.
With the following code I try to build a link between two hubs referencing on the original source table "orders".
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders as src
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order AS dest
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
However I receive following error.
Error at Command Line : 192 Column : 17
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Any help or hint much appreciated!
There are two places where you have used the as keyword. For giving alias to the table you just need to write the name of the alias after table name. as should not be used there.
Try the following code: (changes are mentioned inline)
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders src -- removed AS from here
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order dest -- removed AS from here
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
Cheers!!
Remove the as in your query i.e. instead of
FROM orders as src
use
FROM orders src
Oracle doesn't support the as alias, unless it is/will be added in most current versions
I am trying to practice inner joins but in vain.I am getting table does not exist error while executing inner join statements even though I have created tables and data are present
Kindly help me with this.Also, it shows Customers is disconnected from rest of the join graph.
I have tried using double quotes for table names but then it gives different error as
ORA-00904: "CUSTOMERS"."CUSTOMER_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
SELECT
Orders.OrderID,
Customers.CustomerName
FROM
Orders
inner join Customers on Orders.Customer_Id = Customers.Customer_Id;
Its Oracle 11g and SYS is the owner.
The problem could be due to your create table statement,which contains double quoted table names, that are not all in uppercase. Remember, oracle always stores table names and column names in uppercase by default, unless you have used double quoted strings with varying case in the create/Alter statements.Using the whole table names to refer the columns should be avoided, as that is the reason for invalid identifier error in your case.
So, either rename the table from "Customers" to CUSTOMERS and "Orders" to ORDERS if you can, or use quotes for table names in your select query along with appropriate aliases .
SELECT
o.OrderID,
c.CustomerName
FROM
"Orders" o
inner join "Customers" c on o.Customer_Id = c.Customer_Id;
Also, as #horse has already suggested, create table in any sample schema like HR, SCOTT etc and not in SYS.
I have a very simple query that I am trying to execute:
select *
from submissions
inner join (
select *
from hackers
inner join challenges
on hackers.hacker_id = challenges.hacker_id
) dtable
on submissions.challenge_id = dtable.challenge_id
and submissions.hacker_id = dtable.hacker_id;
Oracle rejects it with:
ORA-00904: "DTABLE"."HACKER_ID": invalid identifier.
I have kept the alias dtable visible by keeping it outside the brackets. Why is Oracle rejecting my query?
SELECT * in your sub-query is a problem.
You have the same column name in both tables that are being joined. This means that you're trying to create an inline-view called dtable where at least two columns have the same name (in this case both tables have a hacker_id column and your use of * is essentially saying "use them both".). You can't do that.
You're going to need SELECT hackers.a, hackers.b, challenges.x, challenges.y, etc, etc in your sub-query. By being explicit in this way you can ensure that no two columns have the same name.
An alternative could be SELECT hackers.*, challenges.a AS c_a, challenges.b AS c_b, etc.
Either way you are being explicit about which fields to pick up, what their positions and names are, etc. The end result being that you can then avoid columns having the same name as other columns.
You do not need subqueries for this. Your query is not actually "simple". The simple form looks more like this:
select . . .
from submissions s join
hackers h
on s.hacker_id = h.hacker_id join
challenges c
on s.challenge_id = c.challenge_id;
Note that I removed the condition between challenge and hackers on hacker_id. That extra join condition doesn't really make sense to me (although it might make sense if you provided sample data).
As others have said: The sub-select selects two different columns hacker_id from two different tables. This confuses Oracle.
But there is no need for a sub-select
select * from
submissions
inner join challenges
on submissions.challenge_id = challenges.challenge_id
inner join hackers
on submissions.hacker_id = hackers.hacker_id;
I keep getting this error trying to run this simple Join.....
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem, client
INNER JOIN client
ON docregitem.nclient = client.nclient
ORA-00904: "DOCREGITEM"."NCLIENT": invalid identifier
I can do a select and all the columns are present and correct...
I think the query you want is:
SELECT dr.reviewdate, dr.nclient, c.name
FROM docregitem dr INNER JOIN
client c
ON dr.nclient = c.nclient;
Your from clause has a comma in it. This is a lot like a cross join, but it affects the columns. These are not known in the on clause, which is what is causing the problem.
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem
INNER JOIN client
ON docregitem.nclient = client.nclient
you try this one as you used client table twice one with simple join and other with inner join without giving the alias name to the table so the compiler is confused in selecting and comparing column from client table.