Queennie L
asked on
Delete in Main SQL table if exists if a value has multiple status then insert it to New SQL table
Hello Experts,
I have a SQL query that I need to delete records if an Employee name has multiple status from dbo.MainTable and if it exists then insert it into a new table dbo.NewInsertTable. Please see attached file.
I tried to do case statements or subquery but I cannot get it to work.
I know this is just easy to all experts.
Thank you again.
DeleteAndInserttoAnotherSQLTableAny.xlsx
I have a SQL query that I need to delete records if an Employee name has multiple status from dbo.MainTable and if it exists then insert it into a new table dbo.NewInsertTable. Please see attached file.
I tried to do case statements or subquery but I cannot get it to work.
I know this is just easy to all experts.
Thank you again.
DeleteAndInserttoAnotherSQLTableAny.xlsx
ASKER
@RussellFox:
I apologize I just got back to you today.
I will test it and let you know.
Thank you.
I apologize I just got back to you today.
I will test it and let you know.
Thank you.
ASKER
@RussellFox:
The query did not output what I needed. If employee name has multiple "status" then insert it to a another table.
Thank you.
The query did not output what I needed. If employee name has multiple "status" then insert it to a another table.
Thank you.
ASKER
Any help please? Thank you.
ASKER
This query not quite what I need:
Thank you again for any help.
SELECT t1.UniqueID
, t1.Data
, t1.Filename
, t1.CheckNumber
, t1.CheckDate
, t1.Payment
, t1.EmployeeName
, t1.CaseNo
, t1.[Status]
FROM dbo.MainTable t1
WHERE EXISTS
(
SELECT 1 FROM dbo.MainTable t2
WHERE t2.EmployeeName = t1.EmployeeName
AND t2.CaseNo = t1.CaseNo
AND t2.[Status] = t1.[Status]
)
order by t1.EmployeeName ASC, t1.CaseNo ASC
Thank you again for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Russell.
Open in new window
Deleting them out of the main table is a little trickier because you need a way for the database to decide which one should be deleted. Let's assume the CheckDate is actually a DateTime and you want to KEEP only the most recent entry. You can use the ROW_NUMBER function, partitioned by the IDNumber and ordered by that date field to get a line item number per user:Open in new window