invalid reference to FROM-clause entry for table "t" [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I have wrote this join statement in sql and triple checked everything and have tried multiple things but can not seem to shake this bug.-- The Talent Acquisition team is looking to fill some open positions.
They want you to get them the territory_description and region_description for territories that do not have any employees, sorted by territory_id.
Let's tackle this one piece at a time.
In order to achieve this result set, we will need to join the territories and regions table together.
So first, select the territory_description column of the territories table, aliased as t, and the region_description of the regions table, aliased as r.
Write a FROM statement for the territories table. Alias it as 't' as you do so.
Then, write a JOIN statement, joining the regions table, aliasing it as 'r'.
This JOIN should find records with matching values ON region_id in the territories and regions tables.
If you run the query you've constructed at this point, you should see a result set that contains territory descriptions and corresponding region descriptions.
But we're not done! We want only records WHERE the territories do not have any employees.
Below the JOIN statement, write a WHERE statement to create a subquery.
Find WHERE the territory_id in the territories table is NOT IN the result set from a subquery that selects the territory_id from the employee_territories table.
Finally, take the final result set and order by territory_id.
SELECT t.territory_description, r.region_description, t.region_id, r.region_id
FROM territories t, regions r
JOIN regions
ON t.region_id = r.region_id
ERROR: invalid reference to FROM-clause entry for table "t"
LINE 29: ON t.region_id = r.region_id
^
HINT: There is an entry for table "t", but it cannot be referenced from this part of the query.
SQL state: 42P01
Character: 1524
I have tried references, removing the region ids from the select statements, and even changing some aliases around or putting different tables in the from statement but nothing seems to work.

Your SQL query is not structured right - you are trying to do both an implicit JOIN via , regions r and an explicit JOIN via JOIN regions.
Here's a corrected version of your query that just uses the explicit JOIN:
SELECT t.territory_description, r.region_description, t.region_id, r.region_id
FROM territories t
JOIN regions r
ON t.region_id = r.region_id

Related

SQL refusing to do a join even when every identifier is valid? (ORA-00904)

Made this account just to ask about this question after being unable to find/expending the local resources I have, so I come to you all.
I'm trying to join two tables - ORDERS and CUSTOMER - as per a question on my assignment
For every order, list the order number and order date along with the customer number, last name, and first name of the customer who placed the order.
So I'm looking for the order number, date, customer number, and the full name of customers.
The code goes as such
SELECT ORDERS.ORDR_ORDER_NUMBER, ORDERS.ORDR_ORDER_DATE, ORDERS.ORDR_CUSTOMER_NUMBER, CUSTOMER.CUST_LAST, CUSTOMER.CUST_FIRST
FROM ORDERS, CUSTOMER
WHERE ORDERS.ORDR_CUSTOMER_NUMBER = CUSTOMER.CUST_CUSTOMER_NUMBER;
I've done this code without the table identifiers, putting quotation marks around ORDERS.ORDR_CUSTOMER_NUMBER, aliases for the two tables, and even putting a space after ORDR_ in both SELECT & WHERE for laughs and nothing's working. All of them keep coming up with the error in the title (ORA-00904), saying [ORDERS.]ORDR_CUSTOMER_NUMBER is the invalid identifier even though it shouldn't be.
Here also are the tables I'm working with, in case that context is needed for help.
Anyway, the query that produces the result you want should take the form:
select
o.ordr_order_number,
o.ordr_order_date,
c.cust_customer_number,
c.cust_last,
c.cust_first
from orders o
join customer c on c.cust_customer_number = o.ordr_customer_number
As you see the query becomes a lot easier to read and write if you use modern join syntax, and if you use table aliases (o and c).
You have to add JOIN or INNER JOIN to your query. Because the data comes from two different tables the WHERE clause will not select both.
FROM Orders INNER JOIN Customers ON Orders.order_Customer_Number = Customer.Cust_Customer_Number

Why am I only getting one row from this Query? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 2 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm querying my database and expecting to get three rows back, but am only getting one.
Here is my table structure:
This is my query:
SELECT M.MovieTitle, D.Director
FROM dbo.MOVIES_MAIN M, dbo.DIRECTORS D, dbo.DIRECTORS_MOVIES_M2M M2M
WHERE M.MovieId = 'tt0010721'
AND M.MovieId = M2M.MovieId
AND D.DirectorId = M2M.DirectorId
This is what's in the Many-to-Many table for the movieId (rectangled):
...and here is the result, a single row:
I expect/want to get all three Directors seen in the Many-to-Many table (nm0085133, nm0349785, and nm0674600) but only get the one. Why? Apparently my SQL is wrong, but I don't know just where it is wrong.
Note, from google:
Tarnished Reputations is a 1920 American silent adventure drama film directed by Herbert Blache, Alice Guy-Blaché, and Léonce Perret
Your existing query and the screen copy from the bridge table really make it look like only one out of the three directors exists in the directors table - so the inner join filters out the two others.
You could run the query as a left join (which is easy of we use standard join syntax):
select m.movietitle, dm.movieid, d.director
from directors_movies_m2m dm
inner join movies_main m on dm.movieid = m.movieid
left join directors d on d.directorid = dm.directorid
where dm.movieid = 'tt0010721'
"Missing" directors will show up as null in the resultset - you can then decided what to do next (maybe fix your data).
Assuming that this solves the problem: consider setting up foreign key constraint in the bridge table to prevent "orphan" rows. The DDL of the table could look like:
create table directors_movies_m2m (
id int identity(1, 1) primary key,
movieid int references movies_main(movieid),
directorid int references directors(directorid)
);

Table join without the first tables id? [duplicate]

This question already has answers here:
Why is a primary-foreign key relation required when we can join without it?
(5 answers)
Foreign Keys vs Joins
(2 answers)
Closed 4 years ago.
my question from the following query is how does the right join to the dm_fiscal_cal table work?
I understand that you can join on fields that don't have a pk fk relationship but how does this ( on mom.approval_wes = fc.week_ending_sunday ) work when it isn't related to the job_order table?
SELECT
jo.order_id
,cd.client_id
,st.staff_name
,c.company_name
,ma.market_code
,SUM(mom.gross_profit)
FROM
---- main table for order information
-- anchoring the query in this table means only clients with orders will return
job_order as jo
LEFT JOIN
--- main mart table for "aqent staff" coordinator id is the agent
dm_staffhier_d as st
on jo.coordinator_id = st.person_id
---- secondary table for clients and their markets each order is related to a "client / market" combination.
--- You must join on both to identify the correct market for the client where the order exists
LEFT JOIN
client_default as cd
on jo.client_id = cd.client_id and
jo.market_id=cd.market_id
--- primary table for clients
--- each client id is represented once and only once in this tbl
LEFT JOIN
client as c
on cd.client_id=c.client_id
--- market lookup table. there are market_ids that relate to many objects (orders, clients, people, leads, ..etc)
--- always make sure you're anchoring the market_id (Fk) on the correct object for the query results. You can (and often do) need
-- to alias this table for multiple uses
LEFT JOIN
market as ma
on jo.market_id= ma.market_id
---- main finanacial data mart tbl. each order fee for each week, for each client is represented.
--- you can join on order id for this query. You could use otherer relations ships for other queries
LEFT JOIN
dm_mainordermetrics_f as mom
on jo.order_id=mom.order_id
--- fiscal calendar tbl
--- we're joining this to the financial tbl because we're looking for GP$ in a certain period. That period is
--- easily found by using the dm_fiscal_cal tbl (fiscal year 2017).
RIGHT JOIN
dm_fiscal_cal as fc
on mom.approval_wes = fc.week_ending_sunday
WHERE
--- market requirement for LA, and BOS
ma.short_description IN ('Los Angeles', 'Boston')
--- not a good solution for this - check having clause
-- AND mom.gross_profit >0
AND fc.fiscal_year = 2017
GROUP BY
jo.order_id
,st.staff_name
,cd.client_id
,c.company_name
,ma.market_code
having sum(mom.gross_profit) > 0
order by 4,1
--- you should get 1509 rows
So it seems like your new to StackOverflow and SQL. First, I'll try and answer your original question.
My interpretation is that you are asking if the second LEFT JOIN (which is also sometimes referred to as a LEFT OUTER JOIN) is a valid statement in SQL even though it does not contain either the job.order_id nor the fiscal.cal_id.
The answer to this question is yes the statement is a valid one and it will run. That does not mean it will necessarily return any records or that it is doing exactly what you want it to be doing.
I apologize if that was not your intended question, but this brings me to my second point.
You should edit your original question to be more clear about what you want to know. If you did any research on your own before asking it, provide links to the sites you were looking through. Add the relevant columns on the tables in the FROM clause of your query. Lastly, think carefully about what specifically it is that you don't understand. Perhaps highlight those line(s) of code in your question.
All of this will help us to better answer your question.

Sql Developer - I am very new (Learning some basics) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've very new to SQL Developer and I have some practice business problems to help build my knowledge..
What im trying to do is I have a table called 'VMStaff' with columns of Name, Department, Favourite Colour, Gender, Job Role and Manger name..
Within this table, some of the managers have entries under 'Manager Name' and 'Name'
What I want from my output is Name of a colleague and their managers favourite colour.. I've tried doing this via self joins but must be missing something - Is anyone able to help or shed light on this?
I have tried twice to make this work, see below -
NUMBER1 -
SELECT vmstaff1.name, vmstaff2."FAVOURITE COLOUR"
from vmstaff name, vmstaff "Favourite Colour"
Left JOIN vmstaff1 vmstaff2
on vmstaff1."Manager Name" = Vmstaff2.Name
NUMBER 2 -
Select VM1.Name, VM2."Favourite Colour"
From vmstaff.Name as VM1
Inner Join vmstaff."Favourite Colour" as VM2
on VM1."Manager Name" = VM2."Favourite Colour"
I im lacking understanding of how to join the table and how to use the alias. In attempt NUMBER1 I get the error message Invalid SQL Statement and in attempt number 2 I get the error message SQL Command not properly ended
You'll be joining back to the table again:
select t1.name, t2.favouritecolour as man_fav_col
from Table1 t1
left join Table1 t2
on t1.managername = t2.name
Using the alias allows you to use the same table as if it's a different one. In this case, every name provides the favourite colour and acts like a primary key, then managername is essentially a foreign key within the same table.
Now, the reason for a left join instead of an inner join? The top boss won't have a manager, but might not want to be excluded...

Why PostgreSQL is not accepting while group by on one table and selecting towards another tables [duplicate]

This question already has an answer here:
PGError: ERROR: column "p.name" must appear in the GROUP BY clause or be used in an aggregate function
(1 answer)
Closed 8 years ago.
I am using postgreSQL version
PostgreSQL 9.1.9 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.7.2-22ubuntu5) 4.7.2, 64-bit,my question is am joining two tables,Let name it as temp1 and temp2 ,here i need to join this two table
Table structure is
marks_map
marks int
stud_id int
student
stud_id int
class_id int
here my query
select class_id,stud_id,count(marks)
from student as s
inner join marks_map as m on (s.stud_id=m.stud_id) group by stud_id
Here i get error as
ERROR: column "s.class_id" must appear in the GROUP BY clause or be used in an aggregate function
Why does this error happen? If I use class_id in group by it's running successfully.
You have to add the class_id attribute to your group by clause because in your select part of the statement there is no aggregation function over this attribue.
In GROUP BY statments you have to add all the attributes over which you haven't aggregated after the GROUP BY clause.
For example:
SELECT
non-aggregating-attr-1, non-aggregating-attr2, non-aggregating-attr3, sum(attr4)
FROM
table
GROUP BY
non-aggregating-attr-1, non-aggregating-attr2, non-aggregating-attr3
That's the way group by work.
You can check your data like
select
array_agg(class_id) as arr_class_id,
stud_id, count(marks)
from student as s
inner join marks_map as m on (s.stud_id=m.stud_id)
group by stud_id
and see how much class_id you have for each group. Sometimes your class_id is dependant from stud_id (you have only one elemnet in array for each group), so you can use dummy aggregate like:
select
max(class_id) as class_id,
stud_id, count(marks)
from student as s
inner join marks_map as m on (s.stud_id=m.stud_id)
group by stud_id
You should be able to understand the problem on a simplified case that doesn't even involve a JOIN.
The query SELECT x,[other columns] GROUP BY x expresses the fact that for every distinct value of x, the [other columns] must be output with only one row for every x.
Now looking at a simplified example where the student table has two entries:
stud_id=1, class_id=1
stud_id=1, class_id=2
And we ask for SELECT stud_id,class_id FROM student GROUP BY class_id.
There is only one distinct value of stud_id, which is 1.
So we're telling the SQL engine, give me one row with stud_id=1 and the value of class_id that comes with it. And the problem is that there is not one, but two such values, 1 and 2. So which one to choose? Instead of choosing randomly, the SQL engine yields an error saying the question is conceptually bogus in the first place, because there's no rule that says each distinct value of stud_id has its own corresponding class_id.
On the other hand, if the non-GROUP'ed output columns are aggregate functions that transform a series of values into just one, like min, max, or count, then they provide the missing rules that say how to get only one value from several. That's why the SQL engine is OK with, for instance: SELECT stud_id,count(class_id) FROM student GROUP BY stud_id;.
Also, when faced with the error column "somecolumn" must appear in the GROUP BY clause, you don't want to just add columns to the GROUP BY until the error goes away, as if it was purely a syntax problem. It's a semantic problem, and each column added to the GROUP BY changes the sense of the question submitted to the SQL engine.
That is, GROUP BY x,y means for each distinct value of the (x,y) couple. It does not mean GROUP BY x, and hey, since it leads to an error, let's throw in the y as well!