<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Splunk equivalent of SQL window function query in All Apps and Add-ons</title>
    <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Splunk-equivalent-of-SQL-window-function-query/m-p/491962#M60545</link>
    <description>&lt;P&gt;Something like this but add more latest() earliest() for tid&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=yourIndex source=yoursource sourcetype=yourSourcetype | stats latest(cid) earliest(cid) by step
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 May 2020 10:20:41 GMT</pubDate>
    <dc:creator>jkat54</dc:creator>
    <dc:date>2020-05-07T10:20:41Z</dc:date>
    <item>
      <title>Splunk equivalent of SQL window function query</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Splunk-equivalent-of-SQL-window-function-query/m-p/491961#M60544</link>
      <description>&lt;P&gt;I am looking for a Splunk query equivalent to the following SQL query involving window functions.&lt;/P&gt;

&lt;P&gt;Assuming I have a table like so:&lt;/P&gt;

&lt;PRE&gt;
CREATE TABLE my_stats_tbl
(
  time timestamp,
  cid character varying(10),
  tid character varying(10),
  step character varying(10)
);
&lt;/PRE&gt;

&lt;P&gt;and SQL query:&lt;/P&gt;

&lt;PRE&gt;
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
&lt;/PRE&gt;

&lt;P&gt;How do i accomplish this in Splunk, assuming I have a search query which generates an output similar to my SQL table &lt;CODE&gt;my_stats_tbl&lt;/CODE&gt;?&lt;/P&gt;

&lt;P&gt;Essentially, I want to:&lt;BR /&gt;
1. Select only those rows from my search query that correspond to the most recent &lt;CODE&gt;cid&lt;/CODE&gt; AND.&lt;BR /&gt;
2. Amongst rows selected in step-1 above, I want to find the earliest and latest timestamps for each &lt;CODE&gt;cid&lt;/CODE&gt;, &lt;CODE&gt;tid&lt;/CODE&gt; and &lt;CODE&gt;step&lt;/CODE&gt; triplet.&lt;/P&gt;

&lt;P&gt;I'd appreciate any pointers.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 06:48:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/Splunk-equivalent-of-SQL-window-function-query/m-p/491961#M60544</guid>
      <dc:creator>vats</dc:creator>
      <dc:date>2020-05-07T06:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk equivalent of SQL window function query</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Splunk-equivalent-of-SQL-window-function-query/m-p/491962#M60545</link>
      <description>&lt;P&gt;Something like this but add more latest() earliest() for tid&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=yourIndex source=yoursource sourcetype=yourSourcetype | stats latest(cid) earliest(cid) by step
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 May 2020 10:20:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/Splunk-equivalent-of-SQL-window-function-query/m-p/491962#M60545</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2020-05-07T10:20:41Z</dc:date>
    </item>
  </channel>
</rss>

