ORA-00904 invalid identifier - can't figure out error - sql

SQL dummy here who could really use some help.
I am attempting to sum up the number of records grouped by 2 fields in a set of tables. I can't figure out why I keep getting an invalid identifier error. I know the column names are correct and they exist. What am I doing wrong here?
select td.prot_number, td.block_number, count(bi.sn_bid) as num_bids
from timstabs.bids bi
inner join timstabs.tract_descs td
on td.sn_tracts_fk = bi.sn_tracts_fk
inner join timstabs.leases ls
on ls.sn_tracts_fk = bi.sn_tracts_fk
where ls.lease_status_cd = 'REJECT'
group by td.prot_number, td.block_number

Related

Error -- SQL Compilation Error: syntax error line 1 at position 0 unexpected 'INNER'

I am trying to perform an inner join on two tables by date and county ID. Whenever I run the query, I get the following error:
SQL Compilation Error: syntax error line 1 at position 0 unexpected 'INNER'
The code is as follows:
SELECT
ISO3166_1,
FIPS,
DATE,
CASES
FROM
"covid-19","public"."timeseries"
WHERE
ISO3166_1 = 'US'
INNER JOIN
"db"."population_schema"."pop_table"
ON
(("covid-19","public"."timeseries".fips = "db"."population_schema"."pop_table".fips)
AND
("covid-19","public"."timeseries".date= "db"."population_schema"."pop_table".date))
For reference, the date and FIPS data types are the same. Are there any glaringly obvious issues?
The WHERE clauses go after all JOINS:
SELECT
ISO3166_1,
FIPS,
DATE,
CASES
FROM "covid-19","public"."timeseries" AS a
INNER JOIN "db"."population_schema"."pop_table" AS b
ON a.fips = b.fips
AND a.date= b.date
WHERE
ISO3166_1 = 'US'
and if you alias those two tables, your join logic can be much more readable.

Unable to join two oracle tables

i have 2 tables.
First table SEC_SEAL_LOG with columns:
DATA_ADD,
DATA_AREA,
SEAL_NUMBER,
DATA_SEALING,
DATA_UNPLUG,
SORRUPTED.
SEC_WRITING_OFF_SEALS
second table with columns:
DATA, SEAL.
I want to put these 2 tables together, but I cannot understand where I have the error, I will be grateful for your help.
select DATA_ADD,
DATA_AREA,
SEAL_NUMBER,
DATA_SEALING,
DATA_UNPLUG,
СORRUPTED,
Data
from SEC_SEAL_LOG,SEC_WRITING_OFF_SEALS
where (data_area = (select data_area
from SEC_USERS_LIST
where login = LOWER(:APP_USER)
and SEAL_NUMBER = SEAL
)
or 20 >= (select u.role
from SEC_users_list u
where u.login = lower(:APP_USER)
)
)
and СORRUPTED = 'Так'
and SEAL_NUMBER = SEAL
ORDER BY data_add DESC
I amd getting this error
ORA-20999: Failed to parse SQL query! ORA-06550: line 7, column 4: ORA-00918: column ambiguously defined
The error Column Ambiguously Defined occurs when a column name is present in more than one table, and you have failed to specify which table.
You are doing that in this line: and SEAL_NUMBER = SEAL (which you have twice).
From which table to you want to compare that SEAL value?
Write it as SEC_SEAL_LOG.SEAL or SEC_WRITING_OFF_SEALS.SEAL or whatever table name you are trying to compare this value from, and it will get rid of the Column Ambiguously Defined error.

[Amazon](500310) Invalid operation: invalid reference to FROM-clause entry for table "fact_spv_commissioned_lot";

I have looked at a few similar questions on stackoverflow but none of it helped me. I keep on getting this error no matter what I do.
Amazon Invalid operation: invalid reference to FROM-clause entry for table "fact_spv_commissioned_lot";
SELECT COUNT(*) FROM staging_serials s
JOIN dim_md_company c ON (c.lsc_company_id = s.companyid)
JOIN staging_product p ON (s.compositeproductcode = p.compositeproductcode)
JOIN dim_packaging_level l ON (l.unit_of_measure = p.packaginguom)
WHERE c.sk_company_id = fact_spv_commissioned_lot.sk_company_id
AND s.lotnumber = fact_spv_commissioned_lot.lot_number
and p.sk_product_id = fact_spv_commissioned_lot.sk_product_id
and l.sk_packaging_level_id = fact_spv_commissioned_lot.sk_packaging_level_id
Because fact_spv_commissioned_lot is not listed in the table clause of the query. You're joining staging_serials, dim_md_company, staging_product, and dim_packaging_level tables. You need to include fact_spv_commissioned_lot table.

Why am i getting Unknown column 'list_class.pk_class_id' in 'on clause' error?

I want to get student details from different tables with given academic year and class.
Query that i used is:
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class,
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')
it shows the following error:
#1054 - Unknown column 'list_class.pk_class_id' in 'on clause'
columns of my table are:
tbl_stu_class:
pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`
list_class:
`pk_class_id`, `class_name`, `class_code`, `fk_user_id`
list_sections:
pk_section_id`, `section_code`, `section_description`, `fk_user_id`
list_acad_years:
`pk_acad_year_id`, `acad_year`, `acad_year_code`, `fk_user_id`
tbl_stu_details:
`pk_stu_id`, `stu_id`, `username`, `stu_fname`, `stu_mname`, `stu_sname`
list_sections:
`pk_section_id`, `section_code`, `section_description`, `fk_user_id`
Why did it say unknown column when the column is present?
It would be great help if you can help me make this query better...
Thanks in advance.
Your problem is that you are using old-style joins. Period. To make matters worse, you are combining them with new style joins.
The names of the tables are not understood across commas. That limits the scope of the definitions.
You appear to know how to write JOINs correctly. So, just fix the FROM clause and your code should work.
Your Inner join will alway connect to the last table in from statement if you are using multiple FROM table.
I have just change the position of INNER JOIN
try this let me know if it works.
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id),
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')

I am getting ORA-00904 "Invalid Identifier" when I use a legitimate column name in a left join

I am getting ORA-00904 "Invalid Identifier" when I use a legitimate column name in a left join, but I do not get an error using the same identifier (I copied and pasted to be sure) in the where clause if I use a standard (inner) join on the table I was trying to limit.
Example 1:
Select NAMES.R_NAME
From CMPLN, NAMES
Where CMPLN.CMPLN_ID = NAMES.CMPLN_ID
Works.
EXAMPLE 2:
Select NAMES.R_NAME
From CMPLN
Left Join NAMES On CMPLN.CMPLN_ID = NAMES.CMPLN_ID
Gets an error.
"ORA-00904: "CMPLN"."CMPLN_ID": invalid identifier
I am using Oracle.
(From an answer posted by the asker Jul 29 '15 at 12:40 that should have been an edit:)
I didn't present the code correctly for the answer to be apparent.
I had inserted another table in the From clause between CMPLN and the join, like this:
Select NAMES.R_NAME
From CMPLN, RSPN
Left Join NAMES On CMPLN.CMPLN_ID = NAMES.CMPLN_ID
That is what caused the error.
(In the Informix SQL I am used to this would have worked, but not in Oracle.)
I had a similar issue: ORA-00904: "TTR"."PARENT_CNTL_NBR": invalid identifier as per the below:
SELECT *
FROM TTR,
TST
LEFT JOIN ITF
ON NVL(TRIM(TST.CLIENT_USE_TXT), TRIM(SUBSTR(TTR.PARENT_CNTL_NBR, 1, 22))) = NVL(TRIM(ITF.CLIENT_USE_TXT), TRIM(SUBSTR(ITF.TRANSACTION_NO, 1, 22)))
WHERE TST.SECURITY_ADP_NBR = TTR.SECURITY_ADP_NBR
I fixed it by having all joins done by the join clause rather than in a where statement:
SELECT *
FROM TTR
INNER JOIN TST
ON TST.SECURITY_ADP_NBR = TTR.SECURITY_ADP_NBR
LEFT JOIN ITF
ON NVL(TRIM(TST.CLIENT_USE_TXT), TRIM(SUBSTR(TTR.PARENT_CNTL_NBR, 1, 22))) = NVL(TRIM(ITF.CLIENT_USE_TXT), TRIM(SUBSTR(ITF.TRANSACTION_NO, 1, 22)))