![]() |
![]() |
Technical Support
|
![]() |
This section contains queries that involve employees, when they worked for the company, and associated payroll transactions and other information.
Within QuickBooks, each employee has a stored set of payroll earnings items and payroll deduction items. You can query the complete history of any of these items - both employee paid and company paid.
OfficeQ also extracts full payroll transaction data (which is not available in competing products). When the pEmployee table is combined with the aTranLine table, dozens of important payroll reports become possible. There are several examples of payroll earnings reports.
The queries listed below are generally arranged from simple to complex. Normally, queries that get their data from only 1 table and do not aggregate any fields are listed first. To get full benefit of these queries and create your own queries - you should understand joins (where multiple tables are combined into a single table) and aggregation (where numeric columns from multiple rows are combined into 1 column). These concepts are not as difficult as they first appear and a web search for 'SQL' and 'join' or 'aggregation' will bring up hundreds of tutorials.
Where a line of dashes (like: --------------------) appears in a query, a useful variation of the query is shown. It could be very simple, such as changing the sort order of the output - or more complex, such as a complete alternate query. Where a single line in the query has the character: # as it's leftmost character - that line is a comment and is not processed by our query runner.
SELECT *
FROM pEmployee
ORDER BY sLastName, sFirstName
SELECT nKey, sName, sPostalCode,
sPhone, sPhoneMobile, sSSAN,
sEmplType, dHired, dReleased, bInactive
FROM pEmployee
ORDER BY sFullName
SELECT nKey, dDate, sTranType, kAcct, sAcctType,
mAmount, kEntity, sEntityType, kEntity2,
sEntity2Type, rUnitPrice, kPayItem,
sPayItemType, dPayThrough,
mWageBase, mPayTaxable
FROM aTranLine
WHERE kPayItem IS NOT NULL
ORDER BY sTranType, sPayItemType
SELECT *
FROM aTranLine
WHERE sEntityType = 'Employee'
ORDER BY sTranType, kEntity, nLine
SELECT sFirstName + ' ' + sLastName,
sSSAN, sEmplType,
sGender, dHired,
ROUND((NOW() - dHired) / 365.25, 1)
AS Years_With_Company
FROM pEmployee
WHERE bInactive = False
ORDER BY ROUND((NOW() - dHired) / 365.25, 1) DESC
SELECT
sTranType,
COUNT(sTranType) AS CntTranType
FROM aTranLine
WHERE
sEntity2Type = 'Employee'
OR sEntityType = 'Employee'
GROUP BY sTranType
------------------------------------------------
SELECT
sTranType, sPayItemType,
# COUNT(sTranType) AS CntTranType,
COUNT(sPayItemType) AS CntTranPayItemType
FROM aTranLine
WHERE
sEntity2Type = 'Employee'
OR sEntityType = 'Employee'
GROUP BY sTranType, sPayItemType
SELECT sDocNum, dDate, sTranType,
(SELECT sFullName FROM aAccount
WHERE aAccount.nKey = kAcct)
AS BankAccount, sAcctType,
mAmount,
(SELECT sName FROM pEmployee
WHERE pEmployee.nKey = kEntity)
AS EmployeeName, sEntityType,
(SELECT sFullName FROM aClass
WHERE aClass.nKey = kClass)
AS Class,
dPayThrough
FROM aTranLine
WHERE sTranType = 'Paycheck'
AND kPayItem IS NULL
ORDER BY dDate
SELECT pEmployee.sFirstName, pEmployee.sLastName,
aTranLine.*
FROM pEmployee LEFT JOIN aTranLine
ON pEmployee.nKey = aTranLine.kEntity
WHERE
sEntityType = 'Employee'
OR sEntity2Type = 'Employee'
ORDER BY kEntity, sTranType, aTranLine.nKey
SELECT sName,
COUNT(*) AS CountOfShifts,
SUM(rHours) AS SumOfHours
FROM pEmployee INNER JOIN pTimeLine
ON pEmployee.nKey = pTimeLine.kEmplVend
WHERE kCustJob IS NULL
GROUP BY sName
SELECT sName, sSSAN,
SUM(mAmount) AS SumAmount,
COUNT(mAmount) AS CountAmount
FROM pEmployee INNER JOIN aTranLine
ON pEmployee.nKey = aTranLine.kEntity2
WHERE kPayItem IN (
SELECT nKey FROM pPayrollItem
WHERE sPayItemSort < '30'
)
AND (dDate >= #1/1/2012#)
AND (dDate <= #12/31/2012#)
GROUP BY sName, sSSAN
ORDER BY sName
--------------------------------------
WHERE (kPayItem IN (
SELECT nKey FROM pPayrollItem
WHERE sPayItemType
IN ('Salary', 'Wage', 'Addition')))
SELECT
sPhone AS PhoneNumber,
sEntityType AS CompanyType,
sFullName AS Name,
sBillCity AS City, sBillState AS State
FROM cCustJob
WHERE sPhone IS NOT NULL
UNION
SELECT
sPhone AS PhoneNumber,
'Vendor' AS CompanyType,
sName AS Name,
sCity AS City, sState AS State
FROM vVendor
WHERE sPhone IS NOT NULL
UNION
SELECT
sPhone AS PhoneNumber,
'Employee' AS CompanyType,
sName AS Name,
sCity AS City, sState AS State
FROM pEmployee
WHERE sPhone IS NOT NULL
ORDER BY PhoneNumber
SELECT
pEmployee.nKey,
pEmployee.sEmplType,
sName,
rVacHoursAvail,
rSickHoursAvail,
MIN(dDate) AS FirstTransDate,
MAX(dDate) AS LastTransDate,
COUNT(dDate) AS CountTrans,
(LastTransDate - FirstTransDate) / CountTrans
AS Avg_Days_Between_Docs
FROM pEmployee INNER JOIN aTranLine
ON pEmployee.nKey = aTranLine.kEntity
WHERE pEmployee.bInactive = False
AND bDocMain = True
GROUP BY pEmployee.nKey, pEmployee.sEmplType,
sName, rVacHoursAvail, rSickHoursAvail
HAVING COUNT(dDate) > 0
SELECT sName,
dDate,
DLookup("sPayItemType", "pPayrollItem",
"nKey = " & kPayItem) As sPayItemType,
DLookup("sName", "pPayrollItem",
"nKey = " & kPayItem) As PayItem,
mAmount
FROM pEmployee INNER JOIN aTranLine
ON pEmployee.nKey = aTranLine.kEntity2
where (kPayItem IN (
SELECT nKey FROM pPayrollItem
WHERE sPayItemSort < '35'
))
AND sTranType = 'PayCheck'
AND mAmount <> 0
AND YEAR(dDate) in (2009, 2010, 2011,
2012, 2013, 2014)
ORDER BY sName, dDate