<?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: WHERE clause not working when comparing fields in events from same sourcetype in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/WHERE-clause-not-working-when-comparing-fields-in-events-from/m-p/545946#M154733</link>
    <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/232992"&gt;@Traer001&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See this example, where I have used your data and added in another user id to help demonstrate things&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval x="2021-03-29 13:29:44  User: 2 (LeaveRoom): 230###2021-03-29 13:29:40  User: 2 Choose To Go To Room 212###2021-03-29 13:29:36  User: 2 (LeaveRoom): 245###2021-03-29 13:29:35  User: 3 (LeaveRoom): 123###2021-03-29 13:29:32  User: 2 Choose To Go To Room 230###2021-03-29 13:29:31  User: 3 Choose To Go To Room 444###2021-03-29 13:29:28  User: 2 Choose To Go To Room 245###2021-03-29 13:29:22  User: 3 Choose To Go To Room 123"
| makemv delim="###" x
| mvexpand x
| rename x as _raw
| eval _time=strptime(_raw, "%F %T")
| rex field=_raw "User:\s(?&amp;lt;user_id&amp;gt;\d+)\s(?&amp;lt;action&amp;gt;[^\d]+)\s(?&amp;lt;room_id&amp;gt;\d+)"
| stats latest(_time) as _time earliest(_time) as earliest values(action) as actions by room_id user_id
| where mvcount(actions)=2
| stats latest(room_id) as room_id latest(_time) as left latest(earliest) as entered by user_id&lt;/LI-CODE&gt;&lt;P&gt;You can paste this search into a Splunk search bar to see the results. The lines up to the first 'rex' statement are setting up the data, but then&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;rex gets the user, action (enter/leave) and room id&lt;/LI&gt;&lt;LI&gt;stats collects the times and actions for each user/room&lt;/LI&gt;&lt;LI&gt;where statement checks for two actions (must be enter AND leave)&lt;/LI&gt;&lt;LI&gt;final stats will show you the results for user id 2 and 3&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;to see the data remove all lines after mvexpand and you can see the data it is working with&lt;/P&gt;&lt;P&gt;Using dedup is not something to use and you can't use where to check things as your row will not contain both rooms until you have performed some stats aggregation.&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Mar 2021 03:30:38 GMT</pubDate>
    <dc:creator>bowesmana</dc:creator>
    <dc:date>2021-03-30T03:30:38Z</dc:date>
    <item>
      <title>WHERE clause not working when comparing fields in events from same sourcetype</title>
      <link>https://community.splunk.com/t5/Splunk-Search/WHERE-clause-not-working-when-comparing-fields-in-events-from/m-p/545937#M154728</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I am trying to retrieve two events: the latest event where a user leaves a room and the earliest event where a user chooses to go to that room. In the example below I would want to retrieve the first and fourth events.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2021-03-29 13:29:44&amp;nbsp; User: 2 (LeaveRoom): 230&lt;/P&gt;&lt;P&gt;2021-03-29 13:29:44&amp;nbsp; User: 2 Choose To Go To Room 212&lt;/P&gt;&lt;P&gt;2021-03-29 13:29:44&amp;nbsp; User: 2 (LeaveRoom): 245&lt;/P&gt;&lt;P&gt;2021-03-29 13:29:44&amp;nbsp; User: 2 Choose To Go To Room 230&lt;/P&gt;&lt;P&gt;2021-03-29 13:29:44&amp;nbsp; User: 2 Choose To Go To Room 245&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You'll notice that the user must choose the next place to go before actually leaving a room. My problem is that I cannot seem to get my WHERE clause to work to narrow down the results to the two events.&amp;nbsp;I cannot simply dedup 4 in order to get the needed events because the latest event is not necessarily a "leave room" event, and there could be other events (unaccounted for below). I might need the 2nd and 5th events, or 1st and 3rd events, or another combination. It depends. Both needed events will appear within the 10 most recent events, however, so I use a dedup in my query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;index=INDEX host=HOSTNAME sourcetype=SOURCETYPE&lt;/P&gt;&lt;P&gt;| rex field=_raw "User:\s(?&amp;lt;user_id&amp;gt;\d+)\s\(LeaveRoom\):\s(?&amp;lt;leave_room_id&amp;gt;\d+)"&lt;/P&gt;&lt;P&gt;| rex field=_raw "User:\s(?&amp;lt;user_id&amp;gt;\d+)\sEntered\s(?&amp;lt;entered_room_id&amp;gt;\d+)"&lt;/P&gt;&lt;P&gt;| dedup 10 user_id&lt;/P&gt;&lt;P&gt;| where leave_room_id=entered_room_id&lt;/P&gt;&lt;P&gt;| stats latest(leave_room_id) as left_room, earliest(entered_room_id) as entered_room by user_id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I rewrite this to get the events I need?&lt;/P&gt;</description>
      <pubDate>Mon, 29 Mar 2021 22:17:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/WHERE-clause-not-working-when-comparing-fields-in-events-from/m-p/545937#M154728</guid>
      <dc:creator>Traer001</dc:creator>
      <dc:date>2021-03-29T22:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: WHERE clause not working when comparing fields in events from same sourcetype</title>
      <link>https://community.splunk.com/t5/Splunk-Search/WHERE-clause-not-working-when-comparing-fields-in-events-from/m-p/545946#M154733</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/232992"&gt;@Traer001&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See this example, where I have used your data and added in another user id to help demonstrate things&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval x="2021-03-29 13:29:44  User: 2 (LeaveRoom): 230###2021-03-29 13:29:40  User: 2 Choose To Go To Room 212###2021-03-29 13:29:36  User: 2 (LeaveRoom): 245###2021-03-29 13:29:35  User: 3 (LeaveRoom): 123###2021-03-29 13:29:32  User: 2 Choose To Go To Room 230###2021-03-29 13:29:31  User: 3 Choose To Go To Room 444###2021-03-29 13:29:28  User: 2 Choose To Go To Room 245###2021-03-29 13:29:22  User: 3 Choose To Go To Room 123"
| makemv delim="###" x
| mvexpand x
| rename x as _raw
| eval _time=strptime(_raw, "%F %T")
| rex field=_raw "User:\s(?&amp;lt;user_id&amp;gt;\d+)\s(?&amp;lt;action&amp;gt;[^\d]+)\s(?&amp;lt;room_id&amp;gt;\d+)"
| stats latest(_time) as _time earliest(_time) as earliest values(action) as actions by room_id user_id
| where mvcount(actions)=2
| stats latest(room_id) as room_id latest(_time) as left latest(earliest) as entered by user_id&lt;/LI-CODE&gt;&lt;P&gt;You can paste this search into a Splunk search bar to see the results. The lines up to the first 'rex' statement are setting up the data, but then&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;rex gets the user, action (enter/leave) and room id&lt;/LI&gt;&lt;LI&gt;stats collects the times and actions for each user/room&lt;/LI&gt;&lt;LI&gt;where statement checks for two actions (must be enter AND leave)&lt;/LI&gt;&lt;LI&gt;final stats will show you the results for user id 2 and 3&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;to see the data remove all lines after mvexpand and you can see the data it is working with&lt;/P&gt;&lt;P&gt;Using dedup is not something to use and you can't use where to check things as your row will not contain both rooms until you have performed some stats aggregation.&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 03:30:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/WHERE-clause-not-working-when-comparing-fields-in-events-from/m-p/545946#M154733</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2021-03-30T03:30:38Z</dc:date>
    </item>
  </channel>
</rss>

