Splunk Search

correlated subsearch

composite
Engager

Hi,
I'm looking for a way to do an equivalent of a SQL correlated subquery in Splunk. [I did look at Splunk for SQL users - it doesn't have an answer].

I have events of the form: _time user=A type=B count=N. I'd like to express the following SQL in Splunk:
Select type, sum(count) from T
where _time=(Select max(_time) from T as T' where T'.type=T.type and T'.user=T.user)
group by type
where T._time + 24h < current_time()

Is there a way to do this?

Thank you

Tags (1)
0 Karma
1 Solution

sideview
SplunkTrust
SplunkTrust

I'm no master of correlated subqueries, but this might be it.

I'm reading your SQL as saying "find me most recent entries in T for each combination of type and user, and give me the type and the sum of the count field from those entries. Then group these by type and show me just the rows earlier than 24 hours ago.

... latest=-24h | eventstats max(_time) as maxTime by user type | where _time=maxTime | stats sum(count) as count by type

If my interpretation is wrong (as it very well may be) can you write it out in english?

View solution in original post

sideview
SplunkTrust
SplunkTrust

I'm no master of correlated subqueries, but this might be it.

I'm reading your SQL as saying "find me most recent entries in T for each combination of type and user, and give me the type and the sum of the count field from those entries. Then group these by type and show me just the rows earlier than 24 hours ago.

... latest=-24h | eventstats max(_time) as maxTime by user type | where _time=maxTime | stats sum(count) as count by type

If my interpretation is wrong (as it very well may be) can you write it out in english?

Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...