| Author |
Topic |
|
alinamiha
Starting Member
2 Posts |
Posted - 2009-04-15 : 17:30:28
|
| Hello,I'm new to sql 2005...and I need some help...I have for example this table:CREATE TABLE #Salary( EmployeeId INT ,WeekEndingDate DATETIME,Days INT,WeeklyPay MONEY)INSERT INTO [#Salary] ( [EmployeeId],[WeekEndingDate],[Days],[WeeklyPay])SELECT 1,'2008-07-04 00:00:00.000',4,2000 UNION ALLSELECT 1,'2008-07-11 00:00:00.000',5,2000 UNION ALLSELECT 1,'2008-07-11 00:00:00.000',4,2000 UNION ALLSELECT 1,'2008-08-08 00:00:00.000',5,2000 UNION ALLSELECT 1,'2008-08-15 00:00:00.000',5,2000 UNION ALLSELECT 1,'2008-08-22 00:00:00.000',3,1800 UNION ALLSELECT 1,'2008-09-19 00:00:00.000',5,1500 UNION ALLSELECT 1, '2008-09-26 00:00:00.000',5,1000 UNION ALLSELECT 1,'2008-10-03 00:00:00.000',5,1200 UNION ALLSELECT 1,'2008-10-10 00:00:00.000',5,700 UNION ALLSELECT 1,'2008-11-25 00:00:00.000',2,1200 UNION ALLSELECT 1,'2008-12-25 00:00:00.000',2,1200SELECT * FROM [#Salary]---------------------------------Next I build an table with the result wanted:DECLARE @Result AS TABLE( employeeId INT, Weekendingdate DATETIME,Days INT,WeeklyPay MONEY)INSERT INTO @ResultSELECT *FROM #SalaryUPDATE @Result SET [WeeklyPay]=( CASE WHEN [Weekendingdate]>'8/15/2008' AND [Weekendingdate]<='10/10/2008' THEN [WeeklyPay]/2 -- there are 2 "5" values entered consecutiveWHEN [Weekendingdate]>'10/10/2008' THEN 0 -- i have 4 time "5" entered consecutiveELSE weeklypayEND)FROM @ResultSELECT * FROM @Result---- i may have more than one employee in #Salary table---- for each employee i want to select in the end something like this:SELECTemployeeId,'8/22/2008' AS StartHalfPay , -- the begining of half pay'10/10/2008' AS EndHalfPay, -- the end of half pay'11/25/2008' AS StartNoPay, -- the begining of no pay'12/25/2008' AS EndNoPay, -- the end of no pay3100 AS TotalHalfPayFROM @ResultGROUP BY [employeeId]--------how should I write to compare if i have two "5" consecutive entries ?Thanks,Alina |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-16 : 14:07:36
|
i think you need another column to store the intermediate result (weekpay1) and then get date ranges based on that.UPDATE @Result SET [WeeklyPay1]=( CASE WHEN [Weekendingdate]>'8/15/2008' AND [Weekendingdate]<='10/10/2008' THEN [WeeklyPay]/2 -- there are 2 "5" values entered consecutiveWHEN [Weekendingdate]>'10/10/2008' THEN 0 -- i have 4 time "5" entered consecutiveELSE weeklypayEND)FROM @ResultSELECTemployeeId,MIN(CASE WHEN WeeklyPay1=WeeklyPay/2 THEN [WeekEndingDate] ELSE NULL END) AS StartHalfPay , -- the begining of half payMAX(CASE WHEN WeeklyPay1=WeeklyPay/2 THEN [WeekEndingDate] ELSE NULL END) AS EndHalfPay, -- the end of half payMIN(CASE WHEN WeeklyPay=0 THEN [WeekEndingDate] ELSE NULL END) AS StartNoPay, -- the begining of no payMAX(CASE WHEN WeeklyPay1=0 THEN [WeekEndingDate] ELSE NULL END) AS EndNoPay, -- the end of no paySUM(CASE WHEN WeeklyPay1=WeeklyPay/2 THEN [WeeklyPay1] ELSE 0 END) AS TotalHalfPayFROM @ResultGROUP BY [employeeId] |
 |
|
|
|
|
|