<?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: Is it possible to use stats inside a join command? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630576#M219073</link>
    <description>&lt;P&gt;As &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/161352"&gt;@gcusello&lt;/a&gt;&amp;nbsp;says, join + inputlookup is not the way to do lookups. Use lookup directly.&lt;/P&gt;&lt;P&gt;Here is an example that either uses _time as logon_time (Example A) or uses the existing logon_time field (Example B)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=...
``` Get the logon time for each user/department - use ONE of these two stats commands ```

``` Example A: Use _time as logon_time and take the earliest time ```
| stats earliest(_time) AS logon_time BY user
| eval logon_time=strftime(logon_time,"%Y-%m-%d %H:%M:%S")
``` ------- ```

``` Example B: Record every logon time for each user ```
| stats count BY user logon_time
| fields - count
``` ------- ```

``` Now get the department for each user ```
| lookup sims_gds_lookup cn as user OUTPUT department
``` Now calculate headcount ```
| eventstats dc(user) as headcount by department
| table logon_time user department headcount&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;This does the aggregations at the start, to minimise the amount of data you need in the lookup, then at the end it finds the department and calculates headcount.&lt;/P&gt;</description>
    <pubDate>Sun, 12 Feb 2023 22:49:52 GMT</pubDate>
    <dc:creator>bowesmana</dc:creator>
    <dc:date>2023-02-12T22:49:52Z</dc:date>
    <item>
      <title>Is it possible to use stats inside a join command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630416#M219017</link>
      <description>&lt;P&gt;I have a user table which shows which department each user belongs to. I want to join this with another table on User so i can get the respective department for each user. However, I would like to have the headcount of each department showing as well. The below code doesn't work but if it makes sense, i would like to achieve something like that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=...
| join type=left user
    [| inputlookup lookup 
    | rename cn as user | stats count(user) as headcount by department]
| table logon_time user department headcount &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 10:35:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630416#M219017</guid>
      <dc:creator>buttsurfer</dc:creator>
      <dc:date>2023-02-10T10:35:18Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to use stats inside a join command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630420#M219019</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/252798"&gt;@buttsurfer&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you please try this?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=...
| lookup sims_gds_lookup cn as user OUTPUT department
| join type=left department
[
    | inputlookup sims_gds_lookup 
    | stats count(user) as headcount by department
]
| table logon_time user department headcount &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;KV&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 10:30:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630420#M219019</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2023-02-10T10:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to use stats inside a join command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630446#M219033</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/252798"&gt;@buttsurfer&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;yes it's possible, putting attention that in the output of the subsearch there's also the field used as key in the join (as&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/127939"&gt;@kamlesh_vaghela&lt;/a&gt;&amp;nbsp;suggested!)&lt;/P&gt;&lt;P&gt;Anyway, I don't like join because it's a very slow command to use only when there isn't any other solution (in other words in the 0.01% of the use cases!).&lt;/P&gt;&lt;P&gt;So try to execute your search in this way:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=...
| lookup sims_gds_lookup cn as user OUTPUT department
| stats earliest(_time) AS logon_time count(user) as headcount BY department
| eval logon_time=strftime(logon_time,"%Y-%m-%d %H:%M:%S")
| table logon_time user department headcount &lt;/LI-CODE&gt;&lt;P&gt;Ciao.&lt;/P&gt;&lt;P&gt;Giuseppe&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 13:13:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630446#M219033</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2023-02-10T13:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to use stats inside a join command?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630576#M219073</link>
      <description>&lt;P&gt;As &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/161352"&gt;@gcusello&lt;/a&gt;&amp;nbsp;says, join + inputlookup is not the way to do lookups. Use lookup directly.&lt;/P&gt;&lt;P&gt;Here is an example that either uses _time as logon_time (Example A) or uses the existing logon_time field (Example B)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=...
``` Get the logon time for each user/department - use ONE of these two stats commands ```

``` Example A: Use _time as logon_time and take the earliest time ```
| stats earliest(_time) AS logon_time BY user
| eval logon_time=strftime(logon_time,"%Y-%m-%d %H:%M:%S")
``` ------- ```

``` Example B: Record every logon time for each user ```
| stats count BY user logon_time
| fields - count
``` ------- ```

``` Now get the department for each user ```
| lookup sims_gds_lookup cn as user OUTPUT department
``` Now calculate headcount ```
| eventstats dc(user) as headcount by department
| table logon_time user department headcount&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;This does the aggregations at the start, to minimise the amount of data you need in the lookup, then at the end it finds the department and calculates headcount.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Feb 2023 22:49:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-use-stats-inside-a-join-command/m-p/630576#M219073</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2023-02-12T22:49:52Z</dc:date>
    </item>
  </channel>
</rss>

