Get latest Record and reverse last record with Negative

Hello All,

I am Pulling time sheet work detail data every 5 minutes inserting into my master table , previously I was doing differences in minutes if I insert , meaning if an employee sends 50 minutes on 5/11 and then changes to 30 mins on 5/11 I would show 50 mins  on 5/11 and -20 on 5/11,this works fine but does not work perfect if date worked changes,

so when I have any differences or changes in minutes I would like for it to behave this way. if an employee sends 60 mins on 5/11 and then changes to 40 mins on 5/11, I would like to see these record in my table

5/11 60mins
5/11 -60 mins
5/11 40 mins.

I would like assistance and just a general of how I can do this. Thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Logic something like this though you will want to add some error checking and wrap the two inserts in a transaction.

   @NewMIn   INT;

INSERT INTO myTable (Date, Min)
SELECT Date, -SUM(Min)
FROM myTable
WHERE Date = @Date

INSERT INTO myTable (Date, Min)
VALUES (@Date, @NewMin)
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, what you are saying is that if a record for that day already exist that you'll need to add the opposite operation to cancel it before adding the correct value, right?
Then, just verify before inserting the new value and if exists then insert first the negative operation:
        FROM TableName
        WHERE EmployeeID = @ID AND ColDate = @Date AND ColValue > 0)
            INSERT INTO TableName (EmployeeID, ColdDate, ColValue)
            SELECT EmployeeID, ColDate, (-1*ColValue)
            FROM TableName
            WHERE EmployeeID = @ID AND ColDate = @Date AND ColValue > 0

INSERT INTO TableName (EmployeeID, ColdDate, ColValue)
VALUES (@ID, @Date, @Value)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.