If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Excluding data for certain criteria only

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-12, 12:09
Tarkon Tarkon is offline
Registered User
 
Join Date: Sep 2003
Location: Washington, DC - USA
Posts: 50
Smile Excluding data for certain criteria only

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
PayPeriodEndEmployeeIDPaycheckNo
2012-02-03441113223
2012-01-20441113208
2012-01-06441112950


Table PR11ERF_History
PaycheckTypeEmployeeIDPayPeriodEndPaycheckAmt
Bonus4412012-02-03 
Regular4412012-02-03 
Regular4412012-01-20 
Bonus4412012-01-20 
Regular4412012-01-20 
Bonus4412012-01-06 
Regular4412012-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
Reply With Quote
  #2 (permalink)  
Old 02-06-12, 18:05
kordirko kordirko is offline
Registered User
 
Join Date: Jan 2012
Posts: 67
Could you provide an expected output of the query for this example data ?
Reply With Quote
  #3 (permalink)  
Old 02-06-12, 20:27
Mark_Otmarich Mark_Otmarich is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
You could try something like 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 OR
                               (PR11ERF_History.PayPeriodEnd = PR04PTF.PayPeriodEnd AND
                                PR11ERF_History.PaycheckType = 'Regular'))
WHERE PR04PTF.EmployeeID=441
group by PR04PTF.PayCheckNo
This excludes bonuses from the current period.
Not sure if you want the query to return the aggregate of all bonuses and regular pay up to each pay period.

If you want to just include the bonus and regular pay for each period, I suggest you add in a payPeriodStart to make your job way easier.
Reply With Quote
  #4 (permalink)  
Old 02-07-12, 11:49
Tarkon Tarkon is offline
Registered User
 
Join Date: Sep 2003
Location: Washington, DC - USA
Posts: 50
Talking

Thanks, Mark! This did the trick!


Quote:
Originally Posted by Mark_Otmarich View Post
You could try something like 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 OR
                               (PR11ERF_History.PayPeriodEnd = PR04PTF.PayPeriodEnd AND
                                PR11ERF_History.PaycheckType = 'Regular'))
WHERE PR04PTF.EmployeeID=441
group by PR04PTF.PayCheckNo
This excludes bonuses from the current period.
Not sure if you want the query to return the aggregate of all bonuses and regular pay up to each pay period.

If you want to just include the bonus and regular pay for each period, I suggest you add in a payPeriodStart to make your job way easier.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On