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
Related
I am trying to execute this query but it doesn't work:
SELECT COLUMN
FROM TABLE A
WHERE A.COLUM_1 = '9999-12-31' AND NOT EXISTS (SELECT 1 FROM TABLE2 ET WHERE ET.COl1 = A.COL2 LIMIT 1)
It results in an error which says the following:
"mismatched input FROM expecting"
Went through this post as it states its supported by Spark with 2.0+ version.
I'm not sure that SparkSQL supports TOP. But it is not needed. Does this work?
SELECT t.COLUMN
FROM TABLE t
WHERE t.COLUM_1 = '9999-12-31' AND
NOT EXISTS (SELECT 1 FROM TABLE2 ET WHERE ET.COl1 = t.COL2);
This fixes a few other syntax issues with the query (such as no alias A).
LIMIT in the subquery is also not needed. NOT EXISTS should stop at the first match.
The following thread successfully showed how to use a an UPDATE SET and FROM clause together, to update an entire row of a specific column with a value derived from a different table.
When executing following expression in Oracle SQL:
UPDATE territory2_t
SET total_sales_person = t.total_count
FROM (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
WHERE territoryid = t.salesterritoryid;
Oracle states: SQL Error: ORA-00933: SQL command not properly ended
In Oracle you can use merge to do the job:
merge into territory2_t
using (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
on (territoryid = t.salesterritoryid)
when matched then
update SET total_sales_person = t.total_count
This can be done with MERGE (in Oracle and most other DB systems - those that implement the MERGE statement from the SQL Standard). MERGE is the preferred solution in most cases.
There is a misconception that this cannot be done with an UPDATE statement in Oracle. I show below how it can be done - not to encourage its use (MERGE is better), but to show that UPDATE can be used as well. This is the transformation of the Postgre SQL the OP solicited in the original post.
update ( select t2.total_sales_person, t.total_count
from territory2_t t2 inner join
( select salesterritoryid, count(*) as total_count
from salesperson_t
group by salesterritoryid
) t
on t2.territoryid = t.salesterritoryid
)
set total_sales_person = total_count;
I am currently taking my first course in SQL and have encountered a bit of a problem. I will now try to explain what I am trying to do. I have this select statement which properly displays what I need. MY problem arises when I try to convert it into an UPDATE statement instead.
SELECT infobb02.uni+tempbb02.sal
from infobb02 JOIN tempbb02 ON infobb02.empno=tempbb02.empno;
In case its not obvious im adding value of uni from table infobb02 to sal in table tempbb02. I have tried all sorts of things to get it to be a permanent update but keep getting errors mostly
"SQL command not properly ended"
Any help is appreciated! Thanks.
Assuming that your query is:
SELECT i.uni + t.sal
FROM infobb02 i JOIN
tempbb02 t
ON i.empno = t.empno;
If you want to update tempbb02, then:
update tempbb02 t
set t.sal = t.sal +
(select i.uni from infobb02 i where i.empno = t.empno)
where exists (select 1 from infobb02 i where i.empno = t.empno);
Instead of using an UPDATE statement, you could use a MERGE:
merge into tempbb02 tgt
using infobb02 src
on (tgt.empno = src.empno)
when matched then
update set tgt.sal = tgt.sal + src.uni;
I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234
Can anyone please point out what is wrong with the following SQL statement:
SELECT DiaryType
FROM tblDiaryTypes
WHERE DiaryType NOT IN (SELECT NextDiary
FROM tblActionLinks
WHERE HistoryType = 'Info Chased');
Now the nested SELECT statement currently returns NULL because there are initially no entries in tblActionLinks, and I am wondering if that is the issue.
The outer SELECT statement if executed on its own does return all the Diary Types from tblDiaryTypes as expected. But when I add the nested SELECT statement to exclusde certain values, then the overall SQL statement returns empty!
Does this have something to do withthe fact that tblActionLinks is currently empty? If so, how can I amend my SQL statement to handle that possibility.
For SQL SERVER (you didn't specified sql engine) try with:
SELECT ISNULL(NextDiary, 0) ...
When no rows found all value is null then it will return 0
Are you sure there are no entries currently in tblActionLinks? If there are no entries in tblActionLinks, then outer query should return all records
Does this have something to do withthe fact that tblActionLinks is currently empty?
Yes... NULL doesn't being handled so good in SQL, Comparing a value to NULL is undifned try give for null a flag value like -999:
SELECT DiaryType
FROM tblDiaryTypes
WHERE DiaryType NOT IN (SELECT NVL(NextDiary, -999) -- <===
FROM tblActionLinks
WHERE HistoryType = 'Info Chased');
NVL(NextDiary, -999) means that if NextDiary IS NULL, replace the value with -999
docs
I would rewrite your query the following way:
SELECT DiaryType
FROM tblDiaryTypes
WHERE NOT EXISTS (SELECT NextDiary
FROM tblActionLinks
WHERE HistoryType = 'Info Chased'
AND NextDiary = DiaryType)
This ensures proper behaviour irrespective of ANSI_NULLS setting and you don't have to worry about properly choosing the magic value returned by ISNULL(NextDiary, 0) (what if you have DiaryType equal to 0 in tblDiaryTypes?)