Postgres "missing FROM-clause entry" error on query with WITH clause - sql

I am trying to use this query in Postgres 9.1.3:
WITH stops AS (
SELECT citation_id,
rank() OVER (ORDER BY offense_timestamp,
defendant_dl,
offense_street_number,
offense_street_name) AS stop
FROM consistent.master
WHERE citing_jurisdiction=1
)
UPDATE consistent.master
SET arrest_id = stops.stop
WHERE citing_jurisdiction=1
AND stops.citation_id = consistent.master.citation_id;
I get this error:
ERROR: missing FROM-clause entry for table "stops"
LINE 12: SET arrest_id = stops.stop
^
********** Error **********
ERROR: missing FROM-clause entry for table "stops"
SQL state: 42P01
Character: 280
I'm really confused. The WITH clause appears correct per Postgres documentation. If I separately run the query in the WITH clause, I get correct results.

From the fine manual:
There are two ways to modify a table using information contained in other tables in the database: using sub-selects, or specifying additional tables in the FROM clause.
So you just need a FROM clause:
WITH stops AS (
-- ...
)
UPDATE consistent.master
SET arrest_id = stops.stop
FROM stops -- <----------------------------- You missed this
WHERE citing_jurisdiction=1
AND stops.citation_id = consistent.master.citation_id;
The error message even says as much:
ERROR: missing FROM-clause entry for table "stops"

This can also happen if you mistype a table name. For example:
UPDATE profiles SET name = ( profile.first_name ) WHERE id = 1
Instead of profiles i incorrectly used profile !! This would work:
UPDATE profiles SET name = ( profiles.first_name ) WHERE id = 1

Related

Table Column Not Updating

update
table_1 as q
set
q_sequence = (row_number() OVER(
PARTITION BY
q.account_uid
,q.c_c_uid
,q.a_uid
order by q.a_time ASC) as new
This does not work, I get the following error message:
Query 1: ERROR: syntax error at or near "as" LINE 10: order by
q.a_time ASC) as new
Any suggestions?
You are incorrectly using the as keyword here.
as is used for aliasing a table or a column in a select statement:
https://www.w3schools.com/mysql/mysql_alias.asp
Removing as new should solve you syntax error.

Hive "with" clause syntax

The with syntax is absolutely not cooperating, can not get it to work. Here is a stripped down version of it
set hive.strict.checks.cartesian.product=false;
with
newd as (select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table1),
oldd as ( select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table2
where ds='2022-09-08')
select
(newd.risk_score_hi-oldd.risk_score_hi)/newd.risk_score_hi diff_risk_score_hi,
(newd.risk_score_cur-oldd.risk_score_cur)/newd.risk_score_cur diff_risk_score_cur,
from newd cross join oldd
order by 1 desc
Apache Hive Error
[Statement 2 out of 2] hive error: Error while compiling statement:
FAILED: SemanticException [Error 10004]: Invalid table alias or
column reference 'newd': (possible column names are:
diff_risk_score_hi, diff_risk_score_cur)
I had been following the general form shown here: https://stackoverflow.com/a/47351815/1056563
WITH v_text
AS
(SELECT 1 AS key, 'One' AS value),
v_roman
AS
(SELECT 1 AS key, 'I' AS value)
INSERT OVERWRITE TABLE ramesh_test
SELECT v_text.key, v_text.value, v_roman.value
FROM v_text JOIN v_roman
ON (v_text.key = v_roman.key);
I can not understand what I am missing to get the inline with views to work.
Update My query (the first one on top) works in Presto (but obviously with the set hive.strict.checks.cartesian.product=false; line removed). So hive is really hard to get happy for with clauses apparently. I tried like a dozen different ways of using the aliases.

PgAdmin4 keeps returning: ERROR: syntax error at or near "IN"

I have a query that updates all records inside my table and the values inside of each column should be in sync with my other table so i created this query:
UPDATE dashboard.event SET operation_start_time IN
(SELECT operation_start FROM dashboard.inventory), operation_end_time IN
(SELECT operation_end FROM dashboard.inventory)
WHERE terminal_id IN (SELECT terminal_id FROM dashboard.inventory)
but the problem postgres keep returning me "ERROR: syntax error at or near "IN"" in which i don't get why. If i put "=" instead of "IN" it returns me the error:
ERROR: more than one row returned by a subquery used as an expression
For the logic of these query. I have an inventory table and in it has a column name operation_start and operation_end. I want the data in those columns to be updated or inserted in the event table for each terminal_id
Any help will be appreciated please. Thank you!
If you want to update the columns in event from inventory for a given terminal_id, the the syntax would look like:
UPDATE dashboard.event e
SET operation_start_time = e.operation_start,
operation_end_time = e.operation_end
FROM dashboard.inventory i
WHERE e.terminal_id = i.terminal_id;

Invalid identifier error when all listed elements exist

I'm doing a basic database assignment on ETL. I'm trying to update a column by referencing three distinct columns in two other tables.
I am getting this error report in SQL Developer:
SQL Error: ORA-00904: "DIMTIME"."DAY_TIME": invalid identifier
00904. 00000 - "%s: invalid identifier"`
I feel my syntax is bad as all listed elements exist.
Code snippet is:
update fact_stage set date_sk = (
select date_sk from time_stage
where (time_stage.year_time = dimtime.year_time)
and (time_stage.month_time = dimtime.month_time)
and (time_stage.day_time = dimtime.day_time)
);
As I understand you want to add some ID from time_stage table into date_sk column in fact_stage table. I propose to change sql query to something like
update fact_stage set date_sk = (
select ID -- identifier column in time_stage table
from time_stage dim join fact_stage fact
on (dim.year_time = fact.year_time
and dim.month_time = fact.month_time
and dim.day_time = fact.day_time)
-- or instead of ON part use "USING (year_time, month_time, day_time)"
);
hope it helps

Merge Query in DB2

I need to update few columns in one table with the very convoluted calculation.
I'm not good enough in SQL so I tried to use "with" clause in combination with update, but It threw error.
Then I found a post online which suggested to use MERGE so I came up with Merge query. But that one was also throwing an error.
So I removed all other column and updating only one column to remove complexity, but no avail still errors
Below is my query, select query inside working perfectly fine.
Please suggest.
MERGE INTO TABLE_1 AS O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE
FROM TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID= TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
and TABLE_3.Primary_ID=TABLE_1.Primary_ID
and TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE =CORRECT.CORRECT_FLOOR_PRICE
Error is
An error occurred when executing the SQL command:
MERGE INTO ........
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=SELECT;VALUES, DRIVER=3.61.75 [SQL State=42601, DB Errorcode=-199]
Try this instead, I think you forgot your identifier in your select statement because after the "ON" statement "CORRECT.Primary_ID" doesn't associate to anything.
MERGE INTO TABLE_1 as O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER
(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)
-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER
(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE,
TABLE_1.Primary_ID AS Primary_ID
FROM
TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID = TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
AND TABLE_3.Primary_ID=TABLE_1.Primary_ID
AND TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE = CORRECT.CORRECT_FLOOR_PRICE