Splunk Search

Is there any function works like group by grouping sets in Mysql?

krim
Explorer

is there any function works like group by grouping sets in Mysql?
So that I can get a value from each group and a total one

Labels (2)
0 Karma

bowesmana
SplunkTrust
SplunkTrust

The stats XXX by YYY will do aggregations to group by YYY and you can include more than one field to group by.

Can you explain a bit more exactly what you are trying to do with the data you have

 

0 Karma

krim
Explorer

krim_1-1664438858620.png

 

select

coalesce(class,"total") as class,

coalesce(name,"total") as name,

sum(num) as sum

from table a
group by grouping sets (class,name,(class,name))

In mysql I can get sum from each group and a total at the same time, from the left table to the right one. 

can I do the same in Splunk?

0 Karma

bowesmana
SplunkTrust
SplunkTrust

The answer is always YES with Splunk 😀 and often there is more than one way to achieve the same result.

Here is an example with your data setup (note that in your data example, your C2/C sum is shown as '3' in the right table, however, from the data in the left, it should be 4, right?

| makeresults
| eval _raw="Class,name,number
C1,A,1
C1,B,2
C2,C,1
C1,A,1
C1,B,2
C2,C,3"
| multikv forceheader=1 

| table Class,name,number

| stats sum(number) as sum by Class name
| addcoltotals labelfield="Class"
| appendpipe [
  | stats sum(sum) as sum by Class
]

so in this example, up to multikv is setting up the sample data from your left hand table, then:

the first stats sum... is calculating the totals by Class/name to give you 3 rows. Then addcoltotals is giving the total for those 3 rows. After that the appendpipe/stats is then calculating the totals for each class based on the values of those 3 rows.

Hope this helps.

 

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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...