Created
June 20, 2021 11:22
-
-
Save trycf/12e84787901a7560e672c4245d6a03b4 to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function getUngroupedRecords(required numeric rows) { | |
createRows = (number) => repeatString(",", rows).listToArray(",", true) | |
date = () => now().add("d", randRange(-365, 365)) | |
amount = () => randRange(1,10000) / 100 | |
fakedDbData = queryNew( | |
"settlementDate,LongTermGainLoss,ShortTermGainLoss", | |
"Date,Double,Double", | |
createRows(rows).map((_) => [date(), amount(), amount()]) | |
) | |
return fakedDbData | |
} | |
ungroupedRecords = getUngroupedRecords(20) | |
writeDump(ungroupedRecords) | |
groupedRecords = ungroupedRecords.reduce((grouped={}, row) => { | |
y = row.settlementDate.year() | |
m = row.settlementDate.month() | |
key = "#y#-#m#" | |
grouped[key] = grouped[key] ?: {stgl = 0, ltgl = 0} | |
grouped[key].stgl = grouped[key].stgl + row.ShortTermGainLoss | |
grouped[key].ltgl = grouped[key].ltgl + row.LongTermGainLoss | |
return grouped | |
}).reduce( | |
(records, key, values) => { | |
records.addRow({ | |
month = key.listLast("-"), | |
year = key.listFirst("-"), | |
ltgl = values.ltgl, | |
stgl = values.stgl | |
}) | |
return records | |
}, | |
queryNew("month,year,ltgl,stgl", "Integer,Integer,Double,Double") | |
).sort((r1, r2) => { | |
yearDiff = r1.year - r2.year | |
if (yearDiff != 0) { | |
return yearDiff | |
} | |
return r1.month - r2.month | |
}) | |
writeDump(groupedRecords) | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment