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

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'

Related

Update Data from another table with more conditions PL/SQL

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;

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

SQL: Filter self-loops in logistic movements

I'm working on a dataset containing logistic movements of stuff. In visualising the stuff we would like to filter out the movements from e.g. A to A (this madness occurs in the dataset).
Say I have a dataset looking like this: Equipment contains the id of the stuff that is moving, FROM and TO the stockrooms, the TIME_FROM when the stuff has been moved to the FROM stockroom, and TIME_TO when the stuff has been moved to the TO stockroom.
EQUIPMENT FROM_MAG TO_MAG TIME_FROM TIME_TO
1 A B 1 2
1 B C 2 3
1 C D 3 4
1 D D 4 5
1 D E 5 6
1 E F 6 7
1 F F 7 8
1 F F 8 9
1 F G 9 10
Than I would like an output from my query without the D-->D and two F-->F movements, but with a logical continuation of the time columns:
EQUIPMENT FROM_MAG TO_MAG TIME_FROM TIME_TO
1 A B 1 2
1 B C 2 3
1 C D 3 5
1 D E 5 6
1 E F 6 9
1 F G 9 10
I tried using queries like, but that does not give me the desired result. I'm working on SAP HANA by the way.
SELECT
EQUIPMENT,
FROM_MAG,
TO_MAG,
min(TIME_FROM),
max(TIME_TO)
FROM MOVEMENTS
GROUP BY EQUIPMENT,
FROM_MAG,
TO_MAG;
Create statement for SQL:
CREATE TABLE IF NOT EXISTS MOVEMENTS(
EQUIPMENT NVARCHAR(1) NOT NULL PRIMARY KEY
,FROM_MAG NVARCHAR(1) NOT NULL
,TO_MAG NVARCHAR(1) NOT NULL
,TIME_FROM NVARCHAR(1) NOT NULL
,TIME_TO NVARCHAR(2) NOT NULL
);
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'A',N'B',N'1',N'2');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'B',N'C',N'2',N'3');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'C',N'D',N'3',N'4');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'D',N'D',N'4',N'5');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'D',N'E',N'5',N'6');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'E',N'F',N'6',N'7');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'F',N'F',N'7',N'8');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'F',N'F',N'8',N'9');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'F',N'G',N'9',N'10');
Checked your query, I don't think there is need for FROM_MAG in GROUP BY. I checked this in MySql, giving desired result.
SELECT
EQUIPMENT,
FROM_MAG,
TO_MAG,
min(TIME_FROM),
max(TIME_TO)
FROM MOVEMENTS
GROUP BY EQUIPMENT,
TO_MAG
I used this, and now it works:
WITH A AS(
SELECT EQUIPMENT,
FROM_MAG,
TO_MAG,
TIME_FROM,
TIME_FROM,
TIME_TO
FROM MOVEMENTS
WHERE FROM_MAG<>TO_MAG
ORDER BY TO_NUMBER(TIME_TO))
SELECT EQUIPMENT,
FROM_MAG,
TO_MAG,
TIME_FROM,
IFNULL(LEAD(TIME_FROM) OVER(PARTITION BY EQUIPMENT ORDER BY TO_NUMBER(TIME_TO)),
TIME_TO) TIME_TO
FROM A;

Complex Networks in Hive - Optimization Code

I have a problem with how to get my Hive code optimized.
I have a huge table as follows:
Customer_id Product_id Date Value
1 1 02/28 100.0
1 2 02/02 120.0
1 3 02/10 144.0
2 2 02/15 120.0
2 3 02/28 144.0
... ... ... ...
I want to create a complex network where I link the products through the buyers. The graph does not have to be directed and I have to count the number of links between them.
In the end I need this:
Product_x Product_y amount
1 2 1
1 3 1
2 3 2
Can anyone help me with this?
I need an optimized way to do this. The join of the table with itself is not the solution. I really need an optimum way on this =/
CREATE TABLE X AS
SELECT
a.product_id as product_x,
b.product_id as product_y,
count(*) as amout
FROM table as a
JOIN table as b
ON a.customer_id = b.customer_id
WHERE a.product_id < b.product_id
GROUP BY product_x, product_y;

How to select two max value from different records that has same ID for every records in table

i have problem with this case, i have log table that has many same ID with diferent condition. i want to select two max condition from this. i've tried but it just show one record only, not every record in table.
Here's my records table:
order_id seq status____________________
1256 2 4
1256 1 2
1257 0 2
1257 3 1
Here my code:
WITH t AS(
SELECT x.order_id
,MAX(y.seq) AS seq2
,MAX(y.extern_order_status) AS status
FROM t_order_demand x
JOIN t_order_log y
ON x.order_id = y.order_id
where x.order_id like '%12%'
GROUP BY x.order_id)
SELECT *
FROM t
WHERE (t.seq2 || t.status) IN (SELECT MAX(tt.seq2 || tt.status) FROM t tt);
this query works, but sometime it gave wrong value or just show some records, not every records.
i want the result is like this:
order_id seq2 status____________________
1256 2 4
1257 3 2
I think you just want an aggregation:
select d.order_id, max(l.seq2) as seq2, max(l.status) as status
from t_order_demand d join
t_order_log l
on d.order_id = l.order_id
where d.order_id like '%12%'
group by d.order_id;
I'm not sure what your final where clause is supposed to do, but it appears to do unnecessary filtering, compared to what you want.

Resources