Splunk Search

How to group by two or many fields fields?

sshubh
Explorer

Hi,

Here is my Data in 2 logs having 3 fields

Log1 : 

AccountName books bought bookName
ABC 4 book1, book2, book3, book1
DEF 3 book1, book2, book2
MNO 1 book3

 

Log 2 :

AccountName books sold bookName
ABC 1 book3
DEF 2 book2, book2
MNO 1 book3

 

 

Result I want :

AccountName Total Books bookName bought sold
ABC 4 book1
book2
book3
2
1
1
0
0
1
DEF 3 book1
book2
1
2
0
2
MNO 1 book3 1 1
         

 

Can anyone please help me in this. 

Labels (3)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| eval bookName=split(bookName,", ")
| eval bought_sold=if(isnotnull("books bought"),"bought","sold")
| stats count by AccountName bookName bought_sold
| eval {bought_sold}=count
| fields - bought_sold count
| stats sum(*) as * by AccountName bookName
| fillnull value=0
| stats list(*) as * by AccountName

sshubh
Explorer

Hi, I tried following :

index="test" "Books bought" | rex field=_raw "Books\sbought\sAccountName=(?P<accountName>[^,]*),\sBooks=\[(?P<books>[^\]]*)]" | join type=left accountName
[search index="test" "Books Sold" | rex field=_raw "Books\sSold\sAccountName=(?P<accountName>[^,]*),\sBook=(?P<book>.*)"] | eval bookUsed = split(books, ",") | stats count as bought count(book) as sold by accountName bookUsed

I get following result:

accountNamebookUsedboughtsold
ABCBook121
ABCBook211
DEFBook13 1

 

Now, here the issue is I am getting multiple rows for same accountName but I want only one row for accountName like:

accountNamebookUsedboughtsold
ABCBook1
Book2
2
1
1
1
DEFBook13 1
    

 

Anyone please help. I want to learn and apologies for tagging explicitly.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

To convert your table to the format you want, use the list aggregator function

| stats list(*) as * by accountName

sshubh
Explorer

Thanks for the taking out time and helping. 

| stats list(*) as * by accountName

 worked out. and i got the following result

accountNamebookUsedboughtsold
ABCBook1
Book2
2
1
1
1
DEFBook13 1

 

 

 

I used below query

index="test" "Books bought" | rex field=_raw "Books\sbought\sAccountName=(?P<accountName>[^,]*),\sBooks=\[(?P<books>[^\]]*)]" | join type=left accountName
[search index="test" "Books Sold" | rex field=_raw "Books\sSold\sAccountName=(?P<accountName>[^,]*),\sBook=(?P<book>.*)"] | eval bookUsed = split(books, ",") | stats count as bought count(book) as sold by accountName bookUsed | stats list(*) as * by accountName

Now, I want to add one more column (total Bought) that contains sum of column bought to get following result:

accountNamebookUsedTotal Boughtboughtsold
ABCBook1
Book2
32
1
1
1
DEFBook133 1

 

Can anyone suggest how I can get this. I tried but not able to get the intended result. Thanks in advance.

0 Karma

sshubh
Explorer

I used following and it worked.

| stats sum(Bought) as "Total Bought" list(*) as * by accountName
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...