<?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: recursively join the same table? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140080#M38625</link>
    <description>&lt;P&gt;Try something like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | table parent, child | join type=left child [search your base search | rename child as child2 | rename parent as child] 
| eval child=child.coalesce(",".child2,"") | table parent, child
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Updated answer&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=csv |join type= left c [search sourcetype=csv |stats delim="," values(c) as cvals by p |rename p as c]|eval c=c.coalesce(",".cvals,"") | table p,c
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 25 Sep 2014 14:18:30 GMT</pubDate>
    <dc:creator>somesoni2</dc:creator>
    <dc:date>2014-09-25T14:18:30Z</dc:date>
    <item>
      <title>recursively join the same table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140079#M38624</link>
      <description>&lt;P&gt;Hi &lt;/P&gt;

&lt;P&gt;I have a data set with parent ID and child ID in a same table.&lt;BR /&gt;
I am looking for a search that produce the following.&lt;/P&gt;

&lt;P&gt;A sample data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; p c
--- ---
111 222
222 333
111 444
555 666
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and, the output I want to get is :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; p   c
--- ---
111 222,333
111 444
555 666
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I was looking at the search documentation and find selfjoin command, but I still can not get it work..&lt;/P&gt;

&lt;P&gt;Any comment would be appreciated!&lt;BR /&gt;
Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Sep 2014 07:06:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140079#M38624</guid>
      <dc:creator>Splunk_Shinobi</dc:creator>
      <dc:date>2014-09-25T07:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: recursively join the same table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140080#M38625</link>
      <description>&lt;P&gt;Try something like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | table parent, child | join type=left child [search your base search | rename child as child2 | rename parent as child] 
| eval child=child.coalesce(",".child2,"") | table parent, child
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Updated answer&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=csv |join type= left c [search sourcetype=csv |stats delim="," values(c) as cvals by p |rename p as c]|eval c=c.coalesce(",".cvals,"") | table p,c
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Sep 2014 14:18:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140080#M38625</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-09-25T14:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: recursively join the same table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140081#M38626</link>
      <description>&lt;P&gt;The solution given by  somesoni2, I am afraid, is not recursive. Though not a completely recursive one, &lt;BR /&gt;
I have managed to come up with a decent solution as follows.&lt;BR /&gt;
You need repeat a line (n - 2) times in order to get n-deep tree from parent-child pairs.&lt;BR /&gt;
I needed to start from the end nodes instead of arbitrary nodes &lt;BR /&gt;
in order to avoid (partially) duplicate branches.&lt;BR /&gt;
(Please pardon my formatting below. This is my first post.)&lt;/P&gt;

&lt;H3&gt;Splunk search to build trees from parent-child pairs&lt;/H3&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=csv 
  | join type=left c [search sourcetype=csv | rename c as c2 | rename p as c ] | where isnull (c2)
  | eval t=p." -&amp;gt; ".c | eval r=p | eval e=c | table t, r, e 
  | eval k=r | join type=left k [search sourcetype=csv | rename c as k ] | eval t=if(isnull(p), t, p." -&amp;gt; ".t) | eval r=p | table t, r, e 
  | eval k=r | join type=left k [search sourcetype=csv | rename c as k ] | eval t=if(isnull(p), t, p." -&amp;gt; ".t) | eval r=p | table t, r, e
  | eval k=r | join type=left k [search sourcetype=csv | rename c as k ] | eval t=if(isnull(p), t, p." -&amp;gt; ".t) | eval r=p | table t, r, e 
  | sort t | table t | outputcsv "result.csv"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H3&gt;Variables&lt;/H3&gt;

&lt;PRE&gt;&lt;CODE&gt;p: parent
c: child
c2: temporary child
r: root
k: (join) key
e: end
t: tree
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H3&gt;Search Explanation&lt;/H3&gt;

&lt;PRE&gt;&lt;CODE&gt;# --------------------
# [Initial Data Processing]
# --------------------
sourcetype=csv 
  | join type=left c [search sourcetype=csv | rename c as c2 | rename p as c ] | where isnull (c2)
  | eval t=p." -&amp;gt; ".c | eval e=c | eval r=p | table t, r, e 
# --------------------
  | join type=left c [search sourcetype=csv | rename c as c2 | rename p as c ] | where isnull (c2)
# (1) Leave only parent-child pairs where the child is an end node
  | eval t=p." -&amp;gt; ".c | eval r=p | eval e=c | table t, r, e 
# (2) Set p(arent) to r(oot), c(hild) to e(nd), and output t(ree), r(oot), e(nd)

# --------------------
# [Repeatable Part]
# --------------------
  | eval k=r | join type=left k [search sourcetype=csv | rename c as k ] | eval t=if(isnull(p), t, p." -&amp;gt; ".t) | eval r=p | table t, r, e 
# You repeat this line n - 2 times to get n-deep tree
# --------------------
  | eval k=r 
# (1) Set r(oot) to k(ey)
  | join type=left k [search sourcetype=csv | rename c as k ] 
# (2) Join the output with the csv with c(hild) renamed as k(ey) based on k(ey)
  | eval t=if(isnull(p), t, p." -&amp;gt; ".t)
# (3) Add p(arent), " -&amp;gt; " in front of t(ree) if p(arent) is not null
  | eval r=p | table t, r, e 
# (4) Set p(arent) to r(oot) and output t(ree), r(oot), e(nd)
# --------------------

# --------------------
# [Finishing Touch]
# --------------------
  | sort t | table t | outputcsv "result.csv"
# --------------------
# Sort by t(ree) and output the result to result.csv
# --------------------
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H3&gt;Sample Tree structure&lt;/H3&gt;

&lt;PRE&gt;&lt;CODE&gt;111 --&amp;gt; 222 --&amp;gt; 777 --&amp;gt; 555 --&amp;gt; 321
            |       +-&amp;gt; 789
            +-&amp;gt; 999
333 --&amp;gt; 444 --&amp;gt; 888 --&amp;gt; 234
    +-&amp;gt; 666
123 --&amp;gt; 456 --&amp;gt; 567
987 --&amp;gt; 654
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H3&gt;parent-child.csv&lt;/H3&gt;

&lt;PRE&gt;&lt;CODE&gt;p,c
111,222
222,777
333,444
777,555
333,666
444,888
222,999
123,456
987,654
777,789
555,321
888,234
456,567
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H3&gt;result.csv&lt;/H3&gt;

&lt;PRE&gt;&lt;CODE&gt;t
"111 -&amp;gt; 222 -&amp;gt; 777 -&amp;gt; 555 -&amp;gt; 321"
"111 -&amp;gt; 222 -&amp;gt; 777 -&amp;gt; 789"
"111 -&amp;gt; 222 -&amp;gt; 789"
"111 -&amp;gt; 222 -&amp;gt; 999"
"123 -&amp;gt; 456 -&amp;gt; 567"
"333 -&amp;gt; 444 -&amp;gt; 888 -&amp;gt; 234"
"333 -&amp;gt; 666"
"987 -&amp;gt; 654"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Sep 2014 10:00:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140081#M38626</guid>
      <dc:creator>rmasuoka</dc:creator>
      <dc:date>2014-09-28T10:00:13Z</dc:date>
    </item>
    <item>
      <title>Re: recursively join the same table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140082#M38627</link>
      <description>&lt;P&gt;Sorry, It's possible instead of write N times the same query, insert a loop?&lt;/P&gt;

&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2015 15:22:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140082#M38627</guid>
      <dc:creator>Federica_92</dc:creator>
      <dc:date>2015-02-25T15:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: recursively join the same table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140083#M38628</link>
      <description>&lt;P&gt;Is there a loop function or functionality available in Splunk? I looked around, but could not find loop when I worked on my answer above.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2015 06:17:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140083#M38628</guid>
      <dc:creator>rmasuoka</dc:creator>
      <dc:date>2015-03-02T06:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: recursively join the same table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140084#M38629</link>
      <description>&lt;P&gt;Unfortunately, there is no loop function. Maybe you could use "map" command, but probably it would be better to write a custom search command that does loop operation.&lt;/P&gt;

&lt;P&gt;&lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEU2"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEU2&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2015 02:32:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/recursively-join-the-same-table/m-p/140084#M38629</guid>
      <dc:creator>melonman</dc:creator>
      <dc:date>2015-03-03T02:32:52Z</dc:date>
    </item>
  </channel>
</rss>

