Accumulate Field Values using Groovy -
i have follwing columns kpi_period
, kpi_value
, want accomplish new column named kpi_output
using groovy.
the logic accomplish kpi_output
adding values of kpi_value
. in other words, apr
kpi_output
same kpi_value
, it's first month. may kpi_output
value kpi_value
s of apr
, may
. jun
kpi_output
kpi_value
s of apr
, may
, , june
. jul
kpi_output
value kpi_value
of apr
, may
, jun
, , jul
- , on....
kpi_period kpi_value kpi_output apr 33091 33091 may 29685 62776 jun 31042 93818 jul 32807 126625 aug 32782 159407 sep 34952 194359 oct 32448 226807 nov 31515 258322 dec 24639 282961 jan 25155 308116 feb 31320 339436 mar 33091 372527
how can accomplish using groovy?
here go:
def input = """kpi_period kpi_value apr 33091 may 29685 jun 31042 jul 32807 aug 32782 sep 34952 oct 32448 nov 31515 dec 24639 jan 25155 feb 31320 mar 33091 """ def splitted = input.split('\n')[1..-1] sum = 0 def transformed = splitted.collect { it.split(/\s+/)*.trim() }.inject([]) { res, curr -> sum += curr[1].tointeger() curr << sum.tostring() res << curr } println "kpi_period kpi_value kpi_output" transformed.each { println "${it[0].padleft(10)} ${it[1].padleft(12)} ${it[2].padleft(12)}" }
hope it's clear, if not sense free ask.
groovy
No comments:
Post a Comment