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.
Author |
Topic |
PeetKoekemoer
Starting Member
12 Posts |
Posted - 2012-04-23 : 13:19:38
|
I have 2 tables.PerformanceManagement and attendancein the performancemanagement table I have one record per employee for a given period. In the other table do I have the employee's clocking details (containig over 17 million records). I want to update the performamcemanagement table's fields with the grouped shifts worked for the period. but this query takes for ever to execute.Update PerformanceManagement set UGShifts = (SELECT Count(Workdate) as UGShifts FROM Attendance a where a.ActualAttendance IN ('U') and a.WorkDate BETWEEN PerformanceManagement.BeginDate and PerformanceManagement.EndDate and a.IndustryNo = PerformanceManagement.IndustryNo), SurfaceShifts = (SELECT Count(Workdate) as UGShifts FROM Attendance a where a.ActualAttendance IN ('W') and a.WorkDate BETWEEN PerformanceManagement.BeginDate and PerformanceManagement.EndDate and a.IndustryNo = PerformanceManagement.IndustryNo), LeaveShifts = (SELECT Count(Workdate) as UGShifts FROM Attendance a where a.ActualAttendance IN ( 'LA', 'LAC', 'LAP', 'LAU', 'LCP', 'LOC', 'LOT', 'LU','S33', 'S50', 'SE', 'SL', 'SLU') and a.WorkDate BETWEEN PerformanceManagement.BeginDate and PerformanceManagement.EndDate and a.IndustryNo = PerformanceManagement.IndustryNo) |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-23 : 13:45:38
|
What indexes do you have on the two tables? Specifically do you have indexes on IndustryNo column and on the Workdate column? Also, while not ANSI standard, an extension that T-SQL allows may make it go faster - see below. I looked at your code and rewrote it with absolutely no testing at all, so please review it and convince yourself if that makes sense. If you do choose to use it, you may want to do a select rather than an update to verify that the counts you are getting are in fact correct.UPDATE p SET UGShifts = COUNT(CASE WHEN a.ActualAttendance IN ('U') THEN Workdate END), SurfaceShifts = COUNT(CASE WHEN a.ActualAttendance IN ('W') THEN Workdate END), LeaveShifts = COUNT(CASE WHEN a.ActualAttendance IN ('LA', 'LAC', 'LAP', 'LAU', 'LCP', 'LOC', 'LOT', 'LU', 'S33', 'S50', 'SE', 'SL', 'SLU') THEN Workdate END)FROM Attendance a INNER JOIN PerformanceManagement p ON a.IndustryNo = p.IndustryNoWHERE a.WorkDate BETWEEN p.BeginDate AND p.EndDate |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2012-04-23 : 14:12:57
|
just to piggy back on sunitabeck's code, call me OCDUPDATE p SET UGShifts = COUNT(CASE WHEN a.ActualAttendance = N'U' THEN Workdate END), SurfaceShifts = COUNT(CASE WHEN a.ActualAttendance = N'W' THEN Workdate END), LeaveShifts = COUNT(CASE WHEN a.ActualAttendance IN ('LA', 'LAC', 'LAP', 'LAU', 'LCP', 'LOC', 'LOT', 'LU', 'S33', 'S50', 'SE', 'SL', 'SLU') THEN Workdate END) FROM dbo.Attendance aINNER JOIN dbo.PerformanceManagement p ON a.IndustryNo = p.IndustryNoWHERE a.WorkDate BETWEEN p.BeginDate AND p.EndDate <><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-23 : 19:17:40
|
quote: Originally posted by yosiasz just to piggy back on sunitabeck's code, call me OCD
Heh! OCD away all you like!! That is what keeps me up during the day and lets me sleep peacefully at night |
 |
|
PeetKoekemoer
Starting Member
12 Posts |
Posted - 2012-04-24 : 01:31:17
|
Sorry no luckMsg 157, Level 15, State 1, Line 2An aggregate may not appear in the set list of an UPDATE statement. |
 |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-04-24 : 09:07:47
|
Do it as a select instead, include the primary key. Use it as a derived table, join to it, and update from there. How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
PeetKoekemoer
Starting Member
12 Posts |
Posted - 2012-04-24 : 12:39:18
|
ok how do I do this? |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-24 : 14:19:08
|
What Don meant is something like this:UPDATE p SET p.UGShifts = c.UGShifts, p.SurfaceShifts = c.SurfaceShifts, p.LeaveShifts = c.LeaveShiftsFROM PerformanceManagement p INNER JOIN ( SELECT a.IndustryNo, UGShifts = COUNT(CASE WHEN a.ActualAttendance = N'U' THEN Workdate END), SurfaceShifts = COUNT(CASE WHEN a.ActualAttendance = N'W' THEN Workdate END), LeaveShifts = COUNT(CASE WHEN a.ActualAttendance IN ('LA', 'LAC', 'LAP', 'LAU', 'LCP', 'LOC', 'LOT', 'LU', 'S33', 'S50', 'SE', 'SL', 'SLU') THEN Workdate END) FROM dbo.Attendance a INNER JOIN dbo.PerformanceManagement p ON a.IndustryNo = p.IndustryNo WHERE a.WorkDate BETWEEN p.BeginDate AND p.EndDate ) c ON c.IndustryNo = p.IndustryNo; Please do test to see if the results are as expected before you use it, because I have not. |
 |
|
PeetKoekemoer
Starting Member
12 Posts |
Posted - 2012-04-25 : 00:28:32
|
thanx it worked and I checked the data (100%), I just had to add a group by a.IndustrynoUPDATE p SET p.UGShifts = c.UGShifts, p.SurfaceShifts = c.SurfaceShifts, p.LeaveShifts = c.LeaveShiftsFROM PerformanceManagement p INNER JOIN ( SELECT a.IndustryNo, UGShifts = COUNT(CASE WHEN a.ActualAttendance = N'U' THEN Workdate END), SurfaceShifts = COUNT(CASE WHEN a.ActualAttendance = N'W' THEN Workdate END), LeaveShifts = COUNT(CASE WHEN a.ActualAttendance IN ('LA', 'LAC', 'LAP', 'LAU', 'LCP', 'LOC', 'LOT', 'LU', 'S33', 'S50', 'SE', 'SL', 'SLU') THEN Workdate END) FROM dbo.Attendance a INNER JOIN dbo.PerformanceManagement p ON a.IndustryNo = p.IndustryNo WHERE a.WorkDate BETWEEN p.BeginDate AND p.EndDate group by a.IndustryNo ) c ON c.IndustryNo = p.IndustryNo |
 |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-04-25 : 10:00:39
|
gotta love a column named UGShift How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-25 : 10:08:04
|
quote: Originally posted by DonAtWork gotta love a column named UGShift How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
You are sooooooooooo BAD! |
 |
|
|
|
|
|
|