<?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: Conditional count distinct if in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284803#M86132</link>
    <description>&lt;P&gt;I recommend breaking this sort of thing into an &lt;CODE&gt;eval&lt;/CODE&gt; statement and then a separate &lt;CODE&gt;stats&lt;/CODE&gt; command just to make it more comprehensible. &lt;/P&gt;

&lt;P&gt;Also in this case you can make a separate "record_type" field and then use that with the chart command to do it a little more compactly. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval record_type=case(record="computer","computer",  record="person","person")
| chart dc(id) over department by record_type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However,  there is another way, and this I think is more the way you were headed -- to create separate id fields and then use stats &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval person_id=if(record="person",id,null()
| eval computer_id=if(record="computer",id,null())
| stats dc(person_id) dc(computer_id) over department 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Apr 2016 18:21:08 GMT</pubDate>
    <dc:creator>sideview</dc:creator>
    <dc:date>2016-04-06T18:21:08Z</dc:date>
    <item>
      <title>Conditional count distinct if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284802#M86131</link>
      <description>&lt;P&gt;Hello all, &lt;/P&gt;

&lt;P&gt;I'm looking to do a "count distinct value if record type = foobar" type of scenario.  Hopefully, I'll be able to articulate what I'm trying to do here.&lt;/P&gt;

&lt;P&gt;record type A:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;record: person
name: bob
id: 123456
sex: m
state: tx
hp: 555-123-1234
dept: finance



record: person
name: jane
id: 7949191
sex: f
state: ca
hp: 555-456-7890
dept: marketing



record: person
name: jane
id: 7949191
sex: f
state: ca
hp: 555-456-7890
dept: marketing
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;record type B: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;record: computer
computername: mycomputer
type: pc
ram: 4GB
ip: 1.1.1.1
dept: finance





record: computer
computername: mylaptop
type: laptop
ram: 4GB
ip: 2.2.2.2
dept: finance





record: computer
computername: theserver
type: server
ram: 16GB
ip: 10.0.0.1
dept: marketing
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;stats if(record="computer",dc(computername)) as dc_computer  if(record="person",dc(id)) as dc_people by dept&lt;/P&gt;

&lt;P&gt;desired result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;dept        dc_computer dc_people   
finance         2           1                       
marketing         1         2                       
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 09:22:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284802#M86131</guid>
      <dc:creator>jclemons7</dc:creator>
      <dc:date>2020-09-29T09:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional count distinct if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284803#M86132</link>
      <description>&lt;P&gt;I recommend breaking this sort of thing into an &lt;CODE&gt;eval&lt;/CODE&gt; statement and then a separate &lt;CODE&gt;stats&lt;/CODE&gt; command just to make it more comprehensible. &lt;/P&gt;

&lt;P&gt;Also in this case you can make a separate "record_type" field and then use that with the chart command to do it a little more compactly. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval record_type=case(record="computer","computer",  record="person","person")
| chart dc(id) over department by record_type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However,  there is another way, and this I think is more the way you were headed -- to create separate id fields and then use stats &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval person_id=if(record="person",id,null()
| eval computer_id=if(record="computer",id,null())
| stats dc(person_id) dc(computer_id) over department 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Apr 2016 18:21:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284803#M86132</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2016-04-06T18:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional count distinct if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284804#M86133</link>
      <description>&lt;P&gt;Give this a try. There is no id field for computer and there is no computername field for person record, it can be simply done by this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | stats dc(computername) as dc_computer dc(id) as dc_people by dept 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Apr 2016 18:22:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284804#M86133</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-04-06T18:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional count distinct if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284805#M86134</link>
      <description>&lt;P&gt;I like this answer but it does rely on the additional assumption that the computername is also unique.   iow there might be some "my laptop" computers out there.  or just null values which wouldn't get counted.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 18:25:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284805#M86134</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2016-04-06T18:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional count distinct if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284806#M86135</link>
      <description>&lt;P&gt;ugh.. sorry, in my "real" data.. the field I need to count distinct values from is actually the same name.  I think my dummy data is not accurate.. &lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 18:25:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-distinct-if/m-p/284806#M86135</guid>
      <dc:creator>jclemons7</dc:creator>
      <dc:date>2016-04-06T18:25:41Z</dc:date>
    </item>
  </channel>
</rss>

