sql to update table for multiple columns.? - sql

I have an EMP table with columns like id,name,job,salary,age,doj.
This EMP table has 10 records, with IDs id1,id2....id10.
I need a DML statement to update names for 4 IDs ( id2,id4, id6, id8).
This requires multiple where conditions.
Such as -
name=a where id=id2
name=b where id=id4.
Please suggest.

You can use a case expression. That should look like:
update emp
set name = case id
when 'id2' then 'a'
when 'id4' then 'b'
when 'id6' then 'c'
when 'id8' then 'd'
end
where id in ('id2', 'id4', 'id6', 'id8')

Related

Using Insert/Update with 2 Tables - Oracle Apex

I'm still new to SQL and am having a hard time figuring out how to update and insert from one table into another table in Oracle Apex (uses SQL).
The two tables are named Temp and Table (example) and both have the same columns. Basically they are copies of each other but with different data. I would like to compare the ID field in Temp to the ID field in Table and if there is a row that matches that ID field in Temp, then to overwrite all the data on the row in Table with the data in the corresponding row in Temp.
Note: Table has 10 million rows of data, Temp has like 500.
if Temp.ID = Table.ID then
update
set
Table.ID = Temp.ID
Table.Address = Temp.Address
else
insert
(Table.ID,
Table.Address)
values
(Temp.ID,
Temp.Address
That is basically what I want done but not sure how to write it in SQL. Have seen a lot of different answers, but none really involving 2 tables and mostly for MySQL or SQL Server specific SQL that I am not sure also works with Oracle.
MERGE into TEST1
USING TEST2 on (TEST2.ID = TEST1.ID)
WHEN matched THEN UPDATE
SET TEST1.ID = TEST2.ID, TEST1.NAME = TEST2.NAME, TEST1.ADDRESS = TEST2.ADDRESS, TEST1.EMAIL = TEST2.EMAIL
WHEN not matched THEN INSERT (ID, NAME, ADDRESS, EMAIL) values (TEST2.ID, TEST2.NAME, TEST2.ADDRESS, TEST2.EMAIL);
I tried this but it is giving me the error:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "TEST1"."ID"
UPDATE: Got it to work!
http://db-oriented.com/2013/09/20/the-merge-statement/
Was helpful with the answer to the error and I did not know that you could not have the IDs in the update clause, which makes total sense now. Thank you also to the respondent below for explaining the merge code to me. :)
Looks like a good candidate for MERGE. Have a look at the example.
Sample tables & data:
SQL> create table ttable (id number, address varchar2(20));
Table created.
SQL> create table temp (id number, address varchar2(20));
Table created.
SQL> insert into ttable
2 select 1, 'Address 1' from dual union all
3 select 2, 'Address 2' from dual union all
4 select 3, 'Address 3' from dual;
3 rows created.
SQL> insert into temp
2 select 1, 'New address 1' from dual union all
3 select 2, 'New address 2' from dual union all
4 select 4, 'New address 4' from dual;
3 rows created.
Merge & the result:
SQL> merge into ttable a
2 using temp e on (e.id = a.id)
3 when matched then update set a.address = e.address
4 when not matched then insert (id, address) values (e.id, e.address);
3 rows merged.
SQL> select * from ttable;
ID ADDRESS
---------- --------------------
1 New address 1
2 New address 2
3 Address 3
4 New address 4
SQL>

hive scan and select in one query

I have a hive table, say emp_details(name, dept).
In this table, I need to check if any records exists with dept = ‘a’ then select those records. If no such record is found then only I will choose records with dept = ‘b’. The source data has either 'a' or 'b' as dept value and my result set will contain either 'a' or 'b' not both.
The problem is I am bound to use only one hive query for this issue.
Calculate a_exist flag and use it for filtering:
select name, dept
from
(select name,
dept,
(count(case when dept='a' then 1 end) over()>0) as a_exist
from test_a
)a
where (a_exist and dept='a') --only a if exists
or ((NOT a_exist)and dept='b') --return b if a not exists
;

how to update multiple rows in oracle

I would like to update multiple rows with different values for all different records, but don't have any idea how to do that, i am using below sql to update for single record but i have 200 plus records to update
update employee
set staff_no = 'ab123'
where depno = 1
i have 50 dep and within those dep i need to update 200 plus staff no. any idea.
At the moment if i just do a
select * from Departments
i can see list of all employee which needs staff no updating.
UPDATE person
SET staff_no =
CASE person_no
WHEN 112 THEN 'ab123'
WHEN 223 THEN 'ab324'
WHEN 2343 THEN 'asb324'
and so on.....
END
You should be able to use MERGE statement to do it in a single shot. However, the statement is going to be rather large:
MERGE INTO employee e
USING (
SELECT 1 as d_id, 'cd234' as staff_no FROM Dual
UNION ALL
SELECT 2 as d_id, 'ef345' as staff_no FROM Dual
UNION ALL
SELECT 3 as d_id, 'fg456' as staff_no FROM Dual
UNION ALL
... -- More selects go here
SELECT 200 as d_id, 'za978' as staff_no FROM Dual
) s
ON (e.depno = S.d_id)
WHEN MATCHED THEN UPDATE SET e.staff_no= s.staff_no
use a case expression
UPDATE employee
SET staff_no =
CASE depno
WHEN 1 THEN 'ab123'
WHEN 2 THEN 'ab321'
--...
ELSE staff_no
END
WHERE depno IN ( 1, 2 ) -- list all cases here. use a subquery if you don't want to / cannot enumerate
For conditional update, you could use multiple update statements, or use CASE expression in the SET clause.
Something like,
UPDATE table
SET schema.column = CASE
WHEN column1= 'value1' AND column2='value2' THEN
'Y'
ELSE
'N'
END
I wish you tried to search for a similar question on this site, there was a recent question and this was my answer.
If you have two tables like:
CREATE TABLE test_tab_1 (id NUMBER, name VARCHAR2(25));
CREATE TABLE test_tab_2 (id NUMBER, name VARCHAR2(25));
You can use UPDATE statement as below:
UPDATE test_tab_1
SET test_tab_1.name = (SELECT test_tab_2.name FROM test_tab_2
WHERE test_tab_1.id = test_tab_2.id);

Create SQL query with given Conditions

I have an SQL query of the form
SELECT
SUM(some_col)
FROM
(some table name here)
WHERE
some_common_condition AND
some_condition1 AND
some_condition2 AND
column_A = 'X'
GROUP BY
column_A;
column_A has some values for which I currently run this query for. Now for each of the unique values of column_A there are some associated conditions.
For eg, if column_A has 3 unique values X,Y,Z, all three have some_common_condition and some specific conditions associated with it,i.e,
for X -> some_condition1 AND some_condition2
for Y -> some_condition3 AND some_condition4
for Z -> some_condition5 AND some_condition6
I want to club all these conditions and make 1 single SQL query,which gives me corresponding SUM(some_col) for all the values of column_A (i.e. X,Y,Z).
Now one obvious way is
SELECT *
FROM(SELECT SUM(some_col) AS val_X FROM (table) WHERE conditions_for_X UNION ALL
SELECT SUM(some_col) AS val_Y FROM (table) WHERE conditions_for_X UNION ALL
SELECT SUM(some_col) AS val_Z FROM (table) WHERE conditions_for_X -- UNION ALL a.s.o.
)
I want to know a solution which only hits the database once. Above query uses 3 select statements, hitting the DB three times, which is bad for scalability reasons.
Does this answer your question?
SELECT SUM(some_col) FROM ...
WHERE
(column_A = 'X' AND some_condition1 AND some_condition2) OR
(column_A = 'Y' AND some_condition3 AND some_condition4) OR
(column_A = 'Z' AND some_condition5 AND some_condition6)
GROUP BY column_A;

Copy column from one database to another and insert data depends on condition in SQL Server

I have two databases on the same SQL server. The first is named Aa with a table Models in which there are two columns: Id and Desc and Bb database with a table ListOfModels in which there are three columns: Id, MachineTypeId, ModelName. I need to copy all Desc values from Aa database into ModelName in Bb and insert 1 into MachineTypeId if Desc starts with "K", otherwise insert 2.
Can you please help me write the script for this?
The question is unclear as to whether you want to insert new rows into the table or just update matching values.
If you actually want to insert records:
insert into bb..ListOfModels(MachineTypeId, ModelName)
select (case when TypeId like 'K%' then 1 else 2 end), m.[desc]
from aa..Models m;
If you want to update the records based on matching by id:
update lom
set ModelName = [desc],
MachineTypeId = (case when m.TypeId like 'K%' then 1 else 2 end)
from bb..ListOfModels lom join
aa..Models m
on lom.id = m.id;
By the way, desc is a lousy name for a column, because it is a reserved word in SQL.
Use a case statement: http://msdn.microsoft.com/en-us/library/ms181765.aspx
Case Substr(MachineTypeID,1,1) When "K" Then 1 Else 2 End