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.
Related
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.
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;
I am trying to create a Pivot in Oracle. I keep getting the error message
ORA-00904: "VALUEZ": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 18 Column: 6
Any thoughts?
SELECT * FROM
(
SELECT ACC.NBR,CTA.NAMEZ
FROM ACCS ACC
JOIN CARS CAR ON CAR.CAR_AAD_ID = ACC.ACC_AAD_ID
JOIN CTAS CTA ON CAR_CUS_ID = CTA_CUS_ID
)
PIVOT
(
MAX(VALUEZ) --comes from table CTAS (ERROR LINE)
FOR NAMEZ IN ('1','2','3') --from table CTAS
)
ORDER BY ACC.NBR;
As a side note, I would love it if it was possible to turn the ('1','2','3') into a subquery, but it looks like that is not possible from other post i have read. If it was easy it would be (select distinct namez from CTAS)
The columns referenced in your PIVOT clause must exist in the row source that is being pivoted. You select ACC.NBR,CTA.NAMEZ from the table; it looks like you need to expand that to ACC.NBR,CTA.NAMEZ,CTA.VALUEZ.
You cannot use a subquery to replace the list of pivot values. I believe the underlying reason for this is that the parser must be able to figure out the columns that the query will produce prior to executing it; so the pivot values must be hardcoded.
What you might be able to do, if it is appropriate to wrap this query up in a procedure or function, is to first execute a query to get the list of pivot values, then build the pivot query string using that information and execute it via dynamic SQL.
ora-01748: only simple column names allowed here pivot
select* from (SELECT TRUNC(I.POST_DATE) DATES FROM INVOICE I
INNER JOIN INVC_TENDER_V T ON T.INVC_SID=I.INVC_SID)
PIVOT
(COUNT(*)
FOR T.TENDER_TYPE IN(0,1) )
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
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