Splunk Search

stats/streamstats to display only last 2 result/row

yuming1127
Path Finder

Hi, i'm looking for a solution which only show the last and last-1 result using stats or streamstats function.  Aim is to only display something like max(row) and max(row)-1

my search...

| stats values(product_tag*) as product_tag* values(*) as * by product,color,product_tag

outcome

productcolorproduct_tagdescription
phoneredabc_1blabla1
phoneredabc_2blabla2
phoneredabc_3blabla3
phoneredabc_4blabla4

 

desired outcome

productcolorproduct_tagdescription
phoneredabc_3blabla3
phoneredabc_4blabla4

 or 

productcolorproduct_tagdescription
phoneredabc_4blabla4
phoneredabc_3blabla3

 

Labels (1)
Tags (3)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@yuming1127 

try this.

YOUR_SEARCH
| eval a=1 | accum a | eventstats max(a) as mx | where a=mx OR a=mx-1

 

Sample:

| makeresults 
| eval _raw="product	color	product_tag	description
phone	red	abc_1	blabla1
phone	red	abc_2	blabla2
phone	red	abc_3	blabla3
phone	red	abc_4	blabla4" 
| multikv forceheader=1 
| table product color product_tag description
| eval a=1 | accum a | eventstats max(a) as mx | where a=mx OR a=mx-1

 

Thanks
KV
▄︻̷̿┻̿═━一

If this reply helps you, an upvote would be appreciated.

View solution in original post

yuming1127
Path Finder

Great one KV,

one more addition question,  how do i display the product_tag i search and the one previous of it.

For example:

my search...

| stats values(product_tag*) as product_tag* values(*) as * by product,color,product_tag

|  search product_tag=abc_3 

Outcome

productcolorproduct_tagdescription
phoneredabc_3blabla3

 

Desired outcome

tcolorproduct_tagdescription
phoneredabc_2blabla2
phoneredabc_3blabla3

 

really appreciate your help.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@yuming1127 

For your search product_tag  scenario try this. 

YOUR_SEARCH
| eval a=1 , search_product_tag="YOUR_SEARCH_TAG"
| accum a 
| eventstats max(eval(if(search_product_tag==product_tag,a,null()))) as mx
| where a=mx OR a=mx-1
| table product	color	product_tag	description

 

Sample Search:

| makeresults 
| eval _raw="product	color	product_tag	description
phone	red	abc_1	blabla1
phone	red	abc_2	blabla2
phone	red	abc_3	blabla3
phone	red	abc_4	blabla4" 
| multikv forceheader=1 
| table product color product_tag description 
| eval a=1 , search_product_tag="abc_3"
| accum a 
| eventstats max(eval(if(search_product_tag==product_tag,a,null()))) as mx
| where a=mx OR a=mx-1
| table product	color	product_tag	description

 

Thanks
KV
▄︻̷̿┻̿═━一

If this reply helps you, an upvote would be appreciated.

kamlesh_vaghela
SplunkTrust
SplunkTrust

@yuming1127 

try this.

YOUR_SEARCH
| eval a=1 | accum a | eventstats max(a) as mx | where a=mx OR a=mx-1

 

Sample:

| makeresults 
| eval _raw="product	color	product_tag	description
phone	red	abc_1	blabla1
phone	red	abc_2	blabla2
phone	red	abc_3	blabla3
phone	red	abc_4	blabla4" 
| multikv forceheader=1 
| table product color product_tag description
| eval a=1 | accum a | eventstats max(a) as mx | where a=mx OR a=mx-1

 

Thanks
KV
▄︻̷̿┻̿═━一

If this reply helps you, an upvote would be appreciated.

Get Updates on the Splunk Community!

Upcoming Webinar: Unmasking Insider Threats with Slunk Enterprise Security’s UEBA

Join us on Wed, Dec 10. at 10AM PST / 1PM EST for a live webinar and demo with Splunk experts! Discover how ...

.conf25 technical session recap of Observability for Gen AI: Monitoring LLM ...

If you’re unfamiliar, .conf is Splunk’s premier event where the Splunk community, customers, partners, and ...

A Season of Skills: New Splunk Courses to Light Up Your Learning Journey

There’s something special about this time of year—maybe it’s the glow of the holidays, maybe it’s the ...