MySQL query for YTD Running Total
Hi all,
I'm having trouble creating a mySQL query that does a running total by a certain Group By dimensions.
Here is a sample structure of the data:
date | store | Channel | Visits | YTD Total |
1/1/2019 | A | Banner | 100 | 100 |
1/1/2019 | A | Search | 50 | 50 |
1/1/2019 | A | 25 | 25 | |
2/1/2019 | A | Banner | 75 | 175 |
2/1/2019 | A | Search | 55 | 105 |
2/1/2019 | A | 30 | 55 | |
3/1/2019 | A | Banner | 200 | 375 |
3/1/2019 | A | Search | 85 | 190 |
3/1/2019 | A | 75 | 130 | |
1/1/2019 | B | Banner | 500 | 500 |
1/1/2019 | B | Search | 100 | 100 |
1/1/2019 | B | 65 | 65 | |
2/1/2019 | B | Banner | 300 | 800 |
2/1/2019 | B | Search | 400 | 500 |
2/1/2019 | B | 25 | 90 | |
3/1/2019 | B | Banner | 250 | 1050 |
3/1/2019 | B | Search | 200 | 700 |
3/1/2019 | B | 70 | 160 |
The YTD Total column is what I'm trying to calculate. The raw data is essentially the first 4 columns. I need to create a Running Total of visits for the year by STORE and CHANNEL.
Appreciate anyone's help!! Thanks!
Comments
-
We have the Redshift SQL enabled in our instance and are able to use this SQL line to calculate a running total:
sum(paymentamount) over(partition by ClaimNumber order by dateofeventyear,all_monthsofdev rows unbounded preceding)
I'm not seeing this exact syntax available available in MySQL, so maybe you can get Redshift enabled if it isn't already.
Are the Running Total cards which would eliminate the need to do any SQL not working for you?
**Check out my Domo Tips & Tricks Videos
**Make sure to any users posts that helped you.
**Please mark as accepted the ones who solved your issue.0 -
Thanks for your response. No I don't have Redshift, nor have the option to enable it. I want to do a pre-calculation due to some cards that require it (such as the gauge cards). Otherwise, I would just use the Running Total cards.
If anyone else has any ideas, much appreciated. Thanks!
0 -
@user06643 - You can utilize windowing functions within a beast mode calculation. For example:
sum(`visits`) over(partition by `channel`, `store` order by `date`)
MySQL has supported windowing functions since version 8.0 so this should work in MySQL as well if you're at or above that version. If not then you'd need to utilize variables within MySQL to keep track and then reset the values when your store or channel changes which is quite a bit more complex.
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**0 -
Domo does not have MySQL v8, so you actually won't find support for windowed functions in MySQL dataflows
You can do it using variables
Basic examples
https://stackoverflow.com/questions/2563918/create-a-cumulative-sum-column-in-mysql
With Partitions.
THIS IS THE FAR EASIER SOLUTION!
Slight tweak to @GrantSmith 's beast mode sample.
sum(sum(`visits`)) over(partition by `channel`, `store` order by `date`)
I didn't expect to reshare this video so many times, but you can do what you want to do via windowed functions in Beaast Modes.
https://www.youtube.com/watch?v=ZPf41Fjn1H8
Jae Wilson
Check out my 🎥 Domo Training YouTube Channel 👨💻
**Say "Thanks" by clicking the ❤️ in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 101 SQL DataFlows
- 622 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 748 Beast Mode
- 60 App Studio
- 41 Variables
- 688 Automate
- 177 Apps
- 453 APIs & Domo Developer
- 48 Workflows
- 10 DomoAI
- 36 Predict
- 15 Jupyter Workspaces
- 21 R & Python Tiles
- 397 Distribute
- 114 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 126 Manage
- 123 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 109 Community Announcements
- 4.8K Archive