<?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 How to count the number of different values (multivalue) in a field? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418019#M120238</link>
    <description>&lt;P&gt;I have a multivalue field with at least 3 different combinations of values. See Example.CSV below (the 2 "apple orange" is a multivalue, not a single value. Its delimited by a newline, "apple" is actually stacked atop of "orange"):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;container        fruit
  15           apple orange
  18           apple orange
  3              apple 
  5              orange
  44             orange
  66             orange
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What query do I need to produce this table:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;apple_and_orange    just_apple    just_orange
        2                1            3
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 30 Jul 2019 18:14:26 GMT</pubDate>
    <dc:creator>russell120</dc:creator>
    <dc:date>2019-07-30T18:14:26Z</dc:date>
    <item>
      <title>How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418019#M120238</link>
      <description>&lt;P&gt;I have a multivalue field with at least 3 different combinations of values. See Example.CSV below (the 2 "apple orange" is a multivalue, not a single value. Its delimited by a newline, "apple" is actually stacked atop of "orange"):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;container        fruit
  15           apple orange
  18           apple orange
  3              apple 
  5              orange
  44             orange
  66             orange
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What query do I need to produce this table:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;apple_and_orange    just_apple    just_orange
        2                1            3
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:14:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418019#M120238</guid>
      <dc:creator>russell120</dc:creator>
      <dc:date>2019-07-30T18:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418020#M120239</link>
      <description>&lt;P&gt;&lt;CODE&gt;... | stats count by fruit&lt;/CODE&gt; ...&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:39:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418020#M120239</guid>
      <dc:creator>adonio</dc:creator>
      <dc:date>2019-07-30T18:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418021#M120240</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;You could try the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval events="15 - apple orange|18 - apple orange|3 - apple|5 - orange|44 - orange|66 - orange"
| eval events=split(events, "|")
| mvexpand events
| eval events=split(events, " - ")
| eval container=mvindex(events, 0)
| eval fruit=mvindex(events, 1)
| eval fruit=split(fruit, " ")
| fields container fruit
| fields - _time
| rename COMMENT as "--- Sample Generated Data Above ---"
| rename COMMENT as "--- Query Below ---"
| eval fruit=mvjoin(fruit, "_and_")
| stats count by fruit
| transpose header_field=fruit
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope it helps!!!&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:41:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418021#M120240</guid>
      <dc:creator>jaime_ramirez</dc:creator>
      <dc:date>2019-07-30T18:41:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418022#M120241</link>
      <description>&lt;P&gt;Hey Russell, &lt;/P&gt;

&lt;P&gt;If the intention is to treat the "apple orange" multivalue fields as a unique fruit type, we'll need to remove convert them to a non multivalue field. This can be done in a few different ways, but for this example I'll use the &lt;CODE&gt;nomv&lt;/CODE&gt; command. &lt;/P&gt;

&lt;P&gt;Once that is done, we can then run a stats count by fruit and it should produce the results we want, just improperly formatted. &lt;/P&gt;

&lt;P&gt;The "rex mode=sed" portion isn't nessesary, but I end up using it to replace any multivalue fields with an "and" breaker for later formatting. &lt;/P&gt;

&lt;P&gt;Then, we'll simply use the &lt;CODE&gt;transpose&lt;/CODE&gt; command to use our "fruit" column values as our new data headers. &lt;/P&gt;

&lt;P&gt;Lastly we'll use the &lt;CODE&gt;rename&lt;/CODE&gt; command to add the string "just_" to all of our field names.&lt;/P&gt;

&lt;P&gt;I'm providing some fake data so you can play around with it to see how it works here (copy paste this into any Splunk instance):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults count=20
| eval container = random()%70
| eval fruit = random()%3
| eval fruit = if(fruit = 0, "apple,orange", if(fruit = 1, "apple", if(fruit = 2, "orange", "tomato?")))
| eval fruit = split(fruit, ",")
| nomv fruit
| stats count by fruit
| rex field=fruit mode=sed "s/\n/_and_/g"
| transpose header_field=fruit
| fields - column
| rename * AS just_*
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Nomv Command Documentation:&lt;/STRONG&gt; &lt;A href="https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/Nomv"&gt;https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/Nomv&lt;/A&gt;&lt;BR /&gt;
&lt;STRONG&gt;Transpose Command Documentation:&lt;/STRONG&gt; &lt;A href="https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/Transpose"&gt;https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/Transpose&lt;/A&gt;&lt;BR /&gt;
&lt;STRONG&gt;Rex Command Documentation (see sed expression header):&lt;/STRONG&gt; &lt;A href="https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/rex"&gt;https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/rex&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Let me know if anything doesn't make sense, or it isn't working for you and I'm happy to help!&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:47:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418022#M120241</guid>
      <dc:creator>rbechtold</dc:creator>
      <dc:date>2019-07-30T18:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418023#M120242</link>
      <description>&lt;P&gt;Tried that, doesn't work unfortunately. It doesn't count the number of the multivalue value, which is apple orange (delimited by a newline. So in my data one is above the other). The result of your suggestion is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;count  fruit
 1   apple
 3   orange
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:48:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418023#M120242</guid>
      <dc:creator>russell120</dc:creator>
      <dc:date>2019-07-30T18:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418024#M120243</link>
      <description>&lt;P&gt;rbechtold's answer also worked, but your post was first so I'll accept this as the answer.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 19:01:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418024#M120243</guid>
      <dc:creator>russell120</dc:creator>
      <dc:date>2019-07-30T19:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of different values (multivalue) in a field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418025#M120244</link>
      <description>&lt;P&gt;Your answer solved my issue, but I accepted jaime.ramirez's answer as the solution since he posted first. With your answer, I learned what the nomv command is/does and I thank you for that. It will make a number of panels I've made that much more simpler.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 19:03:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-the-number-of-different-values-multivalue-in-a/m-p/418025#M120244</guid>
      <dc:creator>russell120</dc:creator>
      <dc:date>2019-07-30T19:03:43Z</dc:date>
    </item>
  </channel>
</rss>

