ORA-00904: "ADDRESS_USAGES"."AUS_PRO_REFNO": invalid identifier - sql

I am trying to run a select statement from two tables, the data that I want to return takes on 3-4 joins to achieve. I am getting the error
ORA-00904: "ADDRESS_USAGES"."AUS_PRO_REFNO": invalid identifier
when both tables and columns exist. I have read the post relating to this error but given that I am just starting out I cant make head nor tail of them. Any suggestions (be gentle). SQL below TIA
select ins_srq_no, adr_line_all from inspections
join properties
on inspections.ins_pro_refno = properties.pro_propref
join addresses
on properties.pro_propref = address_usages.aus_pro_refno
join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;

This on clause is incorrect, because you haven't yet joined to the address_usages table: on properties.pro_propref = address_usages.aus_pro_refno. This is causing Oracle to throw the error you're seeing; it's not about the table or column not existing, it's the fact that within that query, that identifier is invalid because you haven't yet joined to the table.
At a guess (I can't be 100% sure without seeing your table structures and foreign keys), you need to join addresses back to properties. If so, the query should look something like this:
select ins_srq_no, adr_line_all
from inspections
inner join properties
on inspections.ins_pro_refno = properties.pro_propref
inner join addresses
on properties.pro_propref = addresses.[column name of FK to properties]
inner join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;

Related

UPDATE with two INNER JOINS giving error "multi-part identifier could not be bound"

I am trying to write a SQL statement that performs an update after two tables are joined. The SET statement is giving me an error that the multi-part identifier could not be bound. I am thinking it is because I don't reference that table anywhere else but I am not sure where to put it. I can't join it because I am just using tbl_TXEX_IRSS as a lookup table to change a code. Here is what I have:
UPDATE [PAYROLL].[dbo].[TEMPBILLING]
SET [TEMPBILLING].Pay_Code = [tbl_TXEX_IRSS].tax_irss_code
FROM [PAYROLL].[dbo].[TEMPBILLING]
INNER JOIN [PAYROLL].[dbo].[ISP-BASIC]
ON [ISP-BASIC].Form_ID = [TEMPBILLING].ISP_Data_ID
INNER JOIN [PAYROLL].[dbo].[TAX-EXEMPT]
ON SUBSTRING([ISP-BASIC].Employee_ID,4,10) = [TAX-EXEMPT].Sage_ID
WHERE
[ISP-BASIC].ISP_Program like '%IRSS%'
and [ISP-BASIC].Billable='Yes'
and Status='In Prep'
The only unqualified column is:
and Status = 'In Prep'
I imagine Status is in multiple tables. Qualify all column names. Shorter table aliases would make the table easier to write and to read.
As for the table name tbl_TXEX_IRSS. That needs to be in a FROM clause.
Something like this, whereas unknown information is pseudo-coded with <Alias>.<Column>:
UPDATE billing
SET billing.Pay_Code = TxexIrss.tax_irss_code
FROM [dbo].[TEMPBILLING] billing
INNER JOIN [dbo].[ISP-BASIC] IspBasic ON IspBasic.Form_ID = billing.ISP_Data_ID
INNER JOIN [dbo].[TAX-EXEMPT] TaxExempt ON SUBSTRING(IspBasic.Employee_ID,4,10) = TaxExempt.Sage_ID
INNER JOIN [dbo].[tbl_TXEX_IRSS] TxexIrss ON <Alias>.<Column> = TxexIrss.<Colum>
WHERE (IspBasic.ISP_Program LIKE '%IRSS%') AND (IspBasic.Billable='Yes') AND (Status='In Prep');
Your normalization leaves also space for improvement. Consider changing the Billable column to a BIT NOT NULL with a default value and Status to an INT NOT NULL, referencing a little table where all possible states are defined. But the most costly one is IspBasic.ISP_Program LIKE '%IRSS%'. There should be a BIT NOT NULL called IsIrss or an INT NOT NULL called ProgramType or the like in table IspBasic, holding this information. Cutting a piece out of a string and comparing it - that can't be indexed, that needs a full table scan.

Inner Join Ambiguous Syntax

I'm not super familiar with SQL but I know the basics. I was recently trying to replicate some logic form reports to SQL Server 2012. I started with the custom query from Webi (a reporting tool) and was trying to make a view from it in SQL.
Here is what the query look like:
SELECT
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
dimGlobalShipDestinationCountryTbl.area,
dimGlobalShipDestinationCountryTbl.subarea,
dimGlobalCurrentProductTbl.sbuCodeDesc,
dimGlobalShipDateVw.shipDayOfWeekDesc,
sum(factSalesTblVw.globalSalesValue) AS 'Global Sales Value',
SUM(factSalesTblVw.salesUnitQuantity*GlobalFiles.dimCurrentGTINTbl.unitQty) AS 'Sales Unit Quantity'
FROM
dimGlobalCookCompaniesTbl INNER JOIN factSalesTblVw ON
(dimGlobalCookCompaniesTbl.globalCookCompanyID=factSalesTblVw.globalCookCompanyID)
INNER JOIN dimGlobalHistProductTbl ON (dimGlobalHistProductTbl.globalHistProductID=factSalesTblVw.globalHistProductID)
INNER JOIN dimGlobalCurrentProductTbl ON (dimGlobalHistProductTbl.globalCurrentProductID=dimGlobalCurrentProductTbl.globalCurrentProductID)
INNER JOIN dimGlobalHistShipCustomerTbl ON (factSalesTblVw.globalHistShipCustomerID=dimGlobalHistShipCustomerTbl.globalHistShipCustomerID)
INNER JOIN dimGlobalCurrentShipCustomerTbl ON (dimGlobalHistShipCustomerTbl.shipCustomerID=dimGlobalCurrentShipCustomerTbl.globalCurrentShipCustomerID)
***INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON (dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)***
INNER JOIN dimGlobalSalesAnalysisTbl ON (factSalesTblVw.globalSalesAnalysisID=dimGlobalSalesAnalysisTbl.globalSalesAnalysisID)
INNER JOIN dimGlobalShipDateVw ON (dimGlobalShipDateVw.shipJulianDate=factSalesTblVw.shipDateID)
INNER JOIN GlobalFiles.dimCurrentGTINTbl ON (GlobalFiles.dimCurrentGTINTbl.curGtinId=factSalesTblVw.GtinID)
WHERE
(
dimGlobalShipDateVw.shipYearNumber IN (DATEPART(yy,GETDATE())-1)
AND
dimGlobalCurrentShipCustomerTbl.shipCustomerNumberDesc
IN ( 'JPC000222-3','CNC000012-1' )
AND
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc = 'Return Credits'
)
GROUP BY
dimGlobalShipDateVw.shipDate,
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
dimGlobalShipDestinationCountryTbl.area,
dimGlobalShipDestinationCountryTbl.subarea,
dimGlobalCurrentProductTbl.sbuCodeDesc,
Upper(dimGlobalCurrentProductTbl.familyCodeDesc),
dimGlobalShipDateVw.shipYearNumber,
dimGlobalShipDateVw.shipDayOfWeekDesc,
dimGlobalCurrentProductTbl.madeByAbbr,
dimGlobalCookCompaniesTbl.companyDesc
This particular query runs on the production system if ran in the relevant database. When trying to make a view of this query in a different database, I precede the objects by [database_name].[schema/dbo] name.
On running the query, I get the error:
Invalid object name 'WWS.dbo.dimGlobalShipDestinationCountryTbl'
I try to find this particular table on the database, but it isn't there, though hovering over the table name in the query give a table definition but no script.
This table is present in an weird looking inner join (6th inner join) syntax like this:
INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON
(dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)
Two questions:
1. Can someone please explain this query syntax for inner join ?
2. This is pretty stupid but any ideas on how to look into possibly hidden table definitions ?
Two questions: 1. Can someone please explain this query syntax for inner join ?
The inner join in this case is nothing more than a table alias. The creator of the query thought aliasing the table would be easier to understand this name instead of the actual table name, or the same table is referenced twice and one would have to have an alias.
2. This is pretty stupid but any ideas on how to look into possibly hidden table definitions ?
Why? I think you just have a syntax error on your SQL when you added the database_name.schema syntax.
Think of the table alias like a column alias.... but and just like columns, you can omit the 'AS' keyword...
dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl is the same as
dimGlobalCountryTbl AS dimGlobalShipDestinationCountryTbl

Oracle Invalid Identifier ORA-00904

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.

error when joining 2 tables

Query :
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri
order by Name
The error that i get is :
Error code -1, SQL state 42X01: Syntax error: Encountered "order" at line 4, column 5.
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
Why am i getting an error ?
The error is because you forgot to specify JOIN criteria, like this:
SELECT i.Name, ri.Country, ri.State, ri.City
FROM Information as i
JOIN ResidenceInformation as ri ON ri.column = i.column
ORDER BY Name
You need to replace column with the names of the appropriate columns that link the tables correctly for the output you need.
You should also specify the table alias in your ORDER BY, to protect against an ambiguous column reference error.
You get an error because your syntax is wrong: after join there needs to be on, like this:
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri
on ri.info_id=i.id -- <<< Added a join condition
order by Name
SQL needs to know how to "link up" the rows of the table that you are joining to the row(s) of the other table(s) in the query. I am assuming that ResidenceInformation has a foreign key into Information called info_id.
If the Name is present in both ResidenceInformation and Information, you need to prefix it with the table name or an alias. In fact, it's a good idea to do it anyway for added clarity.
I think you may have forgot to tell the join clause which columns to join against. You need to tell the database how these 2 tables connect to each other. Something like ON i.id = ri.InformationId
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri ON i.id = ri.InformationId
order by i.Name
Also, you may need the table alias in the order by clause, which I've added as well.

SQLite multiple table INNER JOIN with USING (...) Errors

I am trying to run a query on a SQLite database that INNER JOINs two additional tables:
SELECT
usages.date AS date,
usages.data AS data,
stores.number AS store,
items.name AS item
FROM usages
INNER JOIN stores USING (store_id)
INNER JOIN items USING (item_id)
However, I get the error
SQL error: cannot join using column item_id - column not present in both tables
I know I can use the explicit INNER JOIN stores ON usages.store_id = stores.store_id (and it works), but:
why does the USING query throw an error in SQLite?
It doesn't on MySQL...
I should note:
This isn't a problem for me, as I am using the ON syntax, but I would like to know why this is happening.
So you have:
INNER JOIN items USING (item_id)
...and you get an error that says:
SQL error: cannot join using column item_id - column not present in both tables
That's got to be one of the least cryptic error messages I've seen.
What I don't like is that it's not clear to me what table is being compared to ITEMS.item_id - is it STORES or USAGES? Which is why I refrain from the USING or NATURAL join syntax...