Splunk Search

Trying to join columns to an initial query

sbedard
Engager

Hello,

I wonder if someone could help me out with a query. I'm trying to compare a value against different point in time, for multiple sources. For some reason, the appendcols seems to be adding columns without matching with another field  so if the values come out in a different order (not all datasource report at different times), then the report gets mixed results from different datasources so they don't match. 

Basically, I need this query to join/group on the actual_data_source field

 

 

 

 

index=bi sourcetype=dbx_bi source=automation earliest=-1h@h latest=@h 
| bin _time span=1h 
| stats sum(error_percentage) as last_hour_percentage by _time,actual_data_source
| appendcols 
  [search index=bi sourcetype=dbx_bi source=automation earliest=-169h@h latest=-168h@h 
  | bin _time as last_week span=1h 
  | stats sum(error_percentage) as last_week_percentage by last_week,actual_data_source  ] 
| appendcols 
  [search index=bi sourcetype=dbx_bi source=automation earliest=-673h@h latest=-672h@h 
  | bin _time as last_month span=1h 
  | stats sum(error_percentage) as last_month_percentage by last_month,actual_data_source ] 
| eval last_hour=strftime(_time,"%Y-%m-%d %H:%M (%:::z %Z)"),last_week=strftime(last_week,"%Y-%m-%d %H:%M (%:::z %Z)"), last_month=strftime(last_month,"%Y-%m-%d %H:%M (%:::z %Z)"), change=(last_hour_percentage-last_week_percentage) 
| search last_hour_percentage>10 
| table actual_data_source, last_hour, last_hour_percentage, last_week, last_week_percentage, last_month, last_month_percentage

 

 

 

 

Thanks!

Labels (1)
0 Karma
1 Solution

johnhuang
Motivator

Something like this:

 

 

index=bi sourcetype=dbx_bi source=automation earliest=-1h@h latest=-0h@h 
| eval last_hour=strftime(_time,"%Y-%m-%d %H:00 (%:::z %Z)")
| stats sum(error_percentage) AS last_hour_percentage BY last_hour actual_data_source
| append [search index=bi sourcetype=dbx_bi source=automation earliest=-169h@h latest=-168h@h 
| eval last_week=strftime(_time,"%Y-%m-%d %H:00 (%:::z %Z)")
| stats sum(error_percentage) AS last_week_percentage BY last_week actual_data_source]
| append [search index=bi sourcetype=dbx_bi source=automation earliest=-673h@h latest=-672h@h
| eval last_month=strftime(_time,"%Y-%m-%d %H:00 (%:::z %Z)")
| stats sum(error_percentage) AS last_month_percentage BY last_month actual_data_source]
| stats max(*) AS * BY actual_data_source
| table actual_data_source last_hour last_hour_percentage last_week last_week_percentage last_month last_month_percentage

 

View solution in original post

yuanliu
SplunkTrust
SplunkTrust

That is Splunk telling you that appendcol is not the correct command if you expect matching.  Try something like

index=bi sourcetype=dbx_bi source=automation ((earliest=-1h@h latest=@h) OR (earliest=-169h@h latest=-168h@h) OR (earliest=-673h@h latest=-672h@h))
| bin _time span=1h@h
| stats sum(error_percentage) as error_percentage by _time, actual_data_source
| eval period = case(_time > relative_time(now(), "-2h", "last_hour", _time < relative_time(now(), "-200h"), "last_week", true(), "last_month")
| foreach last_hour last_week last_month
    [eval <<FIELD>>_percentage = if(period == "<<FIELD>>", error_percentage, null())
    | eval <<FIELD>> = if(period == "<<FIELD>>", _time, null())]
| fields - error_percentage
| stats values(*_percentage) as *_percentage values(last_*) as last_* by actual_data_source
| search last_hour_percentage>10 
| eval last_hour=strftime(_time,"%Y-%m-%d %H:%M (%:::z %Z)"),last_week=strftime(last_week,"%Y-%m-%d %H:%M (%:::z %Z)"), last_month=strftime(last_month,"%Y-%m-%d %H:%M (%:::z %Z)"), change=(last_hour_percentage-last_week_percentage) 
| table actual_data_source, last_hour, last_hour_percentage, last_week, last_week_percentage, last_month, last_month_percentage

 

Tags (2)
0 Karma

johnhuang
Motivator

Something like this:

 

 

index=bi sourcetype=dbx_bi source=automation earliest=-1h@h latest=-0h@h 
| eval last_hour=strftime(_time,"%Y-%m-%d %H:00 (%:::z %Z)")
| stats sum(error_percentage) AS last_hour_percentage BY last_hour actual_data_source
| append [search index=bi sourcetype=dbx_bi source=automation earliest=-169h@h latest=-168h@h 
| eval last_week=strftime(_time,"%Y-%m-%d %H:00 (%:::z %Z)")
| stats sum(error_percentage) AS last_week_percentage BY last_week actual_data_source]
| append [search index=bi sourcetype=dbx_bi source=automation earliest=-673h@h latest=-672h@h
| eval last_month=strftime(_time,"%Y-%m-%d %H:00 (%:::z %Z)")
| stats sum(error_percentage) AS last_month_percentage BY last_month actual_data_source]
| stats max(*) AS * BY actual_data_source
| table actual_data_source last_hour last_hour_percentage last_week last_week_percentage last_month last_month_percentage

 

sbedard
Engager

wow thanks! that worked!

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...