Table Column Not Updating - sql

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.

Related

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;

Use as in select statement on select statement

I have a request with a select statement that works in Oracle, but when I execute it in SQL Server it throws an exception; the request is :
SELECT non_existant FROM
(SELECT rownum AS non_existant ,cab, validite FROM tmp_rapprochement)
WHERE validite like '%non_existant%'
The error is :
Msg 207, Niveau 16, État 1, Ligne 2 Nom de colonne non valide : 'rownum'.
Thank you.
The error says that rownum is not a valid column name (I'm glad Google translate is there to help)
Msg 207, Level 16, State 1, Line 2 Invalid column name 'rownum'.
You need to fix that error first - make sure that all columns from the select's list are defined in the table.
Once you get that out of your way, you would need to provide an alias for the inner select, like this:
SELECT non_existant FROM
(SELECT rownum AS non_existant ,cab, validite FROM tmp_rapprochement) x
-- x above is an alias, it is mandatory in SQL Server syntax.
WHERE validite like '%non_existant%'
EDIT It appears that you are porting this query from Oracle, so rownum is not a real column. In this case you should replace it with a row_number function, like this:
SELECT non_existant FROM
(SELECT row_number() OVER (Order by cab) AS non_existant
, cab -- ^^^ Put rapprochement's primary key there
, validite FROM tmp_rapprochement
) x -- x is an alias, it is mandatory in SQL Server syntax.
WHERE validite like '%non_existant%'

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

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

OODB Update with Aggregate Function

I'm trying to update a column in my table "InventoryTable" with the SUM of values returned from Querying my other table "OrderTable"
I found several other questions like this here, and composed this statement:
UPDATE InventoryTable
SET SalesPerMonth = foo.ASPM
FROM InventoryTable
INNER JOIN (
SELECT InventoryID, SUM(Quantity) AS ASPM
FROM OrderTable
GROUP BY InventoryID
) AS foo ON foo.InventoryID = InventoryTable.InventoryID
I'm using this on OpenOffice Base SQL Edit and I keep getting a syntax error:
Syntax Error in SQL Expression
with these details:
SQL Status: HY000 Error code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or SQL_TOKEN_LIKE
I cannot figure out what I am doing wrong.
Thank you.