Oracle, invalid identifier error - oracle

I have this query that should:
create a listing that identifies different items in the inventory
table on the number of colors available for each option
But I'm getting an error saying:
Error at Command Line:167 Column:66 Error report: SQL Error:
ORA-00904: "INV"."ITEM_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
SELECT inv.inv_id,
item1.item_id,
(SELECT COUNT(*)
FROM (SELECT DISTINCT i.color FROM inventory i WHERE i.item_id = inv.item_id))
FROM inventory inv
INNER JOIN item item1
ON item1.item_id = inv.item_id
GROUP BY inv.inv_id, item1.item_id;

It sounds like you just want
SELECT inv.inv_id,
item1.item_id,
COUNT( distinct inv.color )
FROM inventory inv
JOIN item item1 ON (item1.item_id = inv.item_id)
GROUP BY inv.inv_id, item1.item_id
There doesn't seem to be any reason to hit the inventory table again in a subquery.

Related

Correct syntax for table name under Inner Join?

I am a complete beginner to BigQuery, and I am trying to create an inner join between two table names, where the column 'title' is the joining column. I believe my syntax is correct, but I do not know what I am doing wrong when I input the ON clause. Here is my syntax:
SELECT
*
FROM
book-to-film-adaptations.movies.movies_metadata_relevant
JOIN
book-to-film-adaptations.goodreads_books.goodreads_books_relevant_data
ON
movies_metadata_relevant.title = goodreads_books_relevant_data.title
I get this error message: Unrecognized name: movies_metadata_relevant at [8:3]
I have tried it with the full names (book-to-film-adaptations.movies.movies_metadata_relevant), but then I get an error message: "Syntax error: Unexpected keyword TO"
Any suggestions?
Thanks
You need to alias tables and use those like in below example - but in this case you will need
...
...
FROM
`book-to-film-adaptations.movies.movies_metadata_relevant` t1
JOIN
`book-to-film-adaptations.goodreads_books.goodreads_books_relevant_data` t2
ON
t1.title = t2.title
or if join columns have same name (like in your case) you can use below version
...
...
FROM
`book-to-film-adaptations.movies.movies_metadata_relevant` t1
JOIN
`book-to-film-adaptations.goodreads_books.goodreads_books_relevant_data` t2
USING (title)

Missing keyword - inner join with where

post_code is a primary key in the location table and foreign key in job. I am trying to join them, could anyone help.
SELECT job.start_date, job.cust_id, job.veh_id, location.post_code, location.country_name_location
FROM job
INNER JOIN location job.post_code ON location.post_code AND country_name_location = 'France';
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Your ON is in the wrong position:
SELECT job.start_date, job.cust_id, job.veh_id, location.post_code, location.country_name_location
FROM job
INNER JOIN location ON job.post_code = location.post_code AND country_name_location = 'France';
There is an error in your syntax. The ON keyword is meant to come after you specify the table you are joining with. So the correct query would be:
SELECT job.start_date, job.cust_id, job.veh_id, location.post_code, location.country_name_location
FROM job
INNER JOIN location ON job.post_code = location.post_code AND country_name_location = 'France';

00904. 00000 - "%s: invalid identifier" [duplicate]

This question already has an answer here:
Invalid Identifier SQL
(1 answer)
Closed 5 years ago.
Going crazy with this code. Can Anybody put me on the right track? I am using SQL Develope 3.1.07. Thanks.
SELECT
PHA.PO_HEADER_ID,
PRLA.ATTRIBUTE1 As "REQUISITION_NUMBER",
PHA.SEGMENT1 As PO_NUMBER,
DECODE (nvl(REG.DOCUMENT_TYPE, '-'),
'APW', 'Agreement for Performance of Work',
'APWEMER', 'Agreement for Performance of Work - Emergency',
'CONS', 'Consultant',
'CONCEMER', 'Consultant - Emergency',
'DFC', 'Direct Financial Contribution',
'FELLOW', 'Fellowship',
'GENEXT', 'General External Services',
'IMPRESTCC', 'Imprest/Credit Card',
'IS', 'Internal Services',
'LOA', 'Letter of Agreement',
'TSA', 'Technical Service Agreement') as DOCUMENT_TYPE,
HAOU.NAME As BUDGET_CENTRE,
PHA.CREATION_DATE As PO_CREATION_DATE,
PHA.AUTHORIZATION_STATUS,
PHA.APPROVED_DATE,
PHA.COMMENTS,
PHA.CLOSED_DATE,
PHA.CLOSED_CODE,
PDA.PO_DISTRIBUTION_ID,
AIDA.INVOICE_ID,
AIA.INVOICE_TYPE_LOOKUP_CODE AS INVOICE_TYPE,
AIA.REMIT_TO_SUPPLIER_NAME AS SUPPLIER_NAME,
AIA.REMIT_TO_SUPPLIER_ID AS SUPPLIER_ID,
AIA.INVOICE_DATE, AIA.INVOICE_NUM,
AIA.INVOICE_AMOUNT,
AIA.INVOICE_CURRENCY_CODE AS INVOICE_CURRENCY,
AIA.GL_DATE,
AIA.PAYMENT_CURRENCY_CODE AS PAYMENT_CURRENCY,
AIA.PAYMENT_CROSS_RATE_DATE AS PAYMENT_DATE,
AIA.DESCRIPTION, AIA.EXCHANGE_DATE,
AIA.EXCHANGE_RATE,
AIA.BASE_AMOUNT AS FUNCTIONAL_CURRENCY_AMOUNT
FROM
po_headers_all pha,
po_distributions_all pda,
ap_invoice_distributions_all aida,
ap_invoices_all aia,
po_requisition_lines_all prla,
po_req_distributions_all prda,
hr.hr_all_organization_units haou,
pa.pa_projects_all ppa
LEFT JOIN
xxwrp.xxwrp_service_reg_all reg
ON
reg.po_header_id = pha.po_header_id
WHERE
1=1
AND pha.segment1 = '201251390'
AND pda.req_distribution_id = prda.distribution_id
AND prda.requisition_line_id = prla.requisition_line_id
AND pha.po_header_id = pda.po_header_id
AND pda.po_distribution_id = aida.po_distribution_id
AND aida.invoice_id = aia.invoice_id
AND pda.project_id = ppa.project_id
AND haou.organization_id=ppa.carrying_out_organization_id
AND pha.creation_date > aia.invoice_date
GROUP BY
PHA.PO_HEADER_ID, PRLA.ATTRIBUTE1, PHA.SEGMENT1, REG.DOCUMENT_TYPE, HAOU.NAME, PHA.CREATION_DATE, PHA.AUTHORIZATION_STATUS, PHA.APPROVED_DATE, PHA.COMMENTS, PHA.CLOSED_DATE, PHA.CLOSED_CODE, PDA.PO_DISTRIBUTION_ID, AIDA.INVOICE_ID, AIA.INVOICE_TYPE_LOOKUP_CODE, AIA.REMIT_TO_SUPPLIER_NAME, AIA.REMIT_TO_SUPPLIER_ID, AIA.INVOICE_DATE, AIA.INVOICE_NUM, AIA.INVOICE_AMOUNT, AIA.INVOICE_CURRENCY_CODE, AIA.GL_DATE, AIA.PAYMENT_CURRENCY_CODE, AIA.PAYMENT_CROSS_RATE_DATE, AIA.DESCRIPTION, AIA.EXCHANGE_DATE, AIA.EXCHANGE_RATE, AIA.BASE_AMOUNT
When I run it, it returns this error:
ORA-00904: "PHA"."PO_HEADER_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 50 Column: 21
The issue is in mixing the old Oracle syntax with the ANSI join syntax; for example, say you have these tables:
create table t1(n1 number);
create table t2(n2 number);
create table t3(n3 number);
this is what you get:
SQL> select *
2 from t1,t2
3 left join t3 on t3.n3 = t1.n1
4 where t1.n1 = t2.n2;
left join t3 on t3.n3 = t1.n1
*
ERROR at line 3:
ORA-00904: "T1"."N1": invalid identifier
SQL> select *
2 from t1
3 inner join t2 on t1.n1 = t2.n2
4 left join t3 on t3.n3 = t1.n1;
no rows selected
The problem is your left join. There is probably a column missing; the one you are joining on.
Here is more details on the error you're receiving:
https://www.techonthenet.com/oracle/errors/ora00904.php

Need to Include a Name with Group By oracle sql

I have some Code here,
`SELECT a.Officer_ID, Count(Crime_ID)
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID)))
From Crime_Officers
Group by officer_Id));`
it outputs this,
`OFFICER_ID COUNT(CRIME_ID)
---------- ---------------
111115 9 `
Its cool and all, but my assignment only needs the officers last name which is in table A. I tried it with the Officer_ID and Count just to make sure the code was working. When I try it like this,
`SELECT Last
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID)))
From Crime_Officers
Group by officer_Id));`
I get this error,
`Error starting at line 1 in command:
SELECT Last
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID)))
From Crime_Officers
Group by officer_Id))
Error at Command Line:1 Column:8
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:`
Can anyone help or explain what I'm missing/Doing wrong?
You can only select a column if a) it's in the GROUP BY, b) it's derived from a GROUP BY column or c) it's the result of an aggregate function like MAX/MIN.
In your case, each Officer_ID should only have one Last value.
Simply change your GROUP BY to read:
GROUP BY a.Officer_ID, a.Last

Oracle update statement multiple tables

I am using Oracle (Still a little new to it) and each time i run the update statement below i get the following error message.
SQL Error: ORA-00904: "CH"."CONTRACT_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Here is the query:
UPDATE wr00262_catalogue_201310 ct SET
ct.PORTFOLIO_ID = (SELECT ch.PORTFOLIO_ID
from WR00262_CONTRACT_HEADER ch
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID)
WHERE ct.NPC in (SELECT ctl.NPC
FROM wr00262_contract_line ctl
WHERE ctl.CONTRACT_ID = ch.CONTRACT_ID);
I think i may need a join but not quite sure where or how. The contract_header table does have a column called CONTRACT_ID.
This is a scoping issue. The columns in one sub-query are not visible to any other sub-query. So try something like this:
UPDATE wr00262_catalogue_201310 ct SET
PORTFOLIO_ID = (
SELECT ch.PORTFOLIO_ID
from WR00262_CONTRACT_HEADER ch
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID
)
WHERE ct.NPC in (
SELECT ctl.NPC
FROM WR00262_CONTRACT_HEADER ch
join wr00262_contract_line ctl
on ctl.CONTRACT_ID = ch.CONTRACT_ID
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID
);

Resources