All Apps and Add-ons

Splunk equivalent of SQL window function query

vats
New Member

I am looking for a Splunk query equivalent to the following SQL query involving window functions.

Assuming I have a table like so:

CREATE TABLE my_stats_tbl
(
  time timestamp,
  cid character varying(10),
  tid character varying(10),
  step character varying(10)
);

and SQL query:

select
    cid,
    tid,
    step,
    min(time) as earliest,
    max(time) as latest
from
(
    select
        time,
        cid,
        tid,
        step,
        cid_recent_time,
        dense_rank() over (order by cid_recent_time desc) as cid_recency_rank
    from
    ( 
        select 
            time,
            cid, 
            tid, 
            step, 
            max(time) over(partition by cid) as cid_recent_time
        from 
            my_stats_tbl
    )q1
) q2
where
    cid_recency_rank = 1
group by
    cid,
    tid,
    step
order by
    cid,
    tid,
    step

How do i accomplish this in Splunk, assuming I have a search query which generates an output similar to my SQL table my_stats_tbl?

Essentially, I want to:
1. Select only those rows from my search query that correspond to the most recent cid AND.
2. Amongst rows selected in step-1 above, I want to find the earliest and latest timestamps for each cid, tid and step triplet.

I'd appreciate any pointers.

Tags (2)
0 Karma

jkat54
SplunkTrust
SplunkTrust

Something like this but add more latest() earliest() for tid

index=yourIndex source=yoursource sourcetype=yourSourcetype | stats latest(cid) earliest(cid) by step
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Federated Search for Snowflake Is Now Generally Available on Splunk Cloud Platform

Unlocking Data-In-Place Search Across Splunk and Snowflake  Enterprise data is increasingly distributed across ...

Help Us Build Better Splunk Regex Puzzles (And Win Prizes!)

If you’ve spent any time in the Splunk Community Slack, you’ve likely seen our resident Splunk Trust ...

Fuel Your Journey: What’s Waiting for You at the .conf26 Acceleration Station

Navigating the show floor at .conf26 isn't just about keynotes and technical breakout sessions; it's also ...