![]() |
![]() |
Technical Support
|
![]() |
Queries that involve customers, jobs, accounts receivables, and customer related transactions are in this section. The cCustJob table contains a record for each customer or job with around 50 fields.
Transaction types that involve customers or jobs are (alphabetically): CardChrg, CashSale, Check, CustCred, CustInvc, CustPymt, InvtAdjst, and Journal.
Using the aTranLine table, you can join together any combination of criteria for creating customers lists. For example, using cCustJob and/or aTranLine, here are a few random query examples:
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 cCustJob
ORDER BY sFullName
SELECT nKey, sFullName,
sEntityType, sCompanyName,
sFirstName, sLastName,
sBillState, sShipState, mBalance
FROM cCustJob
ORDER BY sFullName
SELECT sFullName, sEntityType,
mCreditLimit, mBalance,
mCreditLimit - mBalance AS Available
FROM cCustJob
WHERE mCreditLimit IS NOT NULL
ORDER BY sFullName
SELECT COUNT(sEmail) AS CountWithEmail,
COUNT(*) AS CountCustomers,
FORMAT((COUNT(sEmail) / COUNT(*)), "00.0%")
AS PercentWithEmail
FROM cCustJob
SELECT sAcctType, sTranType, nLine,
sEntityType, sEntity2Type, dDate,
dDue, mBalanceDue, mAmount
FROM aTranLine
WHERE sAcctType = 'AcctRec'
AND sEntityType = 'CustJob'
AND sTranType IN
('CustInvc', 'Check', 'CustPymt', 'CustCred')
AND bNoPost = False
ORDER BY nKey
# ORDER BY sTranType, nKey
SELECT
sTranType, sItemType,
COUNT(sTranType) AS CntTranType,
COUNT(sItemType) AS CntItemType
FROM aTranLine
WHERE
sTranType IN
('CashSale','CustInvc','CustChrg','CustCred')
AND
kItem IS NOT NULL
AND
bDocMain = False
AND
sEntity2Type = 'CustJob'
GROUP BY sTranType, sItemType
ORDER BY sTranType, sItemType
SELECT
cCustJob.nKey,
sFullName,
cCustJob.sEntityType,
sTranType,
kEntity,
mSumIncome,
mSumExpense,
mAmount
FROM
cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE cCustJob.sFullName LIKE 'A*'
AND bDocMain = True
AND bNoPost = False
ORDER BY cCustJob.sFullName, aTranLine.kDoc
SELECT
cCustType.sFullName AS CustType,
cCustJob.sFullName AS CustName,
sEntityType AS EntityType
FROM cCustJob LEFT JOIN cCustType
ON cCustJob.kCustType = cCustType.nKey
ORDER BY cCustType.sFullName, cCustJob.sFullName
SELECT
sPostalCode,
sCity,
sState,
COUNT(*) as CountZips
FROM aTranDoc
WHERE sTranType IN
('CustInvc', 'Check', 'CustPymt', 'CustCred')
GROUP BY sPostalCode, sCity, sState
ORDER BY sPostalCode
SELECT
sFullName,
cCustJob.sEntityType,
-SUM(mAmount) AS SalesVolume,
MIN(dDate) AS FirstTrans,
COUNT(*) AS Count_Of_Trans,
MAX(dDate) AS LastTrans
FROM cCustJob LEFT JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE bNoPost = False
AND
(aTranLine.sAcctType = 'Income'
OR
aTranLine.sAcctType = 'OthIncome')
GROUP BY sFullName, cCustJob.sEntityType
ORDER BY sFullName
----------------------------------------
ORDER BY -SUM(mAmount) DESC
SELECT cCustJob.sFullName, sAcctType,
dDate, sTranType, nLine, aTranLine.sEntityType,
dDate AS dSold, dDue, mAmount
FROM aTranLine INNER JOIN cCustJob
ON aTranLine.kEntity = cCustJob.nKey
WHERE (aTranLine.kAcct IN (
SELECT nKey
FROM aAccount
WHERE sAcctType = 'AcctRec'))
AND bNoPost = False
AND mBalance > 0
ORDER BY sFullName, dDate
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
sFullName, sTranType, sItemType,
COUNT(*) AS cnt_saleslines,
-SUM(mAmount) AS sum_mAmount
FROM cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity2
WHERE
sTranType IN
# sTranType NOT IN
('CashSale','CustInvc','CustChrg','CustCred')
AND
bDocMain = False
AND
sEntity2Type = 'CustJob'
GROUP BY sFullName, sTranType, sItemType
SELECT
sFullName,
cCustJob.sEntityType,
COUNT(dDate) AS CountOfTrans
FROM cCustJob LEFT JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
GROUP BY sFullName, cCustJob.sEntityType
HAVING COUNT(dDate) = 0
-------------------------------------------
HAVING COUNT(dDate) > 0
SELECT aTranDoc.dDate,
aTranDoc.sTranType,
aTranDoc.sFlags,
aTranLine.kAcct,
aTranLine.sAcctType,
aTranLine.kEntity,
aTranLine.sEntityType,
aTranLine.mAmount,
aTranLine.dPaidInFull,
aTranLine.mStillOwing
FROM aTranDoc INNER JOIN aTranLine
ON aTranDoc.nKey = aTranLine.kDoc
WHERE
(aTranLine.dPaidInFull IS NOT NULL
OR
aTranLine.mStillOwing > 0)
ORDER BY aTranDoc.sTranType
# aTranLine.bDocMain = True
# AND
# aTranLine.mAmount <> 0
# AND
# bNoPost = False
----------------------------------------------
AND
aTranLine.sTranType IN
('CustChrg', 'CustInvc', 'VendBill')
((aTranDoc.sFlags LIKE '*F*')
OR
(aTranLine.dPaidInFull IS NOT NULL)
OR
(aTranLine.mStillOwing <> 0))
SELECT cCustJob.nKey,
sFullName,
mBalance,
SUM(aTranLine.mAmount) AS SumOfmAmount,
mBalance - SUM(aTranLine.mAmount) AS Difference,
COUNT(aTranLine.mAmount) AS CountOfmAmount
FROM cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE (aTranLine.kAcct IN (
SELECT nKey
FROM aAccount
WHERE sAcctType = 'AcctRec' ))
AND
aTranLine.bNoPost = False
GROUP BY cCustJob.nKey, sFullName, mBalance
ORDER BY sFullName
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
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
SELECT
sFullName,
cCustJob.sEntityType,
-SUM(mSumIncome) AS SalesVolume
FROM cCustJob LEFT JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE bNoPost = False
AND
aTranLine.bDocMain = True
GROUP BY sFullName, cCustJob.sEntityType
HAVING -SUM(mSumIncome) <> 0
ORDER BY sFullName
----------------------------
ORDER BY -SUM(mSumIncome) DESC
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(mSumIncome) - SUM(mSumExpense)
SELECT cCustJob.sFullName,
cCustJob.sEntityType
FROM cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE YEAR(dDate) IN (2009, 2010, 2011, 2012, 2013)
AND bNoPost = False
AND bDocMain = True
GROUP BY cCustJob.sFullName,
cCustJob.sEntityType
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
SELECT
vInvtItem.sItemType,
vInvtItem.sFullName AS ServiceName,
cCustJob.sFullName,
-SUM(aTranLine.rItemCount) AS TotalUnits,
FORMAT(AVG(aTranLine.rUnitPrice), "$#,##0.00")
AS AvgUnitPrice
FROM
(cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity2)
INNER JOIN vInvtItem
ON aTranLine.kItem = vInvtItem.nKey
WHERE aTranLine.sItemType IN ("Service")
GROUP BY
vInvtItem.sItemType,
vInvtItem.sFullName,
cCustJob.sFullName
ORDER BY vInvtItem.sFullName,
CDBL(FORMAT(AVG(aTranLine.rUnitPrice), "0.00")),
cCustJob.sFullName
SELECT
cCustJob.nKey,
cCustJob.sFullName,
cCustJob.sEntityType,
vInvtItem.sFullName,
vInvtItem.sItemType,
-SUM(aTranLine.rItemCount) AS TotalUnits,
-SUM(aTranLine.rItemCount * aTranLine.rUnitPrice)
AS TotalPrice,
FORMAT(TotalPrice / TotalUnits, "0.00")
AS AveragePrice
FROM
(cCustJob INNER JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity2)
INNER JOIN vInvtItem
ON aTranLine.kItem = vInvtItem.nKey
WHERE cCustJob.sFullName LIKE 'C*'
AND
vInvtItem.sItemType IN
("InvtPart", "NonInvtPart")
AND aTranLine.bNoPost = False
GROUP BY
cCustJob.nKey,
cCustJob.sFullName,
cCustJob.sEntityType,
vInvtItem.sFullName,
vInvtItem.sItemType
ORDER BY cCustJob.sFullName, vInvtItem.sFullName
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(mSumIncome)
SELECT
sFullName,
cCustJob.sEntityType
FROM cCustJob LEFT JOIN aTranLine
ON cCustJob.nKey = aTranLine.kEntity
WHERE bNoPost = False
AND aTranLine.bDocMain = True
AND
YEAR(dDate) BETWEEN 2005 AND 2018
AND
mSumIncome <> 0
GROUP BY sFullName, cCustJob.sEntityType
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, "q")
------------------------------
YEAR(dDate) IN (2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012, 2013)
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')