![]() |
![]() |
Technical Support
|
![]() |
This section contains queries involving accounts. Some are simple, such as queries that show simple account fields. Other are more complex, such as queries that show the level of activity for each account, first and last dates of transactions posted, and accumulated account totals. There are queries to make sure that account balances exactly match the sums of transactions.
This category also includes several easily modified versions of profit / loss reports, balance sheets, etc. There are also many queries that separate the different types of accounts and queries for listing accounts that are not being used - as well as budget and class reports.
A unique feature is OfficeQ's ability to extract the gross profit for each transaction. Internally, for each transaction - sums of all transaction lines for P&L accounts are summed - which basically shows the amount of gross profit per transaction (fields mSumIncome and mSumExpense). These can be quickly merged to show profitability by any imaginable criteria.
Some of these queries require that certain QuickBooks features be used. For example, there are queries that categorize transactions by the income tax line (on federal forms) assigned to the account. This requires that each expense account have the proper federal form and line associated with it.
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 aAccount
ORDER BY sAcctSort, sFullName
SELECT sFullName, sAcctType
FROM aAccount
WHERE sAcctType = 'AcctRec';
SELECT sFullName, sAcctType,
sAcctNum, sAcctSort, nReportSort
FROM aAccount
ORDER BY sAcctSort, nReportSort;
SELECT kAcct, nYear, nMonth, mAmount
FROM aBudget
ORDER BY kAcct, nYear, nMonth
SELECT
sAcctSort,
nKey,
sFullName,
nYear,
nMonth,
mAmount
FROM aAccount INNER JOIN aBudget
ON aAccount.nKey = aBudget.kAcct
ORDER BY sAcctSort, sFullName, nYear, nMonth
SELECT
sCashFlowType,
sAcctType,
sFullName,
nTaxLine,
sTaxLineName
FROM aAccount
WHERE (nTaxLine IS NOT NULL
AND nTaxLine <> 0)
OR
(sCashFlowType <> 'None'
AND sCashFlowType <> 'NotApplicable')
ORDER BY sAcctSort, sAcctNum
SELECT nTaxLine,
sTaxLineName,
sAcctType,
sCashFlowType,
COUNT(*) AS Count_Of_Accounts
FROM aAccount
WHERE nTaxLine IS NOT NULL
AND nTaxLine <> 0
GROUP BY nTaxLine, sTaxLineName,
sAcctType, sCashFlowType
SELECT
DLOOKUP(
"sFullName",
"aAccount",
"nKey = " & kAcct)
AS BankAccount,
sDocNum, dDate, sTranType,
sEntityType, -mAmount AS Amount
FROM aTranLine
WHERE sAcctType = 'Bank'
AND mAmount < 0
ORDER BY
DLOOKUP("sFullName", "aAccount",
"nKey = " & kAcct),
dDate, sDocNum
SELECT
aAccount.sFullName,
aAccount.sAcctType,
sAcctNum,
sFullName,
sTranType,
dDate,
mAmount
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE aTranLine.bNoPost = False
AND sFullName LIKE 'Auto*'
AND YEAR(dDate) IN (2012)
ORDER BY sAcctSort, sFullName, aTranLine.nKey
SELECT
sAcctSort,
nKey,
sFullName,
nYear,
nMonth,
mAmount
FROM aAccount INNER JOIN aBudget
ON aAccount.nKey = aBudget.kAcct
ORDER BY sAcctSort, sFullName, nYear, nMonth
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
aAccount.sAcctType,
sAcctNum,
sFullName,
aAccount.sAcctType,
sSpecAcct,
sDescr,
COUNT(dDate) AS CountOfTrans
FROM aAccount LEFT JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
GROUP BY sAcctSort,
nReportSort, aAccount.sAcctType,
sAcctNum, sFullName, aAccount.sAcctType,
sSpecAcct, sDescr
HAVING COUNT(dDate) = 0
SELECT
nKey, sAcctType, sAcctSort,
sAcctNum, sFullName,
(SELECT SUM(mAmount) FROM aTranLine
WHERE bNoPost = False
AND kAcct = aAccount.nKey
AND dDate <= #12/31/2010#) AS Bal_123110,
(SELECT SUM(mAmount) FROM aTranLine
WHERE bNoPost = False
AND kAcct = aAccount.nKey
AND dDate <= #12/31/2011#) AS Bal_123111,
Bal_123111 - Bal_123110 AS Difference
FROM aAccount
WHERE sAcctSort <= '50'
ORDER BY sAcctSort, sAcctNum
SELECT sTaxLineName,
sCashFlowType,
SUM(aTranLine.mAmount) AS SumTrans
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE aTranLine.bNoPost = False
AND sTaxLineName IS NOT NULL
AND YEAR(dDate) IN (2012)
GROUP BY sTaxLineName, sCashFlowType
SELECT
aAccount.sAcctType, sFullName,
sSpecAcct, dDate, mAmount,
kDoc, sTranType
FROM aAccount LEFT JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE sAcctSort >= '50'
AND dDate
BETWEEN #12/15/2013# AND #12/17/2013#
AND aTranLine.bNoPost = False
ORDER BY aTranLine.nKey
SELECT aClass.sFullName,
sTranType, dDate, kDoc, nLine, sAcctSort,
sAcctNum, aAccount.sFullName,
aAccount.sAcctType, mAmount
FROM
(aClass INNER JOIN aTranLine
ON aClass.nKey = aTranLine.kClass)
INNER JOIN aAccount
ON aAccount.nKey = aTranLine.kAcct
WHERE bNoPost = False
ORDER BY sAcctNumaTranLine.nKey
SELECT aAccount.sAcctType, sFullName, mBalance,
SUM(mAmount) AS SumOfAmount,
mBalance - SUM(mAmount) AS Difference,
COUNT(mAmount) AS CountOfAmount
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE aTranLine.bNoPost = False
GROUP BY sAcctSort, aAccount.sAcctType,
sFullName, mBalance
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 aTranLine.kClass IS NULL
ORDER BY aTranDoc.sTranType,
aTranDoc.dDate,
Doc_nKey DESC
SELECT
aAccount.nKey,
aAccount.sAcctType,
sFullName,
MIN(dDate) AS FirstTransDate,
MAX(dDate) AS LastTransDate,
COUNT(dDate) AS CountTrans
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
GROUP BY sAcctSort, aAccount.nKey,
aAccount.sAcctType, sFullName
HAVING COUNT(dDate) > 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
aAccount.nKey,
aAccount.sAcctType,
sAcctSort,
nReportSort,
sAcctNum,
sFullName,
SUM(IIF(MID(sAcctSort, 3, 1)="+",
mAmount, -mAmount)) AS Balance
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE aTranLine.bNoPost = False
AND
aAccount.sAcctSort < '50'
AND
aTranLine.dDate <= #12/15/2013#
GROUP BY aAccount.nKey, aAccount.sAcctType,
sAcctSort, sAcctNum, nReportSort,
sFullName
HAVING COUNT(dDate) > 0
ORDER BY sAcctSort, nReportSort, sAcctNum;
SELECT
aAccount.kAcct,
aAccount.sFullName,
aBudget.nYear,
aBudget.nMonth,
aBudget.mAmount AS Budgeted,
SUM(aTranLine.mAmount) AS Actual
FROM
(aBudget INNER JOIN aTranLine
ON aBudget.kAcct = aTranLine.kAcct)
INNER JOIN aAccount
ON aTranLine.kAcct = aAccount.nKey
GROUP BY
aAccount.kAcct,
aAccount.sFullName,
aBudget.nYear,
aBudget.nMonth,
aBudget.mAmount
ORDER BY
aAccount.kAcct,
aAccount.sFullName,
aBudget.nYear,
aBudget.nMonth
---------------------------------------------
WHERE YEAR(dDate) IN (2011)
SELECT
sTranType,
dDate,
sDocNum,
xEntity.sFullName,
mAmount,
sAcctNum,
sAcctSort,
aAccount.sAcctType,
nReportSort,
aAccount.sFullName
FROM (aAccount INNER JOIN aTranLine
ON aTranLine.kAcct = aAccount.nKey)
INNER JOIN xEntity
ON aTranLine.kEntity = xEntity.nKey
WHERE sAcctSort >= '50' AND sAcctSort < '90'
AND YEAR(dDate) IN (2013)
AND bNoPost = False
ORDER BY sAcctSort, nReportSort, dDate
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(aTranLine.mAmount)
SELECT sTaxLineName, sCashFlowType
FROM aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct
WHERE aTranLine.bNoPost = False
AND sTaxLineName IS NOT NULL
AND YEAR(dDate) IN (2011, 2012, 2013)
GROUP BY sTaxLineName, sCashFlowType
PIVOT YEAR(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")