Hi!
I'm having a problem writing a SQL query that excludes certain data. This is for a pay stub application to display current and previous paycheck stubs. To calculate certain data such as YTD figures and time off, we SUM on other tables. However, to display correctly, I can't SUM bonus checks for the current payperiod ONLY - but for previous pay periods, I must SUM bonus checks.
Here's an example of my data:
Table PR04PTF
| PayPeriodEnd | EmployeeID | PaycheckNo |
| 2012-02-03 | 441 | 113223 |
| 2012-01-20 | 441 | 113208 |
| 2012-01-06 | 441 | 112950 |
Table PR11ERF_History
| PaycheckType | EmployeeID | PayPeriodEnd | PaycheckAmt |
| Bonus | 441 | 2012-02-03 | |
| Regular | 441 | 2012-02-03 | |
| Regular | 441 | 2012-01-20 | |
| Bonus | 441 | 2012-01-20 | |
| Regular | 441 | 2012-01-20 | |
| Bonus | 441 | 2012-01-06 | |
| Regular | 441 | 2012-01-06 | |
Right now my SQL is this:
Code:
SELECT PR04PTF.PayCheckNo, SUM(PR11ERF_History.PayCheckAmt) AS [TotalSum]
FROM PR04PTF
INNER JOIN PR11ERF_History ON
PR11ERF_History.EmployeeID = PR04PTF.EmployeeID
AND PR11ERF_History.PayPeriodEnd <= PR04PTF.PayPeriodEnd
WHERE PR04PTF.EmployeeID=441
Any help anyone could give me would be greatly appreciated.
Thanks!
-T