Yes, you loop through the records and use an array to collect the results. Here is a set of routines you can drop into a button in Customers or in the example XResultSet dsr I gave you. It collects the amounts by State. There are several small generic utility routines for working with the array plus the main body of code. Let me know if you need any of it clarified.
var vRS as Int
var vState as String
var vOldState as String
var vAmount as Money
var vPos as Int
var vLoop as Int
var vCount as Int
var vStateArray as Array[52, 2] of String
// Initializes all array elements to ""
SUBROUTINE ClearArray()
var lLoop1 as Int
var lLoop2 as Int
var lMax1 as Int
var lMax2 as Int
lMax1 = DimLimit(1, vStateArray)
lMax2 = DimLimit(2, vStateArray)
For lLoop1 = 1 To lMax1
For lLoop2 = 1 To lMax2
vStateArray[lLoop1, lLoop2] = ""
Next
Next
END SUBROUTINE
// Utility routine to display filled array
SUBROUTINE EmitArray()
var lLoop as Int
var lMax as Int
lMax = DimLimit(1, vStateArray)
WriteLn(@Text(50, "-"))
For lLoop = 1 To lMax
If (vStateArray[lLoop, 1] + vStateArray[lLoop, 2]) <> ""
{
WriteLn(vStateArray[lLoop, 1] + ": $" + @Decimals(vStateArray[lLoop, 2], 2))
}
Next
END SUBROUTINE
// Find array position of matching entry.
// Create if not found.
FUNCTION GetArrayPos(aVal as String) as Int
var lRet as Int
var lLoop as Int
var lMax as Int
lRet = 0
lMax = DimLimit(1, vStateArray)
lLoop = 1
While (lLoop <= lMax) And (lRet = 0)
{
If aVal = vStateArray[lLoop, 1]
{
lRet = lLoop
}
lLoop = lLoop + 1
}
// If not found, find a blank slot and use it.
If aVal <> ""
{
If lRet = 0
{
lRet = GetArrayPos("")
If lRet > 0
{
vStateArray[lRet, 1] = aVal
}
}
}
Return lRet
END FUNCTION
// Get all Customer records
vRS = @XResultSetSearch(@FN, "Customers", SEARCH_MODE_AND,SEARCH_SYNTAX_QA, "")
If vRS > -1
{
// Sort by state for efficiency
XResultSetSort(vRS, "State:-1")
vCount = @XResultSetTotal(vRS)
ClearArray()
vOldState = "something that never occurs"
vPos = 0
For vLoop = 1 To vCount
XResultSetCurrentPosition(vRS, vLoop)
vState = @XResultSetValue(vRS, "State")
If vState <> vOldState
{
vPos = GetArrayPos(vState)
vOldState = vState
}
If vPos > 0
{
vAmount = @XResultSetValue(vRS, "Currency")
If vAmount > 0
{
vAmount = vAmount + @ToMoney(vStateArray[vPos, 2])
vStateArray[vPos, 2] = @Str(vAmount)
}
}
Else
{
WriteLn("Error: Failed to get array position for " + vState)
}
Next
XResultSetClose(vRS)
EmitArray()
}
Else
{
@MsgBox("Error: failed to open result set.", "", "")
}