Splunk Search

How to compare values across rows and columns

maingirl
New Member

I have two searches with  three fields in common but two field that differ. I'm trying to find returns  that don't have a matching sale for that company_name, mid, and card_number. The return and sales fields are both dollar amounts. 

"total" is the dollar amount of the transaction, return or sale

 index=X sourcetype=Y  earliest=-1d@d latest=@d
| where transaction_type= "refund"
| stats values(total) as returns by company_name, mid, card_number
| append
[  search index=X sourcetype=Y earliest=-30d@d latest=@d
   |  where transaction_type= "sale"
   | stats values(total) as sales by company_name, mid, card_number ]

Currently I have this search that pulls all return transactions from the past day, as well as every sale from the past month, the results look like this 
   
     company.      MID.       card num.        returns   sales
+-------------------------------------------------------------------+
 | company A | 1234 | 1234***7890 |  50.00 |                  |
 | company B | 1254 | 1234***1234 |  80.00 |                  |
 | company C | 1236 | 1234***1230 |  75.00 |                  |
 | company A | 1234 | 1234***7890 |               | 50.00     |
 | company B | 1254 | 1234***1234 |               | 30.00     |
 | company C | 1236 | 1234***1230 |               | 75.00     |

You can see company B has refunded the card number 1234***1234 for the amount of $80.00,  but there was not a sale to that card in that amount. I would like my search to exclusively display the rows (with the return amount only, not any sale numbers) where this happens. so Ideally the search would have returned just one row:

   company.      MID.       card num.        returns 
+------------------------------------------------------+
 | company B | 1254 | 1234***1234 |  80.00 |                  

Labels (2)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

See if this helps.

index=X sourcetype=Y  earliest=-1d@d latest=@d
| where transaction_type= "refund"
| stats values(total) as returns by company_name, mid, card_number
| append
[  search index=X sourcetype=Y earliest=-30d@d latest=@d
   |  where transaction_type= "sale"
   | stats values(total) as sales by company_name, mid, card_number ]
| stats values(*) as * by company, mid, card_number
| where returns != sales
| table company mid card_number returns
---
If this reply helps you, Karma would be appreciated.
0 Karma

somesoni2
Revered Legend

Try this

 index=X sourcetype=Y  (transaction_type= “refund" earliest=-1d@d latest=@d) OR (transaction_type= “sale" earliest=-30d@d latest=@d) 
| eval sales=if(transaction_type= “sale”, total,0) 
| eval returns=if(transaction_type= “ refund”, total,0)
| stats values(sales) as sales sum(returns) as returns by company_name, mid, card_number
| where sales!=returns
Get Updates on the Splunk Community!

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

 Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

🔐 Trust at Every Hop: How mTLS in Splunk Enterprise 10.0 Makes Security Simpler

From Idea to Implementation: Why Splunk Built mTLS into Splunk Enterprise 10.0  mTLS wasn’t just a checkbox ...