Splunk Search

How to count the same values over different fields together?

RonWonkers
Path Finder

Lets say we have the following data set:

 

Fruit_ID Fruit_1 Fruit_2

1 Apple NULL

2 Apple NULL

3 Apple NULL

4 Orange NULL

5 Orange NULL

6 Orange NULL

7 Apple Orange

8 Apple Orange

9 Apple Orange

10 Apple Orange

 

Now I am trying to count the total amount of every fruit, in the above example it should be 7 apples and 7 oranges, the problem is that these fruits are seperated in 2 different columns because a fruit name can be both an apple AND an orange, how do I deal with this when counting the total amount of fruit?

Counting one at a time works:

| stats count by Fruit_1

But how do I count both to give a total number since they are 2 seperate columns

I tried combining both columns so its all in 1 long list of values in 1 column but I could not get a definitive answer on how to do this. I tried appending results so first count Fruit_1, then append count Fruit_2 but I did not get the right result of Apple: 7 Orange: 7. Its either 1 or the other.

Does anybody have a fix for how to count over multiple fields like this and combine the result together in 1 field?

Labels (6)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| eval fruit=mvappend(fruit1,if(fruit2!="NULL",fruit2,null()))
| stats count by fruit

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| eval fruit=mvappend(fruit1,if(fruit2!="NULL",fruit2,null()))
| stats count by fruit

RonWonkers
Path Finder

Thank you! Just like that it works and only in 1 line 🙂

0 Karma

RonWonkers
Path Finder

A little update:

I now got to a point where I have the following situation:

Fruit_1 count Fruit_2 count

Apple 5 null null

Orange 10 null null

Pear 5 Apple 5

Melon 10 Orange 10
How do I get it so that the amount of apples and oranges from column Fruit_1 and Fruit_2 are combined into 1 big fruit list and 1 count list?

So the result should be:

Fruit Count

Apple 10

Orange 20

Pear 5

Melon 5
0 Karma

RonWonkers
Path Finder

For context: this question is regarding use cases/user stories for Splunk.

A use case can be linked to multiple user stories, and I want to count the amount of total user stories.

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Reprocessing XML into Fixed-Length Events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...

Index This | What is broken 80% of the time by February?

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