Splunk Search

How to use streamstats to display the last current result?

desslerlee
Explorer

Hi all,

I am trying to use streamstats to display an event for a particular user, their current Payment Number for this month, and the subsequent Payment number for the next event. (Do note that I sort the date in reverse order).

 | sort  -TransactDate
  | streamstats current=f window=1 last("Payment Number") as NextPaymentNumber  
  | table  Username,  "TransactDate","Payment Number", NextPaymentNumber

I almost get the result I want as below:

 Username       TransactDate         Payment Number      NextPaymentNumber
    Adam                   2/2/2017               2                       3
    Adam                   2/1/2017               1                       2

However, what I need is something like below:

Username       TransactDate         Payment Number      NextPaymentNumber
     Adam                   2/3/2017               3                       3
    Adam                   2/2/2017               2                       3
    Adam                   2/1/2017               1                       2

Where the record on 2/3/2017 is the latest record, and the latest and maximum Payment number is 3.

Please advice how could I achieve that? Thanks!

1 Solution

DalJeanis
SplunkTrust
SplunkTrust

Try something like this

 your search that gets the payments (most recent first)
| reverse
| streamstats count as payno by Username
| reverse
| streamstats current=f window=1 last(payno) as nextpayno by Username
| eval nextpayno = coalesce(nextpayno, payno)

View solution in original post

DalJeanis
SplunkTrust
SplunkTrust

Try something like this

 your search that gets the payments (most recent first)
| reverse
| streamstats count as payno by Username
| reverse
| streamstats current=f window=1 last(payno) as nextpayno by Username
| eval nextpayno = coalesce(nextpayno, payno)

somesoni2
SplunkTrust
SplunkTrust

Do you want to add an extra row for each user with latest date (max date +1) and latest (max + 1) PaymentNumber?

0 Karma

mayurr98
Super Champion

i am getting the results with your query!

| makeresults 
|  eval raw="Adam2/2/201723 Adam2/1/201712 Adam2/3/201733" 
| makemv raw
| mvexpand raw 
| rex field=raw "(?<Username>[^\d]+)(?<TransactDate>\d\/\d\/\d{4})(?<PaymentNumber>\d)(?<NextPaymentNumber>\d)" 
|  fields- raw _time | sort- TransactDate | streamstats   last("PaymentNumber") as NextPaymentNumber 
| table Username, "TransactDate","PaymentNumber", NextPaymentNumber

can you try putting table command | table Username, "TransactDate","Payment Number", NextPaymentNumber before streamstats and see if you are getting a proper table with descending transactdate.

0 Karma
Get Updates on the Splunk Community!

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...