<?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: Count based on condition in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622828#M216545</link>
    <description>&lt;PRE&gt;index="*dockerlogs*" source="*gps-request-processor-test*" OR source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*" event="*Request"
| eval LabelType=coalesce(labelType, documentType)
| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")
| where event == "Sync" AND source like "%gps-request-processor%" OR event == "Async" AND (source like "%gps-external-processor%" OR source like "%gps-artifact-processor%")
| stats count(eval(status="Received")) as received count(eval(status="Failed")) as failed by sourceNodeCode geoCode LabelType event&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;suppose i need to calculate the count of both received and failed but i want this where clause to be applicable while calculating the failed count only, so in that case what should be my query.&lt;/P&gt;</description>
    <pubDate>Thu, 01 Dec 2022 15:27:03 GMT</pubDate>
    <dc:creator>csahoo</dc:creator>
    <dc:date>2022-12-01T15:27:03Z</dc:date>
    <item>
      <title>How to calculate count based on the condition?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622768#M216508</link>
      <description>&lt;P&gt;Hi ,&lt;BR /&gt;&lt;BR /&gt;i want to calculate count based on the condition , like in the below query&lt;BR /&gt;&lt;BR /&gt;if the event is 'sync' then the 'failed' count should fetch from&lt;BR /&gt;source="*gps-request-processor-test*"&lt;BR /&gt;if the event is 'Async' then the 'failed' count should fetch from&lt;BR /&gt;source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*"&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;index="*dockerlogs*" source="*gps-request-processor-test*" OR source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*" event="*Request" documentType="*" OR labelType="*"&lt;BR /&gt;| eval LabelType=coalesce(labelType, documentType)&lt;BR /&gt;| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")&lt;BR /&gt;| rex mode=sed "s/1067/Windrunner/g" field=sourceNodeCode&lt;BR /&gt;| rex mode=sed "s/531/SFS/g" field=sourceNodeCode&lt;BR /&gt;| rex mode=sed "s/EUROPE_MIDDLE_EAST_AFRICA/EMEA/g" field=geoCode&lt;BR /&gt;| eval Geo=geoCode,Node=sourceNodeCode&lt;BR /&gt;| eval syncelapsed=if(source like "%gps-request-processor%",elapsedTime,null())&lt;BR /&gt;| eval asyncelapsed=if(source like "%gps-external-processor%" OR source like "%gps-artifact-processor%",elapsedTime,null())&lt;BR /&gt;| stats count(eval(status="Received" AND source like "%gps-request-processor%" )) as received count(eval(deliveryStatus="Success")) as delivered&lt;BR /&gt;count(eval(status="Failed")) as failed&lt;BR /&gt;avg(syncelapsed) as syncelapsedtime avg(asyncelapsed) as asyncelapsedtime avg(deliveryElapsedTime) as deliverytime by Node Geo LabelType event&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 16:42:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622768#M216508</guid>
      <dc:creator>csahoo</dc:creator>
      <dc:date>2022-12-01T16:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Count based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622791#M216524</link>
      <description>&lt;P&gt;Where is the question? Does your code give what you wanted? &lt;SPAN&gt;If not, what is the output and why is that output what you wanted? (I.e., explain your requirements. Given the complexity of your sample code, you should also explain what are the data like. In particular, what constitutes "sync", what constitutes "async".) Further more, your opening requirement is "count based on the condition". (In Splunk lingo, count with groupby.) But that stats command makes it very unclear what kind of "count" you want to base on those conditions.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If I must take a blind shot, this is your effort to discern the source groups:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval syncelapsed=if(source like "%gps-request-processor%",elapsedTime,null())
| eval asyncelapsed=if(source like "%gps-external-processor%" OR source like "%gps-artifact-processor%",elapsedTime,null())&lt;/LI-CODE&gt;&lt;P&gt;And this is your definition of "sync" vs "async"&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")&lt;/LI-CODE&gt;&lt;P&gt;If my mind-reading is correct, your requirement is basically a filter based on these fields before performing stats. Something like&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| where isnotnull(syncelapsed) AND event == "Sync" OR isnotnull(asyncelapsed) AND event == "Async"&lt;/LI-CODE&gt;&lt;P&gt;This is an extremely literal interpretation of your opening requirement. I get the feeling that there can be many alternatives to make this more efficient. But if we go with this literal approach, your search can be&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index="*dockerlogs*" source="*gps-request-processor-test*" OR source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*" event="*Request" documentType="*" OR labelType="*"
| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")
| eval syncelapsed=if(source like "%gps-request-processor%",elapsedTime,null())
| eval asyncelapsed=if(source like "%gps-external-processor%" OR source like "%gps-artifact-processor%",elapsedTime,null())
| where isnotnull(syncelapsed) AND event == "Sync" OR isnotnull(asyncelapsed) AND event == "Async" ``` remove unnecessary events as early as possible ```
| eval LabelType=coalesce(labelType, documentType)
| rex mode=sed "s/1067/Windrunner/g" field=sourceNodeCode
| rex mode=sed "s/531/SFS/g" field=sourceNodeCode
| rex mode=sed "s/EUROPE_MIDDLE_EAST_AFRICA/EMEA/g" field=geoCode
| eval Geo=geoCode,Node=sourceNodeCode
| stats count(eval(status="Received" AND source like "%gps-request-processor%" )) as received count(eval(deliveryStatus="Success")) as delivered
count(eval(status="Failed")) as failed
avg(syncelapsed) as syncelapsedtime avg(asyncelapsed) as asyncelapsedtime avg(deliveryElapsedTime) as deliverytime by Node Geo LabelType event&lt;/LI-CODE&gt;&lt;P&gt;Here, I moved independent command groups around so the search can have less data to deal with. Hope this helps.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 10:56:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622791#M216524</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2022-12-01T10:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Count based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622803#M216532</link>
      <description>&lt;P&gt;Let me simplifie my requirement , Below is my query&lt;BR /&gt;&lt;BR /&gt;index="*dockerlogs*" source="*gps-request-processor-test*" OR source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*" event="*Request"&lt;BR /&gt;| eval LabelType=coalesce(labelType, documentType)&lt;BR /&gt;| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")&lt;BR /&gt;| stats count(eval(status="Failed")) as failed by sourceNodeCode geoCode LabelType event&lt;BR /&gt;&lt;BR /&gt;where as the&lt;BR /&gt;source : - is my application name&lt;BR /&gt;event :- Type of request whether synchronous request or Asynchronous request&lt;BR /&gt;labeltype : - Different type of label&lt;BR /&gt;sourcenodecode and geocode :- is the shopcode and shopregion from where the label is requested&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Now i want to find the failed request count based on sourceNodeCode, geoCode, LabelType, event&lt;BR /&gt;&lt;BR /&gt;where in case of synchronous request or event the failed count should fetch from '*gps-request-processor-test*' application&lt;BR /&gt;&lt;BR /&gt;in case of asynchronous request or event the failed count should fetch from "*gps-external-processor-test*" OR "*gps-artifact-processor-test*" application&lt;BR /&gt;&lt;BR /&gt;The output should look something similar to this attached o/p where the failed count should fetch from different application based on the event type.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 12:37:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622803#M216532</guid>
      <dc:creator>csahoo</dc:creator>
      <dc:date>2022-12-01T12:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: Count based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622808#M216537</link>
      <description>&lt;P&gt;So my mind-reading is right on spot&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; The above code should work.&lt;/P&gt;&lt;P&gt;In the simplified scenario, there is no pre-definition of sync- or async-lapsed which is based on source group, so you'll make source group explicit in the where command.&lt;/P&gt;&lt;PRE&gt;index="*dockerlogs*" source="*gps-request-processor-test*" OR source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*" event="*Request"
| eval LabelType=coalesce(labelType, documentType)
| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")
| where event == "Sync" AND source like "%gps-request-processor%" OR event == "Async" AND (source like "%gps-external-processor%" OR source like "%gps-artifact-processor%")
| stats count(eval(status="Failed")) as failed by sourceNodeCode geoCode LabelType event&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Dec 2022 13:15:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622808#M216537</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2022-12-01T13:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Count based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622828#M216545</link>
      <description>&lt;PRE&gt;index="*dockerlogs*" source="*gps-request-processor-test*" OR source="*gps-external-processor-test*" OR source="*gps-artifact-processor-test*" event="*Request"
| eval LabelType=coalesce(labelType, documentType)
| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")
| where event == "Sync" AND source like "%gps-request-processor%" OR event == "Async" AND (source like "%gps-external-processor%" OR source like "%gps-artifact-processor%")
| stats count(eval(status="Received")) as received count(eval(status="Failed")) as failed by sourceNodeCode geoCode LabelType event&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;suppose i need to calculate the count of both received and failed but i want this where clause to be applicable while calculating the failed count only, so in that case what should be my query.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 15:27:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/622828#M216545</guid>
      <dc:creator>csahoo</dc:creator>
      <dc:date>2022-12-01T15:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Count based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/623005#M216592</link>
      <description>&lt;P&gt;As I mentioned in your new thread, you just exclude Failed from the restrictive filter, i.e.,&lt;/P&gt;&lt;PRE&gt;index="*dockerlogs*" source="*gps-request-processor-test*"&lt;BR /&gt; OR source="*gps-external-processor-test*"&lt;BR /&gt; OR source="*gps-artifact-processor-test*" event="*Request"
| eval LabelType=coalesce(labelType, documentType)
| eval event = case (like(event,"%Sync%"),"Sync",like(event,"%Async%"),"Async")
| where status != "Failed" AND (event == "Sync" AND source like "%gps-request-processor%"&lt;BR /&gt; OR event == "Async" AND (source like "%gps-external-processor%"&lt;BR /&gt; OR source like "%gps-artifact-processor%"))
| stats count(eval(status=="Failed")) as failed by sourceNodeCode geoCode LabelType event&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Dec 2022 10:26:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-count-based-on-the-condition/m-p/623005#M216592</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2022-12-02T10:26:22Z</dc:date>
    </item>
  </channel>
</rss>

