update from temp table to original table - oracle

I have two tables one is original table and second one is temp table. Temp table having the correct record. Unique column is cust_id. My table structure is
Customer table
cust_id amount
12 100
13 120
14 130
15 250
20 70
25 110
28 900
temp table
cust_id amount
12 300
13 190
14 110
15 240
20 30
25 210
28 500
I want to update the record from temp table to customer orignal table using customer id.

It can be done using merge statement.
merge into original_table ot
using temp_table tp
on (ot.cust_id = tp.cust_id)
when matched
then update set ot.amount = tp. amount

Related

Calculate Months separated (oracle 11G)

Dear Respectful Experts,
We have a table having Column TOTAL_MONTHS e.g. if
As shown below table need to Update using Update Statement.
TOTAL_MONTHS = 15 then Update the Column named "Months<=12" = 12 and Update the Column named "Months>12" = 3
another example in below table is
TOTAL_MONTHS =8 then Update Column named "Months<=12" = 8 and Update Column named "Months>12" = 0
Title
Amount
Total_months
Months<=12
Months>12
10101288
28000
15
12
3
10101289
40000
13
12
1
10101290
2000000
10
10
0
10101291
50000
14
12
2
10101239
6000
11
11
0
10101240
50000
8
8
0
10121003
690
12
12
0
CREATE TABLE "TEST3"
( TITLE VARCHAR2(100 BYTE),
AMOUNT NUMBER,
Total_Months NUMBER,
Months<=12 NUMBER,
Months>12 NUMBER
)
REM INSERTING into TEST3
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101288',28000,15,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101289',40000,13,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101290',2000000,10,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101291',50000,14,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101239',6000,11,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101240',50000,8,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10121003',690,12,null,null);
Somebody can help us please.
Thanks,
Regards,
Out Put Result as below
Title
Amount
Total_months
Months<=12
Months>12
10101288
28000
15
12
3
10101289
40000
13
12
1
10101290
2000000
10
10
0
10101291
50000
14
12
2
10101239
6000
11
11
0
10101240
50000
8
8
0
10121003
690
12
12
0
Use LEAST and GREATEST:
update test3
set "Months<=12" = LEAST(total_months, 12),
"Months>12" = GREATEST(total_months - 12, 0);
The same can be done with CASE expressions of course.
Demo: https://dbfiddle.uk/wB89f9eN
I don't consider it a good idea to store these calculated values in common table columns, by the way. Don't store data redundantly in a database. What would it mean, if some day you find a row containing total_months = 15, "Months<=12" = 12, "Months>12" = 10? Which value would be correct, which not? If you want to see the results like if they were table columns for convenience, use generated columns or a view instead.
If you want to calculate the months as full-years and part-years or as up-to-one year and more-than-one-year then you can calculate those values and do not need to have separate columns in the database that run the risk of becoming out of sync.
If you did want to represent the values in the table then you can use virtual columns:
CREATE TABLE "TEST3"(
TITLE VARCHAR2(100 BYTE),
AMOUNT NUMBER,
Total_Months NUMBER,
Full_Year_Months NUMBER
GENERATED ALWAYS AS (Total_months - MOD(total_months, 12)),
Part_Year_Months NUMBER
GENERATED ALWAYS AS (MOD(total_months, 12)),
"Month>12" NUMBER
GENERATED ALWAYS AS (GREATEST(total_months-12,0)),
"Month<=12" NUMBER
GENERATED ALWAYS AS (LEAST(total_months,12))
);
Which, for the sample data:
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101288', 28000,15);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101289', 40000,13);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101290',2000000,10);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101291', 50000,14);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101239', 6000,11);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101240', 50000, 8);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10121003', 690,12);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('12345678',1234567,45);
Then the table contains:
TITLE
AMOUNT
TOTAL_MONTHS
FULL_YEAR_MONTHS
PART_YEAR_MONTHS
Month>12
Month<=12
10101288
28000
15
12
3
3
12
10101289
40000
13
12
1
1
12
10101290
2000000
10
0
10
0
10
10101291
50000
14
12
2
2
12
10101239
6000
11
0
11
0
11
10101240
50000
8
0
8
0
8
10121003
690
12
12
0
0
12
12345678
1234567
45
36
9
33
12
fiddle

What is the most efficient way to update values of a table based on a mapping from another table

I have a table including following details.
empID department location segment
1 23 55 12
2 23 11 12
3 25 11 39
I also have a mapping table like following
Field old value new value
Department 23 74
department 25 75
segment 10 24
location 11 22
So My task is to replace old values with new values. I can actually use a cursor and update departments first then segments so on and so forth . But that is time consuming and inefficient. I would like to know if there are any efficient way to do this. Which also need to support in future if we were plan to add more columns to the mapping.
cheers.
Check this if it solves the issue.
update emp set department = (select map.new_value from map where emp.department = map.old_value);
How about copying the data to a new table?
CREATE TABLE newemp AS
SELECT e.empid,
NVL(d.new_value, e.department) AS department,
NVL(l.new_value, e.location) AS location,
NVL(s.new_value, e.segment) AS segment
FROM emp e
LEFT JOIN map d ON d.field='DEPARTMENT' AND e.department = d.old_value
LEFT JOIN map l ON l.field='LOCATION' AND e.location = d.old_value
LEFT JOIN map s ON s.field='SEGMENT' AND e.segment = d.old_value
ORDER BY e.empid;
EMPID DEPARTMENT LOCATION SEGMENT
1 84 55 12
2 84 11 12
3 75 11 39
You'll need obviously three passes through the mapping table, but only one pass through the emp table.
We use a LEFT JOIN because not all values will be changed. If no new_value is found, the NVL function uses the existing value of the emp table.
You could update the original table from this new table (if the new table has a primary key):
UPDATE (SELECT empid,
e.department as old_department,
n.department as new_department,
e.location as old_location,
n.location as new_location,
e.segment as old_segment,
n.segment as new_segment
FROM emp e
JOIN newemp n USING (empid))
SET old_department = new_department,
old_location = new_location,
old_segment = new_segment
WHERE old_department != new_department
OR old_location != new_location
OR old_segment != new_segment;

Aggregating the table from many rows to one

I have a table with data as follows:
Name opening receipt transfer closing
abcd 1000 40 30 1010
efg 256 109 219 146
hjk 9356 210 210 9356
mnp 2000
I need is a result like this -
opening_abcd receipt_abcd transfer_abcd closing_abcd opening_efg receipt_efg .....
1000 40 30 1010 256 109 .....
What I have tried till now is to create multiple views one by one having the one row values like -
select opening opening_abcd, receipt receipt_abcd, transfer transfer_abcd, closing closing_abcd
from this_table
where name = 'abcd'
and similarly for others and then I have merged them together by using JOIN.
Is there a solution possible if I can select all of them in one query or any other way which is better than mine?

Hive: Joining two tables with different keys

I have two tables like below. Basically i want to join both of them and expected the result like below.
First 3 rows of table 2 does not have any activity id just empty.
All fields are tab separated. Category "33" is having three description as per table 2.
We need to make use of "Activity ID" to get the result for "33" category as there are 3 values for that.
could anyone tell me how to achieve this output?
TABLE: 1
Empid Category ActivityID
44126 33 TRAIN
44127 10 UFL
44128 12 TOI
44129 33 UNASSIGNED
44130 15 MICROSOFT
44131 33 BENEFITS
44132 43 BENEFITS
TABLE 2:
Category ActivityID Categdesc
10 billable
12 billable
15 Non-billable
33 TRAIN Training
33 UNASSIGNED Bench
33 BENEFITS Benefits
43 Benefits
Expected Output:
44126 33 Training
44127 10 Billable
44128 12 Billable
44129 33 Bench
44130 15 Non-billable
44131 33 Benefits
44132 43 Benefits
It's little difficult to do this Hive as there are many limitations. This is how I solved it but there could be a better way.
I named your tables as below.
Table1 = EmpActivity
Table2 = ActivityMas
The challenge comes due to the null fields in Table2. I created a view and Used UNION to combine result from two distinct queries.
Create view actView AS Select * from ActivityMas Where Activityid ='';
SELECT * From (
Select EmpActivity.EmpId, EmpActivity.Category, ActivityMas.categdesc
from EmpActivity JOIN ActivityMas
ON EmpActivity.Category = ActivityMas.Category
AND EmpActivity.ActivityId = ActivityMas.ActivityId
UNION ALL
Select EmpActivity.EmpId, EmpActivity.Category, ActView.categdesc from EmpActivity
JOIN ActView ON EmpActivity.Category = ActView.Category
)
You have to use top level SELECT clause as the UNION ALL is not directly supported from top level statements. This will run total 3 MR jobs. ANd below is the result I got.
44127 10 billable
44128 12 billable
44130 15 Non-billable
44132 43 Benefits
44131 33 Benefits
44126 33 Training
44129 33 Bench
I'm not sure if I understand your question or your data, but would this work?
select table1.empid, table1.category, table2.categdesc
from table1 join table2
on table1.activityID = table2.activityID;

Hive: Joining tables with different scenarios

I have a question on joining tables in a different scenario. Please find the sample tables below.
Capacity of expected table row 3-5 should be repeated as table 2 does not have those fields.
could anyone please help to get expected table?
Table 1:
No ProjectID Capacity
1 514 4
2 418 10
3 418 30
4 401 40
5 502 41
Table2:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
Expected Table:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
3 418 30 30
4 401 40 40
5 502 41 41
1.Do left outer join
2.For the values not matching take them from table 1 with if condition.
select t1.no,t1.projectid,t1.capacity1,if(t2.capacity2 is null,t1.capacity,t2.capacity)
from table1 t1 left outer join table2 t2 on t1.no=t2.no
I think above query meets your requirement let me know if need any more help.

Resources