Quantcast
Channel: Questions in topic: "sql-server-2005"
Viewing all articles
Browse latest Browse all 415

Need help writing SQL to get Taxable Gross

$
0
0
Hi SQL Gurus! I need help getting the Taxable Gross amount out of our Kronos database via SQL. From what I can tell, this is a derived value using the following. @PayrollTransactionIdNo int, @PersonIdNo int, @DisplayInactives bit = 0 AS DECLARE @TotTaxAmnt dec(14,5) DECLARE @TotDeductionAmnt dec(14,5) DECLARE @TotHours dec(14,5) DECLARE @TotCompAmnt dec(14,5) DECLARE @CalendarDetailStartDate datetime DECLARE @CalendarDetailEndDate datetime DECLARE @CalendarDetailPaymentDate datetime DECLARE @PayrollTransactionPaymentDate datetime DECLARE @PayGroupIdNo INT DECLARE @PayAgencyIdNo INT --Determine the Pay Group and Payment Date SELECT @PayrollTransactionPaymentDate = PayrollTransactionPaymentDate, @PayGroupIdNo = PayGroupIdNo FROM vPAYROLL_TRANSACTIONS_B WHERE PayrollTransactionIdNo = @PayrollTransactionIdNo --Determine the Pay Agency SELECT @PayAgencyIdNo = ReportToOrganization FROM tORGANIZATIONS WHERE OrgCodeIdNo = @PayGroupIdNo AND @PayrollTransactionPaymentDate BETWEEN OrganizationFromEffectDate AND OrganizationToEffectDate -- get total tax and deduction amounts for this payroll transaction SELECT @TotTaxAmnt = ROUND(SUM( CASE pc.PayrollCategoryCodeIdNo WHEN -11104 THEN wd.WithholdingAmount ELSE 0 END ), 2), @TotDeductionAmnt = ROUND(SUM( CASE pc.PayrollCategoryCodeIdNo WHEN -10602 THEN wd.WithholdingAmount ELSE 0 END ), 2) FROM vWITHHOLDING_DETAIL_B wd INNER JOIN vPAYROLL_CODES_ALL pc ON wd.PayrollCodeIdNo = pc.PayrollCodeIdNo WHERE wd.PayrollTransactionIdNo = @PayrollTransactionIdNo AND pc.PayrollCategoryCodeIdNo IN (-10602, -11104) AND wd.WithholdingAmount <> 0.0 -- get total tax and deduction amounts for this payroll transaction -- ZAUN-0004-TJM Expand to 5 decimal places SELECT @TotHours = ROUND(SUM(ISNULL(cd.CompensationDetailQuantity,0)), 5), @TotCompAmnt = ROUND(SUM(cd.CompensationDetailAmount), 2) FROM vCOMPENSATION_DETAIL_B cd INNER JOIN vPAYROLL_CODES_ALL pc ON cd.PayrollCodeIdNo = pc.PayrollCodeIdNo WHERE cd.PayrollTransactionIdNo = @PayrollTransactionIdNo AND pc.PayrollCategoryCodeIdNo = -10400 AND cd.CompensationDetailAmount <> 0.0 -- get the start end dates of the calendar for the payments for this payroll transaction -- these values may turn out to be null, not all payroll transactions have payments, e.g adjustments SELECT TOP 1 @CalendarDetailStartDate = cd.CalendarDetailStartDate, @CalendarDetailEndDate = cd.CalendarDetailEndDate, @CalendarDetailPaymentDate = cd.CalendarDetailPaymentDate FROM vCALENDAR_DETAILS_B cd JOIN vPAYMENTS_B pmt ON cd.CalendarDetailIdNo = pmt.CalendarDetailIdNo WHERE pmt.PayrollTransactionIdNo = @PayrollTransactionIdNo -- Return the header recordset (RS #1) SELECT @CalendarDetailStartDate AS CalendarDetailStartDate, @CalendarDetailEndDate AS CalendarDetailEndDate, PayrollTransactionPaymentDate, ISNULL(@TotHours, 0) AS Hours, ISNULL(@TotCompAmnt, 0) AS CompAmount, ISNULL(@TotTaxAmnt, 0) AS TaxAmount, ISNULL(@TotDeductionAmnt, 0) AS DedAmount, NetAmount = ISNULL((SELECT SUM(pmt.PaymentAmount) FROM vPAYMENTS_B pmt LEFT JOIN vDIRECT_DEPOSITS_B dd ON pmt.DirectDepositIdNo = dd.DirectDepositIdNo -- JURE-0491 WHERE pmt.PayrollTransactionIdNo = @PayrollTransactionIdNo AND (dd.DirectDepositAccountTypeCodeIdNo <> -10604 OR -- JURE-0491 dd.DirectDepositAccountTypeCodeIdNo IS NULL) AND ((@CalendarDetailPaymentDate BETWEEN dd.DirectDepositFromEffectDate AND dd.DirectDepositToEffectDate) OR dd.DirectDepositFromEffectDate IS NULL)),0) --WHR-10120 FROM vPAYROLL_TRANSACTIONS_B WHERE PayrollTransactionIdNo = @PayrollTransactionIdNo /* Return all compensation detail quantities for the specified payroll transaction (RS #2) */ SELECT CONVERT(char(10), cpd.CompensationChargeDate, 101) AS ChargeDate, COALESCE( --If there is an empty string, treat it as a NULL. SQL REPLACE function does not do this so I used a CASE CASE DATALENGTH(povpg.PayrollOrganizationValue) WHEN 0 THEN NULL ELSE povpg.PayrollOrganizationValue END, --If there is an empty string, treat it as a NULL. SQL REPLACE function does not do this, so I used a CASE. CASE DATALENGTH(povpa.PayrollOrganizationValue) WHEN 0 THEN NULL ELSE povpa.PayrollOrganizationValue END, pcv.PayrollCodeValue, pc.PayrollCodeDescription ) AS PayrollCodeDescription, --ZAUN-0004-TJM Expanded decimal places to 5 ROUND(SUM(cpd.CompensationDetailQuantity), 5) AS CompensationDetailQuantity, ISNULL(cpd.PayRate, 0) AS Payrate, cpd.PayrollCodeIdNo, sdc.ShiftDiffCode AS ShiftCode FROM vSHIFT_DIFFERENTIAL_CODES sdc RIGHT JOIN vCOMPENSATION_DETAIL_B cpd ON cpd.ShiftDiffIdNo = sdc.ShiftDiffIdNo INNER JOIN vPAYROLL_CODES_ALL pc ON pc.PayrollCodeIdNo = cpd.PayrollCodeIdNo LEFT JOIN tPAYROLL_CODE_VALUES pcv ON (pcv.PayrollCodeIdNo = pc.PayrollCodeIdNo AND pcv.PayrollPropertyCodeIdNo = -10142) LEFT JOIN tPAYROLL_CODE_AMOUNTS pca ON pca.PayrollCodeAmountIdNo = pc.PayrollCodeIdNo --Join on "Mapping" table that maps Comp Amount "stub desc" record to Compensation Code LEFT JOIN tPAYROLL_ORGANIZATION_VALUES povpg --Pay Group ON (povpg.PayrollCodeIdNo = pca.PayrollCodeIdNo --Join on "Mapping" table that maps Comp Amount "stub desc" record to Compensation Code AND povpg.PayrollPropertyCodeIdNo = -10142 -- stub description AND povpg.PayrollOrganizationIdNo = @PayGroupIdNo AND (cpd.CompensationChargeDate BETWEEN povpg.PayrollOrganizationValueStartDate AND povpg.PayrollOrganizationValueEndDate) ) LEFT JOIN tPAYROLL_ORGANIZATION_VALUES povpa --Pay Agency ON (povpa.PayrollCodeIdNo = pca.PayrollCodeIdNo --Join on "Mapping" table that maps Comp Amount "stub desc" record to Compensation Code AND povpa.PayrollPropertyCodeIdNo = -10142 -- stub description AND povpa.PayrollOrganizationIdNo = @PayAgencyIdNo AND (cpd.CompensationChargeDate BETWEEN povpa.PayrollOrganizationValueStartDate AND povpa.PayrollOrganizationValueEndDate) ) WHERE cpd.PayrollTransactionIdNo = @PayrollTransactionIdNo AND cpd.CompensationDetailQuantity <> 0.0 GROUP BY cpd.CompensationChargeDate, cpd.PayrollCodeIdNo, COALESCE( --If there is an empty string, treat it as a NULL. SQL REPLACE function does not do this so I used a CASE CASE DATALENGTH(povpg.PayrollOrganizationValue) WHEN 0 THEN NULL ELSE povpg.PayrollOrganizationValue END, --If there is an empty string, treat it as a NULL. SQL REPLACE function does not do this, so I used a CASE. CASE DATALENGTH(povpa.PayrollOrganizationValue) WHEN 0 THEN NULL ELSE povpa.PayrollOrganizationValue END, pcv.PayrollCodeValue, pc.PayrollCodeDescription ), cpd.Payrate, sdc.ShiftDiffCode ORDER BY PayrollCodeDescription I need to add taxable gross to the SQL below for validating data. select wd.empno, pspj.LastName, pspj.FirstName,wd.payrolltransactionpaymentdate, wd.payrollcode, wd.payrollcodedescription, wd.withholdingamount, wd.payrolltransactionnetpay from withholding_details wd, Persons pspj where wd.empno = pspj.PersonUserDefined1 and wd.PayrollTransactionPaymentDate ='02/14/2013' and wd.EmploymentStatusToEffectDate='1/1/3000' and wd.PayrollCodeEndDate='1/1/3000' and wd.TaxLocationCodeToEffectDate='1/1/3000' and wd.PositionCodeToEffectDate='1/1/3000' and pspj.PersonToEffectDate = '1/1/3000' order by wd.empno I know what I want it to do, just having trouble getting the SQL to pull only the Taxable Gross. I know this can vary per the tax codes so any help you can provide would be greatly appreciated. Thanks! Mary

Viewing all articles
Browse latest Browse all 415

Trending Articles