| Author |
Topic |
|
qjam
Starting Member
19 Posts |
Posted - 2007-08-23 : 03:35:18
|
Hi all,I have a complex query which I can't seem to get working. The query needs to compare one database to another.The database I'm running this against stores the current normal billable hours per employee, and the second database stores a history of any changes to the normal hours. For example, the tables in the individual databases would look like:Database1.dbo.EmployeeEmpCode, NormalHours101, 35303, 23Database2.dbo.HistEmployeeNum, DateChanged, NormHrs101, 10/02/2001, 22101, 12/05/2003, 65101, 01/01/2007, 42303, 05/02/2007, 23What I am attempting to do is list all the current normal hours in Database1 which have changed since the last record of the normal hours in Database2. I have used this query in SQL Server:SELECT TOP 100 PERCENT E.EmpCode, E.NormalHours, AP.*, getdate() AS TheDateFROM dbo.Employee E LEFT OUTER JOIN (SELECT P1.* FROM Database2.dbo.Hist AS P1 WHERE P1.DateFrom IN (SELECT TOP 1 P2.DateFrom FROM Database2.dbo.Hist P2 WHERE P1.EmployeeNum = P2.EmployeeNum ORDER BY P2.DateFrom DESC)) AP ON E.cEmpCode = AP.EmployeeNumWHERE E.NormalHours <> AP.NormHrsORDER BY E.EmpCodeThis in my head works, and it will join the Employee table with the last record for each employee in the Hist table. However, it seems to disregard the 'WHERE E.NormalHours <> AP.NormHrs' condition, and shows a line for every employee, regardless of if their normal hours have changed eg. even though the employee 303 has the same hours as their last record in the Hist table. An example of the output would be:E.EmpCode, E.NormalHours, EmployeeNum, DateChanged, NormHrs, TheDate101, 35, 101, 01/01/2007, 42, 23/08/2007303, 23, 101, 05/02/2007, 23, 23/08/2007Is anyone able to help me? I feel like I'm beating my head against a brick wall Cheers, qjam. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-23 : 03:52:47
|
[code]declare @employee table (empcode int, normalhours int)insert @employeeselect 101, 35 union allselect 303, 23declare @hist table (employeenum int, datechanged smalldatetime, normhrs int)insert @histselect 101, '10/02/2001', 22 union allselect 101, '12/05/2003', 65 union allselect 101, '01/01/2007', 42 union allselect 303, '05/02/2007', 23;with peso (employeenum, datechanged, normhrs, recid, thedate)as ( select employeenum, datechanged, normhrs, row_number() over (partition by employeenum order by datechanged desc), dateadd(day, datediff(day, 0, current_timestamp), 0) from @hist)select e.empcode, e.normalhours, p.employeenum, p.datechanged, p.normhrs, p.thedatefrom @employee as eleft join peso as p on p.employeenum = e.empcode and e.normalhours <> p.normhrs and p.recid = 1[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2007-08-23 : 09:14:14
|
| Hi,Select e.empcode, e.normalhours, h2.Employeenum, h2.datechanged, h2.normhrs From @employee eLeft join (Select Employeenum, max(datechanged) as datechanged From @hist Group By Employeenum) H1 on h1.Employeenum = e.empcodeLeft join @hist h2 on h1.datechanged = h2.datechanged and h1.Employeenum = h2.Employeenum and e.normalhours <> h2.normhrs |
 |
|
|
qjam
Starting Member
19 Posts |
Posted - 2007-08-23 : 20:14:54
|
| Thanks PeterNeo and Peso. They both work great. |
 |
|
|
|
|
|