I have an employee table like:
Empid EmpName Remark
001 Bob
002 Harish
003 Tom
004 Dicky
001 Bob
003 Tom
I have to find the duplicate employee id and accordingly updating the remark field as duplicate !
Thanks.
update employee set remark = 'duplicate'
where empid in (
select empid
from employee
group by empid, empname
having count(*) > 1 )
This question is very vague, because you do not mention what ORM library you are using or how you are accessing/manipulating your database. But basically want you want to do is execute a derived table query, then make a decision based on the results.
SELECT * FROM
(SELECT empId, count(empId) numIds from Employee group by empId) IdCount
WHERE numIds > 1;
Run this query via a PreparedStatement or whatever your ORM framework provides, then iterate over each result and update your remark field.
Below will give you ID of duplicate records
`select empID, empName, count(empID) as cnt from fschema.myTable group by (empID) having cnt > 1 order by cnt`
Will get back to you how to set remark as 1 for duplicates shortly...
Related
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|
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.
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.
We would typically write Oracle SQL to find max salary of every employee in each department if we had a table with EmpID, DeptID, Salary:
select EmpID,DeptID, rank over(partition by DeptID order by Salary) rnk
from Table
where rnk=1;
OR
select EmpID
from Table1
where Salary =(Select max(Salary) from Table2 group by DeptID
and Table2.DeptId = Table1.DeptId )
If the above table was a file instead, then how can we write custom Java code to implement the same behavior?
If I got your question right, why don't you implement it by reading the file and passing it to the library of your choice for querying? Or by using Hibernate, and then passing the read file as:
Session.createSQLQuery(fromReadFile)
and
Session.createSQLQuery(fromReadFile).uniqueResults()
to get the results as a List or a particular object?
I have a table with the columns Name, QueuePosition
Name QueuePosition
John 1
Paul 1
Mike 2
Sarah 3
Kevin 4
George 4
How do i reorder them quickly to be like this?
Name QueuePosition
John 1
Paul 2
Mike 3
Sarah 4
Kevin 5
George 6
Thanks
Edit -Should have been more specific
i need it to be permanent if possible. Not just a select statement.
There will be no more than 10 names in list. Possibly have to just save them in an array and overwrite the whole list. I was wondering is there a quick way of doing this.
First, SQL tables represent unordered sets. So, there is no way to ensure that "John" comes before "Paul", unless you have another column.
The canonical approach to what you want is the row_number() function. This is an ANSI standard function supported by most databases. The query is:
select name, row_number() over (order by QueuePosition) as QueuePosition
from table t;
This can also be folded into an update, but the syntax would vary by database.
EDIT:
In SQLite you can do:
with toupdate as (
select name, row_number() over (order by QueuePosition) as QueuePosition
from tablename
)
update tablename
set QueuePosition = (select QueuePosition from toupdate where toupdate.name = tablename.name);
EDIT II;
In SQLite you can do:
with toupdate as (
select name, (select count(*) from tablename t2 where t2.QueuePosition <= t.QueuePosition) as QueuePosition
from tablename t
)
update tablename
set QueuePosition = (select QueuePosition from toupdate where toupdate.name = tablename.name);
Try
update yourTableName set QueuePosition = ROWID