Java - Merge data from 2 tables in different data source - java

I have a use case, where I have to merge data from 2 Tables (which are in different data sources).
Consider that each table has employee data (First name, last name, phoneNumber). For each employee, I will have to merge the phoneNumber data from both the Tables and other data will remain the same as in Table 1
If employee data is present in both Table 1 and Table 2, phoneNumber data will be merged as a comma separated values and other data will be sourced from Table 1
If employee is present only in Table 1, then entire data will be sourced from Table 1
If employee is present only in Table 2, then entire data will be sourced from Table 2
These Tables has about 40 lack rows of data each. Close to 5GB.
What is the best approach to do this in Java? My concern is if I pull the data from these 2 tables into Java cache, i will still have to loop thought the entire table 2 to see if an employee is present there as well.
Or will a python script be better?
Table 1
EmployeeID
FirstName
LastName
PhoneNumber
EM01
Jhon
Doe
12345
EM02
Dave
Joe
34567
Table 2:
EmployeeID
FirstName
LastName
PhoneNumber
EM01
Jhon
Doe
89000
EM03
Gabe
Mai
45678
Table 3 (After merging the phone numbers):
EmployeeID
FirstName
LastName
PhoneNumber
EM01
Jhon
Doe
12345,89000
EM02
Dave
Joe
34567
EM03
Gabe
Mai
45678

You can easily do this as a SQL query.
Basically, you want a full join, but -- alas -- MySQL doesn't support that.
So, one method is:
select t1.EmployeeID, t1.FirstName, t1.LastName
concat_ws(',', t1.PhoneNumber, t2.PhoneNumber) as PhoneNumber
from table1 t1 left join
table2 t2
on t1.EmployeeID = t2.EmployeeID
union all
select t2.EmployeeID, t2.FirstName, t2.LastName, t2.PhoneNumber
from table2 t2
table1 t1 left join
on t1.EmployeeID = t1.EmployeeID
where t1.EmployeeID is null;
That is, get all the rows for the employees in table1. Then add in the additional rows from table2. For the first part, concat_ws() is convenient for combining the phone numbers.

Related

Cannot fetch the latest record from the database

I am trying to get the latest record from the Database (Derby database).
I have a BILL table in the database that has a column BillId. The data type of BillId is varchar(15) and is in the format as:
3122022-1
The digits before the "-" (i.e., 3122022) are according to the date (3/12/2002). The value after the "-" is the bill counter (i.e., 1).
The problem is, when I try to get the latest record from the database using max(BILLID), it considers 3122022-9 as the maximum/latest record even if the billId 3122022-10 or higher exists.
In simple words, it ignores the 0 or any value placed at the second place after "-". Why is this issue happening and what is the solution??
Here is the table structure:
Bill table
I used the following query:
select max(billId) as lastBill from Bill where empName='Hassan' and Date=Current Date;
empName is important as there are 4-5 employees and each will have their own count of Bill.
If I run this query:
select billid from bill order by empName desc;
I get this result:
Bill ids when I sort them by empName column
But if I run the max(billId) query, This is what I get:
select max(billId) as lastBill from Bill where empName='Hassan' and Date=Current Date;
max(billid) results
I hope I was able to explain my question well. Will be grateful for your help and support.
I tried max(billId)
i came up with sample dataset and query.
//Postgres sql
with data as
(
select 'A' as emp_name,'03122022-1' as dated_on
union
select 'A' as emp_name,'03122022-2' as dated_on
union
select 'A' as emp_name,'03122022-3' as dated_on
union
select 'A' as emp_name,'03122022-4' as dated_on
union
select 'A' as emp_name,'03122022-5' as dated_on
union
select 'A' as emp_name,'03122022-6' as dated_on
)
,
data_clean as (
select emp_name,dated_on,
to_date((regexp_split_to_array (dated_on,'-'))[1],'DDMMYYYY') as bill_dated_on,
(regexp_split_to_array (dated_on,'-'))[2] ::int as bill_id
from data)
select emp_name,max(bill_id) from data_clean
where bill_dated_on='20221203'
group by emp_name;
emp_name|max|
--------+---+
A | 6|

Writing data to tables with relationship in SQL Server

I have two different tables with multiple columns. Like the following
Table1
ID CUSTOMER PRODUCT DETAILS
21 joe phone
22 doe shoe
23 john cup
Table2
PRODUCT ID COST ID
1 9000 21
2 600 22
3 30 23
ID in table1 is primary index and PRODUCT ID in table2 is the primary index. The common link between these two tables is ID. I used the following query to read the values.
String query = "SELECT * FROM Table1 c1 INNER JOIN Table2 c2 ON c1.ID = c2.ID";
But my problem is when I am adding new row, I use the following queries for it.
String query1 = "INSERT INTO Table1(CUSTOMER) VALUES ('" + alex + "') ";
String query2 = "INSERT INTO Table2(COST) VALUES ('" + 500 + "') ";
Since ID is not the primary index for Table2, I get a NULL value in ID. When I try to read again, since ID are not matched the I cannot reload the saved data.
Edit
This is what I have tried based on the explanation given here,
String foreignKey = "ALTER TABLE Table2 ADD FOREIGN KEY (ID) REFERENCES Table1(ID)";
Still the column ID is null in table2. Do I need combine the queries in some sort to acheive the result ? Because I just execute queries one by one as follows.
Statement stmt = connect.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(foreignKey);
General idea here - you need to get autogenerated id from first "insert" to Table1 and set "ID" at Table2 explicitly with this id in the second "insert". You can see how to do that here for example.
Foreign key in your second table - is just a constraint, that helps to prevent data's inconsistency. Its main purpose - is to give some message to you when you're trying to put some data with foreign key that doesn't exist in primary table. Also it can help (if you set this while creating foreign key) to remove from Table2 rows linked on foreign key with Table1 when you delete row at Table1.
And you shouldn't call stmt.executeUpdate(foreignKey); each time you insert data to Table1, Table2. It should be done once when you describe the structure of your database.

MySql count of rows and insert into the table that count

I have a scenario where i need to take count of rows in mysql table for the current branch(in that table we are store branch) and insert the count of rows with other details into the same table. But the problem is when two or more concurrent users try to insert from the same branch at the same time the count is same for all the users, but for me the insert should not happ for the other user(s) until i read the count and insert that one user request . Is there any way the locking works for this and any example would be helpful(All i need to do this in MySql store procedure)
Edit : Sorry, I cant share the working code but i can write example here
My table structure is here
id name branchid count
1 abc 1 1
2 xyz 1 2
3 abcd 2 1
4 wxyz 2 2
Here am taking count of rows from the above table for given branch(ex : 1) and inserting the row with that calculated count
Ex :
set #count = (select count(id) from tbl where branchid = 1);
later
insert into tbl(id, name, branchid, count)
values(5, 'abcd', 1, #count)
This works great provided if only one user access this from one branch , but if more than one user from same branch try to access this at exact same time the
#count
is duplicating for the branch users.
Why not just do it in one query:
insert into tbl(id, name, branchid, count)
select 5, 'abcd', 1, count(*)
from from tbl
where branchid = 1;

SQL one row equals to two merged rows

Hi I'm doing project in Java and i have two tables in SQL and i want to link data between them.
Here is my problem, the first table have 2 columns named name and surname , and the second table have a row namesurname (there are other columns in each table) where name and surname are connected.
Table 1 :
name - Example
surname - Something
Table 2:
namesurname - Example Something
How can i make a link between these two tables to display all data from Table 2. based on inserted name and surname in Table 1.
Have you tried something like
select * from 1.table
join 2.table on [2.table].namesurname = [1.table].name+" "+[1.table].surname

Im mysql, not getting id

in our chat application, the following is the case, there is person 'A' who have chat with person 'B' in two cases,1) one-to-one chat and 2) group chat. so in the database records are as follows,
Table : core
id name
1 A
2 B
3 C
Table : Master
id core_id
1 1
2 2
Table : recepient
id core_id master_id
1 1 1
2 2 1
3 1 2
4 2 2
5 3 2
so in the recepient table, we have 2 entries for Person 'A'. Now the problem is, i want the master_id = 1 form recepient table, in which person 'A' and 'B' are communicated. But there are two entries in recepient table for this. so how to get that id?
It's unclear what you are asking and your table names do not assist in making this any clearer. However, I suspect you are selecting records from the recipient table based only on the master_id and need to join with the other tables so that you can also include the name in the query.
So with this in mind, try the following (caveat: I haven't tested this, and I work with Oracle not mysql) but:
select *
from core c, master m, recipient r
where c.id = m.core_id
and c.id = r.core_id
and m.id = r.master_id
and c.name = 'A'
And before anyone complains that this sql does not follow the latest ANSI standard, it's the way I do it and it works for me!

Categories