Update individual records within a table

Hi,

I am trying to update individual records within a row of data, but I can't seem to get the script right, Perhaps I've been looking at it too long.

I have two tables. Table 1, PayType, contains 1900+ unique rows based off a unique employee ID. Table 2, HoursWorked, contains multiple rows for each employee id.

Paytype table example:

EmID    PayType
0001    H
0002    S
0003    S
0004    H

HoursWorked table example:

EmID    Date               TotalHours   PayType
0001    01/01/2013     3                  NULL
0001    01/02/2013     1                  NULL
0001    01/03/2013     4                  NULL
0002    01/01/2013     6                  NULL
0002    01/02/2013     6                  NULL
0003    01/03/2013     3                  NULL
0003    01/04/2013     8                  NULL
0003    01/05/2013     1                  NULL
0003    01/06/2013     4                  NULL
0003    01/07/2013     4                  NULL
0004    01/01/2013     3                  NULL
0004    01/03/2013     2                  NULL
0004    01/04/2013     3                  NULL


I need the script to update the table to that the HoursWorked table contains accurate PayType data. EX:

EmID    Date               TotalHours   PayType
0001    01/01/2013     3                  H
0001    01/02/2013     1                  H
0001    01/03/2013     4                  H
0002    01/01/2013     6                  s
0002    01/02/2013     6                  s
0003    01/03/2013     3                  s
0003    01/04/2013     8                  s
0003    01/05/2013     1                  s
0003    01/06/2013     4                  s
0003    01/07/2013     4                  s
0004    01/01/2013     3                  H
0004    01/03/2013     2                  H
0004    01/04/2013     3                  H

I've tried manipulating this script to do what I need to do, but I can't seem to do it. I'm pretty sure I need to use While, but I couldn't get that to work either.
update [DirectCareAnalysis]
set WageType=(
select top 1 max(left(a.paypolicy,1)) 
from  HR_PayPolicy a
left join [DirectCareAnalysis] b
on a.employeeclock=b.[Employee ID]
where a.employeeclock=b.[Employee ID] and WageType is null
group by a.employeeclock)

Open in new window


Any guidance would be appreciated. Thanks.
LVL 1
susnewyorkAsked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
That's and UPDATE with a JOIN between the tables on EmID
UPDATE HoursWorked
SET hw.PayType = pt.PayType
FROM PayType pt
   JOIN Hoursorked hw ON pt.EmID = hw.EmID

Open in new window

0

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
YZlatCommented:
try

UPDATE hw SET hw.PayType=pt.PayType
FROM  HoursWorked as hw JOIN Paytype pt On
 hw.EmID=pt.EmID

Open in new window

0
YZlatCommented:
LOL sorry jimhorn, didn't see you already posted - didn't mean to post identical answer:)
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
btw What's your reason for wanting this?  Since the two tales as is are already normalized, doing this update would mean that data could become obsolete if a change is made in one table but not the other.  

Better to create a view that has the desired layout, and use the view for whatever purpose you have, and keep the tables normalized..
CREATE VIEW YourView AS

-- Add some code comments here

SELECT hw.EmID, hw.Date, hw.TotalHours, pt.PayType
FROM HoursWorked hw
   LEFT JOIN Paytype pt ON hw.EmID = pt.EmID
GO

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
np.  As Frazier Crane says, when you know the answer it's hard not to hit the buzzer.
0
susnewyorkAuthor Commented:
I have an SSIS package that merges data from multiple sources and then adds that data to a master table. The paytype info is located on an excel spreadsheet, but I add the data to a temp table so that SQL Server could add the paytype to the master table.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
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 2008

From novice to tech pro — start learning today.