![]() |
![]() |
Technical Support
|
![]() |
These queries involve transactions that are not particularly associated with the other groups of queries. Transactions are the most difficult part of QuickBooks to master - but also the most powerful. Because of that, we include many queries whose purpose is educational rather than producing directly useful output.
There are 29 different transaction types. Some fields are identical across all types. Some are used by a subset of the types, and a few are unique to a particular type. The best way to learn transactions is to extract the data from a small QuickBooks file, print out the table / field listings on this site, and experiment by running the queries here and observing results.
There are 2 complex SQL features that need to be mastered to produce the most powerful transaction queries. One in the SQL join - and the other is the pivot. There are many examples here to get you started.
There are also directly useful queries here - such as transaction statistics, queries for detecting incomplete data (such as class not entered or wrong quantity counts), queries showing activities per day of week, queries calculating profit dollars several ways, and listings of transactions posted to retained earnings.
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 aTranDoc
ORDER BY dDate
SELECT *
FROM aTranLine
ORDER BY dDate, sTranType
--------------------------------------
ORDER BY sTranType, dDate
SELECT dDate, sTranType, kDoc,
nLine, bDocMain, sDocNum,
kAcct, sAcctType, mAmount,
kEntity, sEntityType, kEntity2, sEntity2Type,
kClass, sMemo80
FROM aTranLine
WHERE sTranType IN ('Journal')
ORDER BY dDate, sTranType, kDoc, nLine
SELECT nKey, dDate, sTranType, sDocNum, kDoc, nLine,
sAcctType, kClass
FROM aTranLine
WHERE sTranType IN
('Journal')
ORDER BY kDoc, nLine
------------------------------------------------
WHERE sTranType IN
('CashSale', 'CustInvc')
SELECT nKey, dDate, sTranType, kAcct, sAcctType,
mAmount, bNoPost, sEntityType, sItemType,
dService
FROM aTranLine
WHERE dService IS NOT NULL
ORDER BY dService
SELECT *
FROM aTranLink
ORDER BY dDate
SELECT sTranTypeFrom, sTranTypeTo,
COUNT(*) As Count
FROM aTranLink
WHERE sLinkType = "Amt"
AND NOT (
(sTranTypeFrom IN
("CustInvc", "CustChrg", "VendBill"))
OR (sTranTypeTo IN
("CustInvc", "CustChrg", "VendBill"))
)
GROUP BY sTranTypeFrom, sTranTypeTo
------------------------------
WHERE sLinkType = "Quan"
SELECT dDate, sTranType, kDoc,
nLine, bDocMain, sDocNum,
kAcct, sAcctType, mAmount, bNoPost,
kEntity, sEntityType, kEntity2, sEntity2Type,
kItem, sItemType, rItemCount, rUnitPrice,
bUnitPricePcnt,
mBalanceDue, mStillOwing, dPaidInFull,
kClass, kPayMeth, bAutoTax, kSalesTax, rCountMisc,
kPayItem, sPayItemType, dPayThrough,
mWageBase, mPayTaxable, sMemo80,
sBillableStatus, dService
FROM aTranLine
ORDER BY dDate, sTranType, kDoc, nLine
--------------------------------------------
WHERE sTranType IN ('VendBill', 'VendCred')
SELECT dDate, sTranType, kDoc,
nLine, bDocMain, sDocNum,
kAcct, sAcctType, mAmount, bNoPost,
kEntity, sEntityType, kEntity2, sEntity2Type,
kItem, sItemType, rItemCount, rUnitPrice,
bUnitPricePcnt, mBalanceDue, mStillOwing,
dPaidInFull, kClass, kPayMeth, bAutoTax,
kSalesTax, rCountMisc, sMemo80,
sBillableStatus, dService
FROM aTranLine
ORDER BY dDate, sTranType, kDoc, nLine
SELECT dDate, sTranType, kDoc, kClass
FROM aTranLine
WHERE bDocMain = True
ORDER BY sTranType, dDate, nKey
-----------------------------------
AND NOT(ISNULL(kClass))
SELECT *
FROM aTranLine
WHERE dDate BETWEEN #10/1/2011# AND #12/31/2011#
ORDER BY nKey
SELECT
YEAR(dDate) & '-'
& FORMAT(dDate, "mmm") as Month,
COUNT(*) as Count_TranDoc
FROM aTranDoc
GROUP BY
YEAR(dDate) & '-' & FORMAT(dDate, "mm"),
YEAR(dDate) & '-' & FORMAT(dDate, "mmm")
ORDER BY YEAR(dDate) & '-' & FORMAT(dDate, "mm")
SELECT
sTranType,
COUNT(sTranType) as CountOfTranType
FROM aTranDoc
GROUP BY sTranType
---------------------------------------------------
SELECT
sTranType,
COUNT(sTranType) as CountOfTranType
FROM aTranLine
GROUP BY sTranType
SELECT MIN(dDate) as DateFirst,
MAX(dDate) as DateLast,
COUNT(dDate) AS Count_TranDoc
FROM aTranDoc
SELECT INT((DATE() - dDate)/91.3) as Quarters_Ago,
COUNT(*) AS Count_During_Quarter
FROM aTranDoc
GROUP BY INT((DATE() - dDate)/91.3)
SELECT sAcctType, sTranType,
COUNT(*) AS `Count Of AcctType_TranType`
FROM aTranLine
WHERE bDocMain=True
GROUP BY sAcctType, sTranType
SELECT sTranTypeFrom, sTranTypeTo,
COUNT(*) As Count
FROM aTranLink
WHERE sLinkType = "Amt"
GROUP BY sTranTypeFrom, sTranTypeTo
------------------------------
WHERE sLinkType = "Quan"
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 dDate, sTranType, kDoc,
nLine, bDocMain, sDocNum,
kAcct, aAccount.sFullName,
aTranLine.sAcctType, mAmount,
kEntity, sEntityType, kEntity2, sEntity2Type,
rUnitPrice, kClass,
kPayItem, sPayItemType, dPayThrough,
mWageBase, mPayTaxable
FROM aTranLine INNER JOIN aAccount
ON aTranLine.kAcct = aAccount.nKey
WHERE
sEntityType = 'Employee'
OR
sEntity2Type = 'Employee'
# ORDER BY dDate, sTranType, kDoc, sLine
ORDER BY sTranType, kDoc, nLine
SELECT
CHOOSE(FORMAT(dDate, "w"),
"Sun","Mon","Tue","Wed","Thu",
"Fri","Sat","Sun")
AS DayOfWeek,
COUNT(*) AS Count_On_This_Weekday
FROM aTranDoc
WHERE sTranType IN
('CashSale', 'CustInvc')
GROUP BY
FORMAT(dDate, "w")
TRANSFORM COUNT(dDate)
SELECT sTranType
FROM aTranDoc
GROUP BY sTranType
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, "q")
--------------------------------- variations:
PIVOT YEAR(dDate)
WHERE YEAR(dDate) IN (2001, 2002, 2003, 2004,
2005, 2006, 2007, 2008, 2009, 2010, 2011,
2012, 2013)
SELECT aTranDoc.sTranType,
COUNT(*) AS TranTypeCount_CustJob
FROM aTranLine INNER JOIN aTranDoc
ON aTranLine.kDoc = aTranDoc.nKey
WHERE
aTranLine.bDocMain = True
AND
aTranLine.bNoPost = False
AND
(aTranLine.kEntity
IN (SELECT nKey FROM cCustJob)
OR
aTranLine.kEntity2
IN (SELECT nKey FROM cCustJob))
GROUP BY aTranDoc.sTranType
TRANSFORM SUM(-mSumIncome - mSumExpense)
SELECT YEAR(dDate) & '-Q' & FORMAT(dDate, 'q')
AS TimePeriod
FROM aClass INNER JOIN aTranLine
ON aTranLine.kClass = aClass.nKey
WHERE aTranLine.bDocMain = True
GROUP BY YEAR(dDate) & '-Q' & FORMAT(dDate, 'q')
PIVOT aClass.sFullName
SELECT aTranDoc.dDate,
aTranDoc.nKey AS Doc_nKey,
aTranDoc.sTranType,
aTranLine.kEntity,
aTranLine.sEntityType,
-mSumIncome AS Income,
mSumExpense AS Expense,
(-mSumIncome - mSumExpense) AS Profit
FROM aTranDoc LEFT JOIN aTranLine
ON aTranDoc.nKey = aTranLine.kDoc
WHERE YEAR(aTranDoc.dDate) IN (2013)
AND
aTranLine.bDocMain = True
AND
aTranLine.bNoPost = False
AND
(mSumIncome <> 0
OR
mSumExpense <> 0)
TRANSFORM COUNT(dDate)
SELECT sTranType
FROM aTranDoc
WHERE YEAR(dDate) IN (2013)
GROUP BY sTranType
PIVOT YEAR(dDate) & '-' & FORMAT(dDate, "mm")
--------------------------------- variations:
WHERE YEAR(dDate) IN (2011, 2012, 2013)
PIVOT YEAR(dDate)
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, "q")
SELECT
aTranLine.sTranType,
aTranLine.bNoPost,
aTranLine.kDoc,
aTranLine.nLine,
aTranLine.rItemCount,
aTranLine.rUnitPrice,
aTranLine.bUnitPricePcnt,
aTranLine.kItem,
vInvtItem.sFullName,
vInvtItem.sItemType,
aTranLine.mAmount,
aAccount.nKey,
aAccount.sFullName
FROM
(aAccount RIGHT JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct)
LEFT JOIN vInvtItem
ON aTranLine.kItem = vInvtItem.nKey
WHERE (kItem IS NOT NULL)
AND (aTranLine.rUnitPrice IS NOT NULL)
AND (rItemCount IS NULL)
AND (bNoPost = False)
ORDER BY vInvtItem.sItemType
---------------------------------------------
AND (bNoPost = False)
SELECT aTranDoc.dDate,
aTranDoc.sTranType,
aTranDoc.sDocNum,
aTranDoc.nKey AS Doc_nKey,
aAccount.sFullName AS Account,
aTranLine.mAmount
FROM
(aTranDoc INNER JOIN aTranLine
ON aTranDoc.nKey = aTranLine.kDoc)
INNER JOIN aAccount
ON aTranLine.kAcct - aAccount.nKey
WHERE aAccount.sSpecAcct = 'RetEarnings'
AND aTranLine.bNoPost = False
AND aTranLine.mAmount <> 0
AND aTranDoc.sTranType NOT IN
('CashSale', 'CustInvc',
'Paycheck', 'VendBill')
ORDER BY aTranDoc.sTranType, aTranDoc.dDate
TRANSFORM SUM(mAmount)
SELECT aAccount.sAcctType,
aAccount.sFullName
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE YEAR(dDate) IN (2011, 2012)
AND bNoPost = False
GROUP BY aAccount.sAcctSort,
aAccount.sAcctType,
aAccount.sFullName
------------------------------------
# PIVOT YEAR(dDate) & '-' & FORMAT(dDate, "mm")
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, "q")