I need to return a period to date total as well as a last value from a table. Given the following data set...
I need to return the sum of the gross pay as well as the last value (by date) of the IRA_Contrbution grouped by the Employee's ID. So the query should return something like...
*The IRA_contribution for 01123MRK is 0 becuase there was no check for the last check date of 01-03-2010.
The Sum_gross is simple. I don't see an elegant way to grab the latest value for the IRA though. I was looking for something like the LAST() aggregate in Access but I know SQL doesn't have that type of function.
Any ideas?
Thank you.
Tim Roop
"If you can do something about it, why waste time getting upset? If you can't do anything about it, why bother getting upset on top of it? -Shantideva
Code:
Declare @Temp Table(EmpId VarChar(20), Checkdate smalldatetime, Gross_Pay numeric(19,5), IRA_Contribution numeric(19,5))
Insert Into @Temp Values('01123DOE', '01/01/2010', 500.00, 5.00)
Insert Into @Temp Values('01123DOE', '01/02/2010', 600.00, 6.00)
Insert Into @Temp Values('01123DOE', '01/03/2010', 550.00, 5.50)
Insert Into @Temp Values('01123TIM', '01/01/2010', 1000.00, 10.00)
Insert Into @Temp Values('01123TIM', '01/02/2010', 1100.00, 11.00)
Insert Into @Temp Values('01123TIM', '01/03/2010', 1150.00, 11.50)
Insert Into @Temp Values('01123MRK', '01/01/2010', 1000.00, 10.00)
Insert Into @Temp Values('01123MRK', '01/02/2010', 1100.00, 11.00)
I need to return the sum of the gross pay as well as the last value (by date) of the IRA_Contrbution grouped by the Employee's ID. So the query should return something like...
Code:
EmpID Sum_Gross Last_IRA
01123DOE 1650.00 5.50
01123TIM 3250.00 11.00
01123MRK 2100.00 0.00*
*The IRA_contribution for 01123MRK is 0 becuase there was no check for the last check date of 01-03-2010.
The Sum_gross is simple. I don't see an elegant way to grab the latest value for the IRA though. I was looking for something like the LAST() aggregate in Access but I know SQL doesn't have that type of function.
Any ideas?
Thank you.
Tim Roop
"If you can do something about it, why waste time getting upset? If you can't do anything about it, why bother getting upset on top of it? -Shantideva