Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
this update query works fine but I only need to update employee records for those who are non-exempt (meaning these emp has an entry in the clocking_details table, and i can see the emp_id when do a group by emp_id). update clocking_statusset clocking_status= (SELECT min(CASE WHEN (cd.time_out IS NULL) THEN 'in' ELSE 'out' END) AS cStatusFROM dbo.CLOCKING_DETAILS AS cdwhere (cd.emp_id=clocking_status.emp_id)GROUP BY cd.emp_id),clocking_date = (SELECT max(cd.clocking_date) FROM dbo.CLOCKING_DETAILS AS cdwhere (cd.emp_id=clocking_status.emp_id) -- REFERING TO THIS LINEGROUP BY cd.emp_id)
chrianth
Yak Posting Veteran
50 Posts
Posted - 2009-05-28 : 05:27:33
quote:Originally posted by doran_doran this update query works fine but I only need to update employee records for those who are non-exempt (meaning these emp has an entry in the clocking_details table, and i can see the emp_id when do a group by emp_id). update clocking_statusset clocking_status= (SELECT min(CASE WHEN (cd.time_out IS NULL) THEN 'in' ELSE 'out' END) AS cStatusFROM dbo.CLOCKING_DETAILS AS cdwhere (cd.emp_id=clocking_status.emp_id)GROUP BY cd.emp_id),clocking_date = (SELECT max(cd.clocking_date) FROM dbo.CLOCKING_DETAILS AS cdwhere (cd.emp_id=clocking_status.emp_id) -- REFERING TO THIS LINEGROUP BY cd.emp_id)
Can you explain more about the details of your problem and post some test data? I tried to run your update statement and it seems to update the correct records and informations.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-05-28 : 12:10:21
seems like this:-
update csset cs.clocking_status=cd.cStatus,cs.clocking_date = cd.clocking_date FROM clocking_status csINNER JOIN(SELECT emp_id,min(CASE WHEN (time_out IS NULL) THEN 'in' ELSE 'out' END) AS cStatus,max(clocking_date) AS clocking_date FROM dbo.CLOCKING_DETAILS AS GROUP BY emp_id)cdon cd.emp_id=cs.emp_id
doran_doran
Posting Yak Master
179 Posts
Posted - 2009-05-28 : 22:19:30
Thanks visakh for your help. This worked (except just a very minor twick). Thanks a lot.=== update sql here ===update csset cs.clocking_status=cd.cStatus,cs.clocking_date = cd.clocking_date FROM clocking_status csINNER JOIN(SELECT emp_id,min(CASE WHEN (time_out IS NULL) THEN 'in' ELSE 'out' END) AS cStatus,max(clocking_date) AS clocking_date FROM CLOCKING_DETAILSGROUP BY emp_id) as cdon cd.emp_id=cs.emp_id