![]() |
![]() |
Technical Support
|
![]() |
This section contains queries that involve inventory and fixed assets. Inventory 'items' include many types of items that are not intuitively obvious - such as services, tax items, and groups. Fixed assets are stored along with inventory items - although they have substantially more fields.
There are a lot of example queries in this section. Examples include reports of all items on hand, all accounts assigned to inventory items, service items used in time lines, all items currently on order sorted by due date, comparison of on hand count vs. sum of adding or deleting transactions for each stocked item.
An inventory item will be one of 12 types:
"Service", "InvtPart", "InvtAssy", "NonInvtPart", "FixedAsset", "OthCharge",
"SubTotal", "Group", "Discount", "Payment", "TaxItem", "TaxGroup"
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 vInvtItem
ORDER BY sItemSort, sItemType, sFullName
SELECT *
FROM aTranLine
WHERE sItemType = 'InvtPart'
ORDER BY kDoc, nLine
SELECT *
FROM vInvtItem
WHERE bInactive = False
ORDER BY sItemSort, sFullName
SELECT nKey, sFullName, sManufPart,
dPurch, dSold, sLocation, sSerialNumber,
dWarrExpire,
FORMAT(rCostBasis, "0.00") AS CostBasis
FROM vInvtItem
WHERE sItemType = "FixedAsset"
ORDER BY dPurch
SELECT
sItemType,
COUNT(sItemType) as Count_ItemType
FROM vInvtItem
GROUP BY sItemType
ORDER BY COUNT(sItemType) DESC
SELECT dDate, sTranType, kDoc,
nLine, bDocMain, sDocNum,
kAcct, sAcctType, mAmount, bNoPost,
kEntity, sEntityType, kEntity2, sEntity2Type,
kItem, sItemType, rItemCount, rUnitPrice,
bUnitPricePcnt, kClass, bAutoTax, kSalesTax,
rCountMisc, sMemo80, sBillableStatus, dService
FROM aTranLine
WHERE sItemType IS NOT NULL
ORDER BY dDate, sTranType, kDoc, nLine
---------------------------------------
# these should always be NULL or 0
# so they were deleted from list above
SELECT * FROM aTranLine
WHERE kItem IS NOT NULL
AND (mBalanceDue <> 0
OR mStillOwing <> 0
OR dPaidInFull IS NOT NULL
OR kPayMeth IS NOT NULL
OR kPayItem IS NOT NULL
OR sPayItemType IS NOT NULL
OR dPayThrough IS NOT NULL
OR mWageBase <> 0
OR mPayTaxable <> 0
)
SELECT sFullName, sItemType,
rOnHandCount,
FORMAT(rEnteredCost,"0.00") AS EnteredCost,
FORMAT(rAverageCost, "0.00") AS AverageCost,
FORMAT(rUnitPrice, "0.00") AS UnitPrice,
FORMAT(((rUnitPrice-rAverageCost)/rUnitPrice*100),
"0") & "%" As ProfitMargin,
FORMAT(rOnHandCount * rAverageCost, "0.00")
AS OnHandValue,
FORMAT(rOnOrder, "0.00") AS OnOrder,
FORMAT(rOnSalesOrder, "0.00") AS OnSalesOrder
FROM vInvtItem
WHERE
sItemType = 'InvtPart'
AND rOnHandCount IS NOT NULL
AND rAverageCost > 0
AND rUnitPrice > 0
ORDER BY sFullName
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 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
aAccount.sFullName,
aAccount.sAcctType,
aTranLine.sTranType,
aTranLine.bNoPost,
aTranLine.kDoc,
aTranLine.nLine,
aTranLine.sLineRoles,
vInvtItem.sFullName,
vInvtItem.sItemType,
aTranLine.mAmount
FROM
(aAccount RIGHT JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct)
INNER JOIN vInvtItem
ON aTranLine.kItem = vInvtItem.nKey
ORDER BY kDoc, nLine
---------------------------------------
add?: WHERE aTranLine.sTranType = 'CustInvc'
SELECT
dDate, sTranType, kDoc, nLine,
sFullName, rItemCount,
aTranLine.rUnitPrice, mAmount,
bNoPost, sLineRoles
FROM vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE vInvtItem.sFullName = 'Wood Door:Exterior'
# AND bNoPost = False
ORDER BY kDoc, nLine
-----------------------------------------
WHERE vInvtItem.nKey = 22
SELECT
sFullName,
dDue,
rItemCount,
aTranLine.rUnitPrice
FROM vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE aTranLine.sTranType = 'PurchOrd'
AND aTranLine.bReceived = False
AND aTranLine.bDocMain = False
ORDER BY dDue
SELECT vInvtItem.nKey,
vInvtItem.sItemType,
sFullName,
rOnHandCount,
SUM(rItemCount) AS SumItemCount,
rOnHandCount - SUM(rItemCount) AS Difference,
COUNT(rItemCount) AS CountItemCount
FROM vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE bNoPost = False
AND vInvtItem.sItemType
IN('InvtPart', 'InvtAssy')
GROUP BY vInvtItem.nKey, vInvtItem.sItemType,
sFullName, rOnHandCount
ORDER BY sFullName
SELECT dDate, sTranType, kItem, sFullName,
vInvtItem.sItemType, rItemCount,
aTranLine.rUnitPrice, mAmount,
kAcct, sAcctType
FROM vInvtItem LEFT JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE rItemCount <= 0
AND bNoPost = False
AND (vInvtItem.sItemType = 'InvtPart'
or vInvtItem.sItemType = 'NonInvtPart')
ORDER BY sTranType, dDate
SELECT dDate, sTranType,
kAcct, sAcctType, -mAmount AS Amount,
cCustJob.sFullName AS CustJobName,
vVendor.sName AS VendorName,
vInvtItem.sName AS TaxName, aTranLine.rUnitPrice,
sMemo80
FROM ((aTranLine INNER JOIN vInvtItem
ON aTranLine.kItem = vInvtItem.nKey)
INNER JOIN vVendor
ON aTranLine.kEntity = vVendor.nKey)
INNER JOIN cCustJob
ON aTranLine.kEntity2 = cCustJob.nKey
WHERE aTranLine.sItemType = 'TaxItem'
AND bNoPost = False
ORDER BY dDate, sTranType, kDoc, nLine
SELECT kItem, vInvtItem.sFullName,
aTranLine.sItemType, dDate, sTranType,
rItemCount, aTranLine.rUnitPrice, mAmount,
aAccount.sFullName, aAccount.sAcctType,
dPurch, sVendor, sAssetDescr, rCostBasis
FROM
(vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem)
INNER JOIN aAccount
ON aTranLine.kAcct = aAccount.nKey
WHERE bNoPost = False
AND (aTranLine.sItemType IN ("FixedAsset"))
ORDER BY vInvtItem.sFullName
SELECT vInvtItem.sItemType, kItem, sFullName,
FORMAT(-SUM(rItemCount * aTranLine.rUnitPrice),
"0.00")
AS TotalSales
FROM vInvtItem LEFT JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE DATEPART("yyyy", dDate) IN (2013)
AND vInvtItem.sItemType
IN ("Service", "InvtPart", "NonInvtPart")
GROUP BY vInvtItem.sItemType, kItem, sFullName
SELECT
aTranLine.sTranType,
aTranLine.kDoc,
aTranLine.nLine,
vInvtItem.sItemType,
aTranLine.kItem,
vInvtItem.sFullName,
aTranLine.rItemCount,
aTranLine.rUnitPrice,
aTranLine.bUnitPricePcnt,
aTranLine.kAcct,
aAccount.sFullName,
aTranLine.sAcctType,
aTranLine.mAmount
FROM
(aAccount RIGHT JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct)
LEFT JOIN vInvtItem
ON aTranLine.kItem = vInvtItem.nKey
WHERE aTranLine.sTranType = 'InvAdjst'
AND
aTranLine.bNoPost = False
ORDER BY kDoc, nLine
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
SELECT sTranType, dDate,
kItem, vInvtItem.sFullName, vInvtItem.sItemType,
rItemCount, aTranLine.rUnitPrice, mAmount,
kAcct, aAccount.sFullName, aTranLine.sAcctType
FROM
(aAccount INNER JOIN aTranLine
ON aAccount.nKey = aTranLine.kAcct)
INNER JOIN vInvtItem
ON vInvtItem.nKey = aTranLine.kItem
WHERE bNoPost = False
AND rItemCount >= 0
AND ((vInvtItem.sItemType = 'InvtPart'
OR vInvtItem.sItemType = 'NonInvtPart'))
# ORDER BY vInvtItem.sFullName, dDate
ORDER BY sTranType
SELECT kItem, vInvtItem.sFullName,
aTranLine.sItemType, dDate, sTranType,
aTranLine.kDoc, aTranLine.nLine, rItemCount,
aTranLine.rUnitPrice, mAmount, kAcct,
aAccount.sFullName, aAccount.sAcctType
FROM
(vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem)
INNER JOIN aAccount
ON aTranLine.kAcct = aAccount.nKey
WHERE bNoPost = False
AND (aTranLine.sItemType IN ("InvtAssy"))
ORDER BY vInvtItem.nKey, aTranLine.kDoc,
aTranLine.nLine
TRANSFORM FORMAT(AVG(-rItemCount), "0.0")
SELECT vInvtItem.sFullName
FROM vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE vInvtItem.sItemType = 'Service'
AND bNoPost = False
GROUP BY vInvtItem.sFullName
PIVOT YEAR(dDate) & '-Q' & FORMAT(dDate, "q")
----------------------
for monthly:
PIVOT YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
TRANSFORM SUM(-mAmount)
SELECT YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
AS Month
FROM vInvtItem INNER JOIN aTranLine
ON vInvtItem.nKey = aTranLine.kItem
WHERE vInvtItem.sItemType = 'Service'
AND bNoPost = False
GROUP BY YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
PIVOT vInvtItem.sFullName
----------------------
to swap rows with columns, change these
(note that GROUP BY needs to be same as SELECT)
SELECT vInvtItem.sFullName
GROUP BY vInvtItem.sFullName
PIVOT YEAR(dDate) & '-' & FORMAT(dDate, 'mm')
for quarterly, GROUP BY:
YEAR(dDate) & '-Q' & FORMAT(dDate, "q")