Update Data from another table with more conditions PL/SQL - oracle

So I have this following table
Table 1
NIP
NAMA_PENSIUN
JNS_KELAMIN
ID_PTKP
NPWP_PENSIUN
195605212010002
Hardjadi
Laki-Laki
K3
939766245522000
195402192010003
Sutikno
Laki-Laki
K1
937896346533000
196008142010004
Adlina Humaira
Perempuan
TK0
937686259522000
196401012010005
Retno Subandi
Laki-Laki
TK2
917678275532000
195908302010006
Baby Fajrina
Perempuan
K3
982638279888000
Table 2
ID_PTKP
KET_PTKP
TARIF_PTKP
TK0
Tidak Kawin - 0 Tanggungan
54,000,000
TK1
Tidak Kawin - 1 Tanggungan
58,500,000
TK2
Tidak Kawin - 2 Tanggungan
63,000,000
TK3
Tidak Kawin - 3 Tanggungan
67,500,000
Table 3
TAHUN_PAJAK
NPWP_PENSIUN
PENGHSL_PENSIUN
NILAI_PTKP
2021
939766245522000
7500000
2021
937896346533000
4500000
2021
937686259522000
4000000
I want to update NILAI_PTKP in the table 3 from TARIF_PTKP table 2, but table 3 has the same identifier which is NPWP_PENSIUN with table 1, and table 1 has the same identifier from table 2 which is ID_PTKP. from this what syntax I should use?

merge is one option:
SQL> merge into table3 c
2 using (select a.npwp_pensiun, b.tarif_ptkp
3 from table1 a join table2 b on a.id_ptkp = b.id_ptkp
4 ) x
5 on (c.npwp_pensiun = x.npwp_pensiun)
6 when matched then update set
7 c.nilai_ptkp = x.tarif_ptkp;
1 row merged.
SQL> select * From table3;
NPWP_PENSIUN NILAI_PTKP
------------------ ----------
939766245522000
937896346533000
937686259522000 54000000
SQL>
Why is only one row updated? Because sample data you posted doesn't have match between table1 and table2.

Already solve with
UPDATE (SELECT pt.NILAI_PTKP A, tp.TARIF_PTKP B FROM PajakTahunan pt
JOIN MasterPensiun mp ON pt.NPWP_PENSIUN = mp.NPWP_PENSIUN
JOIN TarifPTKP tp ON mp.ID_PTKP = tp.ID_PTKP)
SET A = B;

Related

How to write correct left Join of two tables?

I want to join two tables, first table primary key data type is number, and second table primary key data type is VARCHAR2(30 BYTE). How to join both tables.
I tried this code but second tables all values are null. why is that?
SELECT a.act_phone_no,a.act_actdevice,a.bi_account_id, a.packag_start_date, c.identification_number,
FROM ACTIVATIONS_POP a
left JOIN customer c
on TO_CHAR(a.act_phone_no) = c.msisdn_voice
first table
act_phone_no bi_account_id
23434 45345
34245 43556
Second table
msisdn_voice identification_number
23434 321113
34245 6547657
It seems that you didn't tell us everything. Query works, if correctly written, on such a sample data:
SQL> with
2 -- Sample data
3 activations_pop (act_phone_no, bi_account_id) as
4 (select 23434, 45345 from dual union all
5 select 34245, 43556 from dual
6 ),
7 customer (msisdn_voice, identification_number) as
8 (select '23434', 321113 from dual union all
9 select '34245', 6547657 from dual
10 )
11 -- query works OK
12 select a.act_phone_no,
13 a.bi_account_id,
14 c.identification_number
15 from activations_pop a join customer c on to_char(a.act_phone_no) = c.msisdn_voice;
ACT_PHONE_NO BI_ACCOUNT_ID IDENTIFICATION_NUMBER
------------ ------------- ---------------------
23434 45345 321113
34245 43556 6547657
SQL>
What could be wrong? Who knows. If you got some result but columns from the CUSTOMER table are empty (NULL?), then they really might be NULL, or you didn't manage to join rows on those columns (left/right padding with spaces?). Does joining on e.g.
on to_char(a.act_phone_no) = trim(c.msisdn_voice)
or
on a.act_phone_no = to_number(c.msisdn_voice)
help?
Consider posting proper test case (CREATE TABLE and INSERT INTO statements).
You are using Oracle ?
Please check the below demo
SELECT a.act_phone_no, a.bi_account_id, c.identification_number
FROM ACTIVATIONS_POP a
left JOIN customer c
on TO_CHAR(a.act_phone_no) = c.msisdn_voice;
SQLFiddle

delete unique rows from table having minimum value using joins

select a.rowid,a.ta_transaction_at_k,a.ta_approvalid_k
from OF_TATRANSACTIONAPPROVALS a,OF_ATAVAILMENTTICKETS b
where a.TA_TRANSACTION_AT_K=b.at_transaction_k
and a.TA_APPROVALROLE_RO = 98
and a.TA_APPROVALTYPE = 'TA'
and b.AT_GROUP_ID=402
I have this query which gives result in below format. How can I delete records from it
331789 3
331789 4
331789 5
331787 3
331787 4
331787 5
I want to delete ids with minimum values
got the issue resolved using row ids.
delete from OF_TATRANSACTIONAPPROVALS where rowid in(
select rowsid from(
select a.rowid rowsid,a.ta_transaction_at_k trns,a.ta_approvalid_k,row_number() over (partition by TA_TRANSACTION_AT_K order by TA_TRANSACTION_AT_K,ta_approvalid_k) rn
from OF_TATRANSACTIONAPPROVALS a,OF_ATAVAILMENTTICKETS b where a.TA_TRANSACTION_AT_K=b.at_transaction_k
and a.TA_APPROVALROLE_RO = 98 and a.TA_APPROVALTYPE = 'TA' and b.AT_GROUP_ID=402) where rn=1)

Oracle PL/SQL- query a table, selecting data from the same table but utilizing an ID from another

I know the title will not do justice, and I have tried searching around between Joins and Merges, however I am a bit stumped and could use some guidance writing this.
I have two tables:
Table 1
A B C
20348 12306 191
31502 12306
20342 12297 191
31492 12297
20341 12296 191
31504 12296
20344 12299 191
31499 12299
Table 2
A(ident)B (F_Key of T1_A) C D E
25003 20348 1 2 3
35915 20342 1 2 3
41883 20341 1 2 3
31303 20344 1 2 3
I want to take table 2, select the contents B, C, D, E, However I want to select Table 1, column A as B where Table 1 C is null Table 1 B = X.
Table 2 Column B is the foreign Key of Table 1 A
As a result, I would like to see this:
A B C D E
5555 31502 1 2 3
5556 31492 1 2 3
5557 31504 1 2 3
5558 31499 1 2 3
Any help is much appreciated.
So if I understood your question correctly, you need case statement in your select:
select (case when t1.C is null and t1.b=X then t1.A else t2.B end), t2.C, t2.D, t2.E
from Table1 t1 join table2 t2 on t1.A = t2.B
Also not sure what Table 1 B = X means so I just kept the syntax same as in your explanation. Feel free to modify that part. Give it a try to see whether it works for you
I was able to figure this out for what I needed.
I end up going up a further level in my database where I introduced Table 3, where its Primary key was the Foreign Key of Table 1 Column B that referenced a primary key and created:
select t1b.A, t2.C, t2.D, t2.E
from Table1 t1a, table1 t1b, table2 t2, table3 t3
WHERE t3.A = t1a.b
AND t1a.C is null
AND t2.B = t1a.A
AND t1b.B = t3.A
AND t1b.B = 'xxx'

UPDATE with JOIN syntax for Oracle Database

First, I execute the following SQL statements.
drop table names;
drop table ages;
create table names (id number, name varchar2(20));
insert into names values (1, 'Harry');
insert into names values (2, 'Sally');
insert into names values (3, 'Barry');
create table ages (id number, age number);
insert into ages values (1, 25);
insert into ages values (2, 30);
insert into ages values (3, 35);
select * from names;
select * from ages;
As a result, the following tables are created.
ID NAME
---------- ----------
1 Harry
2 Sally
3 Barry
ID AGE
---------- ----------
1 25
2 30
3 35
Now, I want to update increment the age of Sally by 1, i.e. set it to 31. The following query works fine.
update ages set age = age + 1 where id = (select id from names where name = 'Sally');
select * from ages;
The table now looks like this.
ID AGE
---------- ----------
1 25
2 31
3 35
I want to know if there is a way it can be done by joins. For example, I tried the following queries but they fail.
SQL> update ages set age = age + 1 from ages, names where ages.id = names.id and names.name = 'Sally';
update ages set age = age + 1 from ages, names where ages.id = names.id and names.name = 'Sally'
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL> update ages set age = age + 1 from names join ages on ages.id = names.id where names.name = 'Sally';
update ages set age = age + 1 from names join ages on ages.id = names.id where names.name = 'Sally'
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
The syntax of the UPDATE statement is:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm
where dml_table_expression_clause is:
Please pay attention on ( subquery ) part of the above syntax.
The subquery is a feature that allows to perform an update of joins.
In the most simplest form it can be:
UPDATE (
subquery-with-a-join
)
SET cola=colb
Before update a join, you must know restrictions listed here:
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_8004.htm
The view must not contain any of the following constructs:
A set operator
A DISTINCT operator
An aggregate or analytic function
A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause
A collection expression in a SELECT list
A subquery in a SELECT list
A subquery designated WITH READ ONLY
Joins, with some exceptions, as documented in Oracle Database Administrator's Guide
and also common rules related to updatable views - here (section: Updating a Join View):
http://docs.oracle.com/cd/B19306_01/server.102/b14231/views.htm#sthref3055
All updatable columns of a join view must map to columns of a
key-preserved table. See "Key-Preserved Tables" for a discussion of
key-preserved tables. If the view is defined with the WITH CHECK
OPTION clause, then all join columns and all columns of repeated
tables are not updatable.
We can first create a subquery with a join:
SELECT age
FROM ages a
JOIN names m ON a.id = m.id
WHERE m.name = 'Sally'
This query simply returns the following result:
AGE
----------
30
and now we can try to update our query:
UPDATE (
SELECT age
FROM ages a
JOIN names m ON a.id = m.id
WHERE m.name = 'Sally'
)
SET age = age + 1;
but we get an error:
SQL Error: ORA-01779:cannot modify a column which maps to a non key-preserved table
This error means, that one of the above restriction is not meet (key-preserved table).
However if we add primary keys to our tables:
alter table names add primary key( id );
alter table ages add primary key( id );
then now the update works without any error and a final outcome is:
select * from ages;
ID AGE
---------- ----------
1 25
2 31
3 35

how to merge data while loading them into hive?

I'm tring to use hive to analysis our log, and I have a question.
Assume we have some data like this:
A 1
A 1
A 1
B 1
C 1
B 1
How can I make it like this in hive table(order is not important, I just want to merge them) ?
A 1
B 1
C 1
without pre-process it with awk/sed or something like that?
Thanks!
Step 1: Create a Hive table for input data set .
create table if not exists table1 (fld1 string, fld2 string ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
(i assumed field seprator is \t, you can replace it with actual separator)
Step 2 : Run below to get the merge data you are looking for
create table table2 as select fld1,fld2 from table1 group by fld1,fld2 ;
I tried this for below input set
hive (default)> select * from table1;
OK
A 1
A 1
A 1
B 1
C 1
B 1
create table table4 as select fld1,fld2 from table1 group by fld1,fld2 ;
hive (default)> select * from table4;
OK
A 1
B 1
C 1
You can use external table as well , but for simplicity I have used managed table here.
One idea.. you could create a table around the first file (called 'oldtable').
Then run something like this....
create table newtable select field1, max(field) from oldtable group by field1;
Not sure I have the syntax right, but the idea is to get unique values of the first field, and only one of the second. Make sense?
For merging the data, we can also use "UNION ALL" , it can also merge two different types of datatypes.
insert overwrite into table test1
(select x.* from t1 x )
UNION ALL
(select y.* from t2 y);
here we are merging two tables data (t1 and t2) into one single table test1.
There's no way to pre-process the data while it's being loaded without using an external program. You could use a view if you'd like to keep the original data intact.
hive> SELECT * FROM table1;
OK
A 1
A 1
A 1
B 1
C 1
B 1
B 2 # Added to show it will group correctly with different values
hive> CREATE VIEW table2 (fld1, fld2) AS SELECT fld1, fld2 FROM table1 GROUP BY fld1, fld2;
hive> SELECT * FROM table2;
OK
A 1
B 1
B 2
C 1

Resources