Creating master ID in Oracle - sql

Let's say, I have a data table with below columns:
EMAIL PHONE IP
123#ABC.COM 123456 0.0.0.0
123#ABC.COM 123456 0.0.0.1
123#ABC.COM 654321 0.0.0.0
321#ABC.COM 123456 0.0.0.0
000#ABC.COM 000000 1.1.1.1
000#ABC.COM 000001 1.1.1.2
I want to create a table on top of that data table, which has the master ID. The rule is as long as one of each attribute (Email, Phone, IP) is matched then they will have the same master ID.
For example:
EMAIL PHONE IP Master_ID
123#ABC.COM 123456 0.0.0.0 1
123#ABC.COM 123456 0.0.0.1 1
123#ABC.COM 654321 0.0.0.0 1
321#ABC.COM 123456 0.0.0.0 1
000#ABC.COM 000000 1.1.1.1 2
000#ABC.COM 000001 1.1.1.2 2
Is there any suggestion on how can I get it done by just using query/function in Oracle?

Related

SQL Query Multiple Joins to same table

I have the following table
Address Table
Code Company Name Type Address Line 1 Address Line 2 City State ZIP
ABC01 ABC Company B 123 Bill To 1 Bill to Apt 2 Newark NJ 12345
ABC01 ABC Company S 425 Ship To 1 Ship To Line 2 Edison NJ 44445
DEF01 DEF Bill To B 993 Bill To 1 Bill to Apt 2 Newark NJ 12345
DEF01
DEF Ship To S 456 Ship To 1 Ship To Line 2 Edison NJ 44445
Invoice Table
Invoice # Code Bill To Name Ship to Name
12345 ABC01 ABC Company ABC Company
12346 DEF01 DEF Bill To DEF Ship To
I need the following results
Invoice # Code Bill To Name Ship To Name Bill to Address Line 1 Ship to Address Line 1
12345 ABC01 Bill To 1 Ship To 1 123 Bill To 1 425 Ship To 1
12346 DEF01 DEF Bill To DEF Ship To 993 Bill to 1 456 Ship To 1
Basically I need to join the Invoice table to the address table based on the code + Company Name + Type in 2 different joins.
I am stuck with the query and cant figure how to join them together.
for answering your question you can use the following query to get the required result :
SELECT invoice.`Invoice #`,invoice.Code,
ba.`Company Name` as "Bill to Name" ,sa.`Company Name` as "Ship to Name" ,
ba.`Address Line 1` as "Bill to Address Line 1", sa.`Address Line 1`
FROM `invoice`
JOIN address as ba on ba.`Company Name` = invoice.`Bill To Name` and ba.Type = 'B'
JOIN address as sa on sa.`Company Name` = invoice.`Bill To Name` and sa.Type = 'S';

Select Duplicated payments and invoices

Few months ago we had a bug with our payments.
What happened was our system somehow made duplicated payments from salaries and invoices and sent them to bank
Salaries have the same user_salary_id in 2 different rows if they are duplicated, and invoice payments happened 2 times with different ref_id
Total of 4 salaries paid should only be 2
View from v_user_ledger_salary
user_profile_id
user_salary_id
requested_salary
user_ledger_id
1111
32500
144.00
1
1111
32500
144.00
2
2222
32600
96.00
3
2222
32600
96.00
4
2324
32603
245.00
5
45423
32606
978.49
6
11151
32610
38.65
7
20000
32611
100.12
8
But invoices have a different reference number, reference numbers are "neighbors", with the same amount, meaning they are also duplicated
View from v_user_ledger
user_profile_id
ref_type
ref_id
credit
1111
INVOICE_PAYMENT
999
100.00
1111
INVOICE_PAYMENT
1000
100.00
2222
INVOICE_PAYMENT
1049
320.50
2222
INVOICE_PAYMENT
1050
320.50
2324
INVOICE_PAYMENT
1051
666.23
45423
INVOICE_PAYMENT
1052
320.50
11151
INVOICE_PAYMENT
1053
10.64
20000
INVOICE_PAYMENT
1538
1392.93
View of t_invoice
id(same as invoice_id)
biller_id(same as user_profile_id)
234534
1111
235000
2222
235010
2324
235020
45423
235033
11151
235444
20000
View of t_invoice_payment
id(same as ref_id)
invoice_id
amount
999
234534
100.00
1049
235000
320.50
1051
235018
666.23
1052
235025
320.50
1053
235043
10.64
1538
235444
1392.93
What I need is a query that will SELECT * FROM v_user_ledger_salary WHERE rows have same user_salary_id but different user_ledger_id AND user_profile_id in (list)
This should return :
user_profile_id
user_salary_id
requested_salary
user_ledger_id
1111
32500
144.00
1
1111
32500
144.00
2
2222
32600
96.00
3
2222
32600
96.00
4
And from invoices SELECT * FROM v_user_ledger WHERE ref_id IN (SELECT id FROM t_invoice_payment WHERE invoice_id IN(SELECT id FROM t_invoice WHERE biller_id IN (list))) but ref_id needs to have same credit AND "neighbor" of the other ref_id AND user_profile_id IN (list)
This should return :
user_profile_id
ref_type
ref_id
credit
1111
INVOICE_PAYMENT
999
100.00
1111
INVOICE_PAYMENT
1000
100.00
1111
INVOICE_PAYMENT
1049
320.50
1111
INVOICE_PAYMENT
1050
320.50
What I have is the list of user_profile_id 's that might have duplicated salaries/invoices
Tables that are used t_invoice_payment, t_invoice
Views that are used v_user_ledger, v_user_ledger_salary
"Why is this a public question?" I have no idea how to search or in which words to search the answer for my problem, so please if you know how to solve this, or you have a link to other question similar to this one send it to me

SQL combine two rows with common batch number into one line

I have an Oracle SQL (select *) query that spools batch transactional information, the result of which outputs the main sender and receivers to different lines for the same transaction like below:
BATCH_NO DATE AMOUNT SENDER_ACCT_NO SENDER NAME RECEIVER_ACCT_NO RECEIVER NAME
12345678 15-May-20 103.55 400001000 ENDCORP (null)
12345678 15-May-20 34.55 400001000 (null) 78909909 JOHN SMITH
12345678 15-May-20 44.00 400001000 (null) 78853229 HENRY FAGIL
12345678 15-May-20 4.00 400001000 (null) 76849852 BENJAMIN HOOK
12345678 15-May-20 21.00 400001000 (null) 72555099 FELIX MAGS
12222222 16-May-20 1902.00 400000105 ZENDCORP (null)
12222222 17-May-20 1899.00 400000105 (null) 90333300 KEN GOTL
12222222 18-May-20 3.00 400000105 (null) 90127765 JONES MAST
In this case, ENDCORP and ZENDCORP sent out batch payments of a total of 103.55 and 1902.00 to 4 and 2 clients respectively.
However, I would prefer my results to be in the form below:
BATCH_NO DATE AMOUNT SENDER_ACCT_NO SENDER NAME RECEIVER_ACCT_NO RECEIVER NAME
12345678 15-May-20 34.55 400001000 ENDCORP 78909909 JOHN SMITH
12345678 15-May-20 44.00 400001000 ENDCORP 78853229 HENRY FAGIL
12345678 15-May-20 4.00 400001000 ENDCORP 76849852 BENJAMIN HOOK
12345678 15-May-20 21.00 400001000 ENDCORP 72555099 FELIX MAGS
12222222 17-May-20 1899.00 400000105 ZENDCORP 90333300 KEN GOTL
12222222 18-May-20 3.00 400000105 ZENDCORP 90127765 JONES MAST
I would appreciate guidance on how to formulate my query for this result
I'm assuming when you say the query is select * that you are showing us the table structure. Otherwise we'd need more detail of the tables and your query.
If it's one table, you just need to select where sender name is null and join to the same table on sender acct no and sender name is not null, selecting appropriate columns from each relation.
I think you can use window functions to impute the name and then filter:
select . . . -- columns you want
from (select t.*,
max(sender_name) over (partition by batch_no) as imputed_sender_name
from t
) t
where RECEIVER_ACCT_NO is not null;

Query 1 table with multiple criteria from different rows that are dependent upon each other

ID segment Type start date end date added date
----------------------------------------------------------------
12345 10 2222 20170101 20200101 20180901
12345 20 2222 20140101 20160101 20150901
12345 50 4444 20170301 20200101 20180901
12345 60 4444 20140101 20160101 20150901
56789 4 2222 20170101 20200101 20180901
56789 6 2222 20140101 20160101 20150901
56789 10 3333 20170301 20200101 20180901
56789 56 3333 20140101 20160101 20150901
56789 7 4444 20110301 20120101 20180901
56789 12 4444 20100101 20100301 20150901
I am having a hard time querying data in 1 table where I need an ID depending on data in a different row in the same table. I am new to Oracle SQL.
I will have the following:
type
added date
I need to know which ID and segment has type 2222 added on the 20180901 with a start date before 20180901. Also for this ID there is a type 4444 with a start date before 20180901 and after the start date of 2222 segment.
In this example it would only be the first line ID - 12345 Segment -10.
Since the following:
The ID has a '2222' segment that has an added date that equals
The ID has a '4444' with a start date between the above '2222' segment start date and added date.
Thanks in advance for your help.
Truly,
Wendy
You don't post your DDL CREATE TABLE ... SQL statements, so I can only guess on the exact table name and column names/types. The query you are looking for should be similar to:
select
*
from my_table t1
where type = 2222
and added_date = date '2018-09-01'
and start_date < date '2018-09-01'
and exists (
select 1 from my_table t2
where t2.id = t1.id
and t2.type = 4444
and t2.start_date < date '2018-09-01'
and t2.start_date > t1.start_date
)

How to get a result for the below table using teradata

I am new to Teradata. I have created the table with empno, mobile and office no as follows. I need mobile and office no in separate column.
empno mobile office
111 9999 4444
222 null 3333
333 5555 7896
444 6666 null
I need to have output as:
111 9999
111 4444
222 3333
333 5555
333 7896
444 6666
SELECT empno, mobile FROM t WHERE mobile IS NOT NULL
UNION ALL
SELECT empno, office FROM t WHERE office IS NOT NULL
ORDER BY empno;
This is not Teradata-specific, works for any SQL database.