![]() |
![]() |
Technical Support
|
![]() |
These queries involve jobs or employee time tracking. Even though jobs have fields identical to customers and each job has a customer as a parent - the focus here is on employee time tracking and time billing - and not financial (such as receivables).
Each record in the pTimeLine table is for a block of time - with info as to when it occurred, how long it was for, the customer the service was provided for, the employee or vendor who performed the service and what the actual service (inventory item) was - plus other fields.
There are queries for unbilled blocks of time, 'useage' queries for employee time, queries by customer, cross queries that intersect employee time, customer time, and service item, and any of the above separated by date boundaries.
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 pTimeLine
ORDER BY dDate, kEmplVend, kCustJob
# ORDER BY nKey
SELECT sBillStatus,
COUNT(*) AS Count_BillStatus
FROM pTimeLine
GROUP BY sBillStatus
SELECT
YEAR(dDate) & '-'
& FORMAT(dDate, "mmm") as Month,
COUNT(*) as Count_TimeLines
FROM pTimeLine
GROUP BY
YEAR(dDate) & '-' & FORMAT(dDate, "mm"),
YEAR(dDate) & '-' & FORMAT(dDate, "mmm")
ORDER BY YEAR(dDate) & '-' & FORMAT(dDate, "mm")
SELECT sItemType,
sFullName,
COUNT(*) AS CountOfBlocks,
SUM(rHours) AS TotalHours,
FORMAT(TotalHours / CountOfBlocks, "0.0")
As AverageHoursPerBlock
FROM vInvtItem INNER JOIN pTimeLine
ON vInvtItem.nKey = pTimeLine.kItem
GROUP BY sItemType, sFullName
----------------------------------------
SELECT sItemType,
sFullName,
COUNT(*) AS CountOfBlocks,
SUM(rHours) AS TotalHours,
FORMAT(TotalHours / CountOfBlocks, "0.0")
As AverageHoursPerBlock
FROM vInvtItem INNER JOIN pTimeLine
ON vInvtItem.nKey = pTimeLine.kItem
WHERE sBillStatus = 'Billable'
GROUP BY sItemType, sFullName
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
sFullName, sBillCity, sBillState,
cCustJob.sEntityType,
MIN(dDate) AS FirstDate,
MAX(dDate) AS LastDate,
COUNT(dDate) AS CountTrans
FROM cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE nLine = 0
GROUP BY sFullName, sBillCity, sBillState,
cCustJob.sEntityType
HAVING COUNT(dDate) > 0
SELECT pTimeLine.dDate,
cCustJob.sFullName,
vInvtItem.sItemType,
vInvtItem.sFullName,
pTimeLine.rHours,
sBillStatus
FROM
(pTimeLine INNER JOIN cCustJob
ON pTimeLine.kCustJob = cCustJob.nKey)
INNER JOIN vInvtItem
ON pTimeLine.kItem = vInvtItem.nKey
WHERE sBillStatus = 'Billable'
ORDER BY cCustJob.sFullName
--------------------------------------------
ORDER BY pTimeLine.dDate
SELECT
sFullName,
cCustJob.sEntityType,
COUNT(*) AS Count_Of_Sales,
-SUM(mSumIncome) AS SumIncome,
SUM(mSumExpense) AS SumExpense,
SumIncome - SumExpense AS Gross_Profit
FROM cCustJob LEFT JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE bNoPost = False
AND
aTranLine.bDocMain = True
GROUP BY sFullName, cCustJob.sEntityType
TRANSFORM SUM(rHours)
SELECT
cCustJob.sFullName AS Job,
xEntity.sEntityType AS EmplOrVend,
xEntity.sFullName AS EmployeeOrVendName
FROM ((pTimeLine INNER JOIN cCustJob
ON pTimeLine.kCustJob = cCustJob.nKey)
INNER JOIN xEntity
ON xEntity.nKey = pTimeLine.kEmplVend)
WHERE pTimeLine.sBillStatus = 'Billable'
AND YEAR(dDate) IN (2013)
GROUP BY
cCustJob.sFullName,
xEntity.sEntityType,
xEntity.sFullName
PIVOT YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
TRANSFORM SUM(rHours)
SELECT
xEntity.sEntityType AS EmplOrVend,
xEntity.sFullName AS EmplVendName,
cCustJob.sFullName AS CustJobName,
vInvtItem.sFullName AS Service,
pPayrollItem.sName AS PayItemName
FROM
((((pTimeLine LEFT JOIN cCustJob
ON cCustJob.nKey = pTimeLine.kCustJob)
LEFT JOIN xEntity
ON xEntity.nKey = pTimeLine.kEmplVend)
LEFT JOIN vInvtItem
ON vInvtItem.nKey = pTimeLine.kItem)
LEFT JOIN pPayrollItem
ON pPayrollItem.nKey = pTimeLine.kPayItem)
WHERE sBillStatus = 'NotBillable'
GROUP BY
xEntity.sEntityType,
xEntity.sFullName,
cCustJob.sFullName,
vInvtItem.sFullName,
pPayrollItem.sName
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, 'q')
SELECT dDate,
cCustJob.sFullName AS CustJobName,
xEntity.sEntityType & ' : ' & xEntity.sFullName
AS EmplVendName,
rHours
FROM
((pTimeLine LEFT JOIN cCustJob
ON cCustJob.nKey = pTimeLine.kCustJob)
LEFT JOIN xEntity
ON xEntity.nKey = pTimeLine.kEmplVend)
ORDER BY cCustJob.sFullName,
xEntity.sEntityType & ' : ' & xEntity.sFullName,
dDate
TRANSFORM SUM(rHours)
SELECT cCustJob.sFullName
FROM pTimeLine INNER JOIN cCustJob
ON pTimeLine.kCustJob = cCustJob.nKey
WHERE pTimeLine.sBillStatus = 'Billable'
# AND YEAR(dDate) IN (2013, 2014, 2015)
GROUP BY cCustJob.sFullName
PIVOT YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
TRANSFORM SUM(rHours)
SELECT cCustJob.sFullName
FROM pTimeLine INNER JOIN cCustJob
ON pTimeLine.kCustJob = cCustJob.nKey
# WHERE YEAR(dDate) IN (2012, 2013, 2014)
GROUP BY cCustJob.sFullName
# PIVOT YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, 'q')