I'm trying to join two tables and followed every step but I keep getting this error "missing FROM-clause entry for table "turbocharger". I have two tables: turbocharger and engine. This is the query:
SELECT *
FROM "Engine"
FULL OUTER JOIN "Turbocharger" ON Turbocharger.Manufacturer_ID = Engine.Manufacturer_ID;
It's very possible that you've created your table with quotes (create table "Turbocharger")
In this case PostgreSQL will always want to access this table with quoutes because name became case-sensitive.
So you need this
SELECT * FROM "Engine" FULL OUTER JOIN "Turbocharger" ON "Turbocharger".Manufacturer_ID = "Engine".Manufacturer_ID;
but I recommend use aliases
SELECT * FROM "Engine" e FULL OUTER JOIN "Turbocharger" t ON t.Manufacturer_ID = e.Manufacturer_ID;
HTH
Related
I want to do a join with another table. I followed the tutorial on the site and the my code compiles but it's not performing the join and instead just selects the first table.
SELECT
"table1.col1"
"table1.col2"
"table1.col3"
FROM
"table1"
JOIN "table2" ON "table1"."col1" = "table2"."col1"
LIMIT
1
It is only returning the data from table1 and not concatenating the columns where the condition for table1 and table2 is met.
I execute the query using the following code:
Entity::find()
.from_raw_sql(Statement::from_string(DatabaseBackend::Postgres, query.to_owned()))
.all(&self.connection)
.await?
That returns a Vec<Model>. Is this the correct way? Also, how can I build a SQL statement using an Entity as the base which looks like SELECT * from "table1".
After 'SELECT' (and before 'FROM') you are specifying which columns
to include in the output,
and you are selecting only three columns from table1 in your code.
Add the columns you want to include from table2 here, and you may get
the results you want.
When I run this query in Redshift:
select sd.device_id
from devices.s_devices sd
left join devices.c_devices cd
on sd.device_id = cd.device_id
I get an error like this:
ERROR: Spectrum nested query error
DETAIL:
-----------------------------------------------
error: Spectrum nested query error
code: 8001
context: A subquery that refers to a nested table cannot refer to any other table.
query: 0
location: nested_query_rewriter.cpp:726
process: padbmaster [pid=6361]
-----------------------------------------------
I'm not too sure what this error means. I'm only joining to one table I'm not sure which "other table" it's referring to, and I can't find much info about this error on the web.
I've noticed if I change it from left join to join, the error goes away, but I do need to do a left join.
Any ideas what I'm doing wrong?
Redshift reference mentions:
If a FROM clause in a subquery refers to a nested table, it can't refer to any other table.
In your example, you're trying to join two nested columns in one statement.
I would try to first unnest them separately and only then join:
with
s_dev as (select sd.device_id from devices.s_devices sd),
c_dev as (select cd.device_id from devices.c_devices cd)
select
c_dev.device_id
from c_dev
left join s_dev
on s_dev.device_id = c_dev.device_id
The solution that worked for me, was to create a temporary table with the nested table's data and then join the temp table with the rest of the tables I needed to.
For example, if the nested table is spectrum.customers, the solution will be:
DROP TABLE IF EXISTS temp_spectrum_customers;
CREATE TEMPORARY TABLE
temp_spectrum_customers AS
SELECT c.id, o.shipdate, c.customer_id
FROM spectrum.customers c,
c.orders o;
SELECT tc.id, tc.shipdate, tc.customer_id, d.delivery_carrier
FROM temp_spectrum_customers tc
LEFT OUTER JOIN orders_delivery d on tc.id = d.order_id;
SELECT pl_id,
distinct ON (store.store_ID),
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID;
while running this query in postgres server i am getting the below error..How to use the distinct clause..in above code plan 1 is the schema name.
ERROR: syntax error at or near "distinct" LINE 2: distinct ON
(store.store_ID),
You are missing an order by where the first set of rows should be the ones specified in the distinct on clause. Also, the distinct on clause should be at start of the selection list.
Try this:
SELECT distinct ON (store_ID) store.store_ID, pl_id,
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID
order by store_ID, pl_id;
I want to list all constraint names with their respective column name for a table. I am using the below mentioned code :
select *
from sys.objects
where parent_object_id = object_id('qw') --this gives me the constraints lists but does not give me columns in which they are applied.
select *
from sys.columns
where object_id = object_id('qw') -- this gives me the column list of the table.
My problem is that I am not able to join these two queries to get columns along with their constraints.
Sounds like you need CONSTRAINT_COLUMN_USAGE.
I am joining three tables and generating a new table. However, the new table is empty. Below is my query:
CREATE TABLE TEMP (WORD, TOTALCOUNT, AGENCYNAME) AS
SELECT NSFABSTRACTS.WORD, DOCUMENT_FREQUENCY.TOTALCOUNT, AGENCY.AGENCYNAME
FROM NSFABSTRACTS LEFT JOIN DOCUMENT_FREQUENCY
ON NSFABSTRACTS.WORD=DOCUMENT_FREQUENCY.WORD
INNER JOIN AGENCY
ON NSFABSTRACTS.FILEID=AGENCY.FILEID;
When I do not add the top line to create a table, the results look fine. But when I add the top line, it generates an empty table. Any thoughts why is this?
I think it is because of your inner join; I cannot say surely without looking at your data in the database. but try following;
CREATE TABLE TEMP (WORD, TOTALCOUNT, AGENCYNAME) AS
SELECT NSFABSTRACTS.WORD, DOCUMENT_FREQUENCY.TOTALCOUNT, AGENCY.AGENCYNAME
FROM NSFABSTRACTS LEFT JOIN DOCUMENT_FREQUENCY
ON NSFABSTRACTS.WORD=DOCUMENT_FREQUENCY.WORD
LEFT JOIN AGENCY
ON NSFABSTRACTS.FILEID=AGENCY.FILEID;