<?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: Combine mvexpand and stats(sum) in one command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327980#M97624</link>
    <description>&lt;P&gt;I figure it out already, thanks&lt;/P&gt;</description>
    <pubDate>Fri, 27 Oct 2017 12:53:58 GMT</pubDate>
    <dc:creator>claudio_manig</dc:creator>
    <dc:date>2017-10-27T12:53:58Z</dc:date>
    <item>
      <title>Combine mvexpand and stats(sum) in one command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327975#M97619</link>
      <description>&lt;P&gt;Hi Ninjas&lt;/P&gt;

&lt;P&gt;I struggle with query including several "challenges".&lt;/P&gt;

&lt;P&gt;I got proxy events like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time="10-27-17 10:00:00" url="www.applepiesamurai.org/get_more_apple_pie" user="arnold.schwarzenegger" response_size=5000000 domain="applepiesamurai.org" url_cat="1,5"
time="10-27-17 10:02:00" url="www.applepiesamurai.org/get_more_apple_pie" user="arnold.schwarzenegger" response_size=7000000 domain="applepiesamurai.org" url_cat="1,5"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now i have a csv lookup look like the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;url_cat,category_name
1,"Business"
5,"Cooking"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What now wanted to create is a sum of the response_size by domain, day, user and then list the proxy category_name(s) by each domain:&lt;/P&gt;

&lt;P&gt;Day| user| domain| response_size| category_name&lt;BR /&gt;
10-27-17|arnold.schwarzenegger|applepiesamurai.org|12000000|Business Cooking&lt;/P&gt;

&lt;P&gt;Turned out that this isn't quite as easy as i thought.&lt;/P&gt;

&lt;P&gt;As url_cat can consist of multiple values i created a new field called "cat" as a multivalue field using transforms "MV_ADD=true". &lt;/P&gt;

&lt;P&gt;In the search, I use mv_expand on cat to  do the lookup and get all the category_name's by each event. But using that, the sum of the response size is misscalculated as mv_expand creates x-times events as it has different cat values and therefore multiplies the sum x-times in my stats sum command.&lt;/P&gt;

&lt;P&gt;After wasting hours with appends and evals I had to pause before I smashed my keyboard.&lt;/P&gt;

&lt;P&gt;Any ideas ond how i get the correct calculation but showing all the category_names?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:26:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327975#M97619</guid>
      <dc:creator>claudio_manig</dc:creator>
      <dc:date>2020-09-29T16:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Combine mvexpand and stats(sum) in one command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327976#M97620</link>
      <description>&lt;P&gt;Try this!&lt;BR /&gt;
Please change the part of stats to efficient one.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(your search)
| eval link_key=url_cat
| makemv delim="," link_key
| mvexpand link_key
| lookup your_lookup_file link_key as url_cat OUTPUT category_name
| stats first(*) as *,values(category_name) as join_category_name by _raw
| nomv join_category_name
| fields - category_name
| rename join_category_name as category_name
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Oct 2017 10:40:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327976#M97620</guid>
      <dc:creator>HiroshiSatoh</dc:creator>
      <dc:date>2017-10-27T10:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Combine mvexpand and stats(sum) in one command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327977#M97621</link>
      <description>&lt;P&gt;Thats what i did now, still the same problem:&lt;BR /&gt;
base search&lt;BR /&gt;
| eval link_key=urlf_categories&lt;BR /&gt;
| makemv delim="," link_key&lt;BR /&gt;
| mvexpand link_key&lt;BR /&gt;
| lookup proxy_categories url_cat as link_key OUTPUT category_name&lt;BR /&gt;
| eval date=strftime(_time,"%m-%d-%y")&lt;BR /&gt;
| stats sum(response_size) as size,values(category_name) as join_category_name by date, user, domain &lt;BR /&gt;
| eval sizeMB=round(size/1024/1024,2)&lt;BR /&gt;
| nomv join_category_name&lt;BR /&gt;
| fields - category_name&lt;BR /&gt;
| rename join_category_name as category_name&lt;/P&gt;

&lt;P&gt;gets back exactly the same (wrong) sum size as my first attempts with the multivalue field. The mvexpand before the stats sum causes multiplication of the response_size as well, ends up with a x times higher sum as it effectly is.&lt;BR /&gt;
At the end the query should bring back the exact same same as it would without the mvexpand adding the extra category_name field &lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:30:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327977#M97621</guid>
      <dc:creator>claudio_manig</dc:creator>
      <dc:date>2020-09-29T16:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: Combine mvexpand and stats(sum) in one command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327978#M97622</link>
      <description>&lt;P&gt;Ok with parts of Hiroshi's query and some hints from collegues and the fact that due to that I was able to do the mvexpand after the stats sum i figured it out:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search
| eval date=strftime(_time,"%m-%d-%y")
| stats sum(response_size) as size,by date, user, domain, url_cat
| eval link_key=url_cat
| makemv delim="," link_key
| mvexpand link_key
| lookup lookuptablename url_cat as link_key OUTPUT category_name
| stats list(*) as * by date, user, domain, size
| eval sizeMB=round(size/1024/1024,2)
| fields- size, link_key, url_cat
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now the size gets summarized before the expand, merging it together by using stats list by-&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 12:39:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327978#M97622</guid>
      <dc:creator>claudio_manig</dc:creator>
      <dc:date>2017-10-27T12:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: Combine mvexpand and stats(sum) in one command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327979#M97623</link>
      <description>&lt;P&gt;This is a process of returning multiple events to singular.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats first(*) as *,values(category_name) as join_category_name by _raw
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It is not the same.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats sum(response_size) as size,values(category_name) as join_category_nam`e by date, user, domain
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You should sum it after returning.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; (your search)
 | eval link_key=url_cat
 | makemv delim="," link_key
 | mvexpand link_key
 | lookup your_lookup_file link_key as url_cat OUTPUT category_name
 | stats first(*) as *,values(category_name) as join_category_name by _raw
 | nomv join_category_name
 | fields - category_name
 | rename join_category_name as category_name
 | eval date=strftime(_time,"%m-%d-%y")
 | stats sum(response_size) as size by date, user, domain ,category_name
 | eval sizeMB=round(size/1024/1024,2)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Oct 2017 12:46:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327979#M97623</guid>
      <dc:creator>HiroshiSatoh</dc:creator>
      <dc:date>2017-10-27T12:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Combine mvexpand and stats(sum) in one command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327980#M97624</link>
      <description>&lt;P&gt;I figure it out already, thanks&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 12:53:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Combine-mvexpand-and-stats-sum-in-one-command/m-p/327980#M97624</guid>
      <dc:creator>claudio_manig</dc:creator>
      <dc:date>2017-10-27T12:53:58Z</dc:date>
    </item>
  </channel>
</rss>

