Splunk Search

How to TOP based on one of the stats by field ?

sangs8788
Communicator

Hi

I have below query which gives me TOP 20 Requests based on REQUEST_COST regardless of the RequestType

| search RequestType= "Type1" OR RequestType="Type2"|
stats count as RCOUNT, sum(REQUEST_COST) as REQUEST_COST
P95(REQUEST_COST) as P95_REQUEST_COST

by R, RequestType|sort -REQUEST_COST | head 20

What i need is to get TOP 20 based on REQUEST_COST for each RequestType. That is TOP 20 Request for Type1 and TOP 20 Requests for Type2 requesttypes. How do i achieve this? Can someone help me on this ?

Thanks
SS

Tags (1)
0 Karma
1 Solution

niketn
Legend

@sangs8788 you can use streamstats to filter the results based on top count as per need.

Following is a run anywhere example based on Splunk's _internal index that gives Top 5 Components with Errors and Top 5 Components with Warning.

index=_internal sourcetype=splunkd log_level IN ("WARN","ERROR")
| stats count by component log_level
| sort 0 - log_level count
| streamstats count as rank by log_level
| search rank<=5

Please try out and adjust as per your needs.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

niketn
Legend

@sangs8788 you can use streamstats to filter the results based on top count as per need.

Following is a run anywhere example based on Splunk's _internal index that gives Top 5 Components with Errors and Top 5 Components with Warning.

index=_internal sourcetype=splunkd log_level IN ("WARN","ERROR")
| stats count by component log_level
| sort 0 - log_level count
| streamstats count as rank by log_level
| search rank<=5

Please try out and adjust as per your needs.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

sangs8788
Communicator

Thanks. It worked.

0 Karma
Get Updates on the Splunk Community!

Celebrating Fast Lane: 2025 Authorized Learning Partner of the Year

At .conf25, Splunk proudly recognized Fast Lane as the 2025 Authorized Learning Partner of the Year. This ...

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...