Beginning SQL - adding the values of one column to another from separate tables - sql

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;

Related

Find multiple SQL columns and update based on defined data listed in query

I have an update query in which I am trying to locate data in a column from a single table. All while taking other defined data listed in the query to update another column in the same table once a match has been found with that original search. Below is an example of my update statement. My end goal is to find '003447710' then update AltId to '540112'
UPDATE Site
SET AltId = ('540112'
'540129'
'540142'
'540143')
WHERE CCMFStatus in ('003447710',
'002754540',
'003564370',
'005942870')
I am sure there may already be something like this out there but I am really having trouble on an easy method on how to do this quickly and accurately.
Try this
update site
set altid = a.altid
from
(select altid,CCMFstatus from site) as a
where site.CCMFstatus = a.CCMFstatus
The best way might be multiple update statements:
UPDATE Site
SET AltId = '540112'
WHERE CCMFStatus = '003447710';
And so on.
If not, you can do this with a giant case statement or a join:
WITH values as (
SELECT '003447710' as oldstatus, '540112' as newaltid UNION ALL
SELECT '002754540', '540129' UNION ALL
SELECT '003564370', '540142' UNION ALL
SELECT '005942870', '540143'
)
UPDATE s
SET AltId = va.newaltid
FROM site s JOIN
values v
ON s.CCMFStatus = v.oldstatus;
If you already have the values in a table, then you don't need the WITH statement. You can just use the table.
Have you tried using CASE statement?
UPDATE SITE SET AltID = (CASE
WHEN CCMFStatus = '003447710' THEN '540112'
WHEN CCMFStatus = '002754540' THEN '540129'
END)
WHERE
CCMFStatus in ('003447710', '002754540', '003564370', '005942870');
BR,

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

sql server if statement not working

Can anyone advise me as to what is wrong with the following SQL server update statement:
IF (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT') = ''
GO
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID = (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
You don't need an if statement - you can just run the update statement, and if the subquery returns no rows, no rows will be updated. The if won't really save anything - you're performing two queries instead of one.
You either want
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID In (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
if there are multiple ID's with that code OR use
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE code = 'SOUND_WRONG_GARMENT'
either way and lose the IF statement as #Mureinik said.
Although Mureinik's answer is the logical solution to this, I will answer why this isn't actually working. Your condition is wrong, and this approach will work instead using IF EXISTS:
IF EXISTS (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT')
BEGIN
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID IN (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
END
As a side note, you're using an = sign instead of IN, which means you'll be matching to an arbitrary singular ID and only update 1 row based on this. To use a set based operation, use the IN clause.
You could actually 'golf' this by doing away with the derived query altogether, and using a simple WHERE code='SOUND_WRONG_GARMENT' on the table you're updating on.

Alternative update SQL query

I am looking for an alternative for this query. I know that such query will end up with invalid identifier in Oracle. So please give me the same query for updating one filed from another field in another table.
update RBT_CMP_RECOM_9304
set FIRST_RECOM_NAME=(select rbt_cmp_base_code.RBT_NAME
from rbt_cmp_base_code
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM)
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM;
FYI:
RBT_CMP_RECOM_9304=(firt_recom,first_recom_name)
RBT_CMP_BASE_CODE = (rbt_code, rbt_name)
I get this error when I try it:
ORA-00904: RBT_CMP_BASE_CODE.RBT_CODE: invalid identifier
Regards.
One way is to repeat the subquery:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select bc.RBT_NAME
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
EDIT:
If you are getting an error that more than one row is returned, then you have to decide which value. Nothing in your code suggests that this might be an issue (hint: sample data and desired results always help a question).
The easiest solution is to use and aggregation function:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select max(bc.RBT_NAME)
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
But you might want to fix the rbt_cmp_base_code table so it doesn't have duplicates. From the table name, it sounds like there should be one row per code.

Update table from another table for certain fields

I need you help in updating changes a table.
the table i want to update is IP_VISIT_EASTERN_REGION, the field I want to update is VISIT_STAT
so I wrote this query
UPDATE IP_VISIT_EASTERN_REGION SET
IP_VISIT_EASTERN_REGION.VISIT_STAT=TEST_IP_VISIT_EASTERN_REGION.VISIT_STAT
WHERE IP_VISIT_EASTERN_REGION.VISIT_ID = TEST_IP_VISIT_EASTERN_REGION.VISIT_ID
The Issue is I get an error
ORA-00904 INVALID IDENTIFIER
and is this the correct way to do it.
your help will be really appreciated.
thank you
You can't write an UPDATE from a SELECT by using a join. Try this approach instead.
update set from inner join query thowing error
In your case:
UPDATE IP_VISIT_EASTERN_REGION REGION
SET REGION.VISIT_STAT= ( SELECT TEST.VISIT_STAT
FROM TEST_IP_VISIT_EASTERN_REGION TEST
WHERE REGION.VISIT_ID = TEST.VISIT_ID )
WHERE EXISTS ( SELECT TEST.VISIT_STAT
FROM TEST_IP_VISIT_EASTERN_REGION TEST
WHERE REGION.VISIT_ID = TEST.VISIT_ID );
Fiddle:
http://sqlfiddle.com/#!4/2178ae/1