Top 10 for multiple Categories
** Please like responses by clicking on the thumbs up
** Please Accept / check the answer that solved your problem / answered your question.
Best Answer
-
@AdamT I know this is super old but wanted to say thanks for posting that. I had a use-case where I needed to do something similar and was able to do it in ETL's rank and windows vs MySQL. But not sure if Rank&Window was an option in 2016, I can't remember. But posting this in case someone else sees this old post and needs some help too. Thanks again.
1
Answers
-
Hi @Shevy - Here is a post from one of our team members here at Domo on this very same topic. Below is the MySQL query he used to achieve this in a DataFlow.
Here is a generalized version of what I used to see the top 5 customers for each sales team based on revenue, in case it's a good starting point for someone else. The query assumes that the data has already been grouped, so In my case, I first wrote a transform to group the data based on the Sales Team and Customer, summing the Revenue for each record. These fields have been generalized as Group_Field, Item_Field, and Measure_Field, respectively, in the code below.
SELECT
`Group_Field` -- The column that indicates the group of records for which we want to find the top n records
, `Item_Field` -- The thing of which we want to find the top n for each group
, `Measure_Field` -- The column we used to order the records so we know which are the top n
FROM
(
SELECT
`Group_Field`
, `Item_Field`
, `Measure_Field`
/* Gives an incremental row number if the group hasn't changed. If it has, it starts over at 1. */
, (@row_num := CASE WHEN @Group_Variable = `Group_Field` THEN @row_num + 1 ELSE 1 END) as row_number
/* Updates the value of @Group_Variable so it can be used in the next iteration */
, (@Group_Variable := Group_Field) as `notUsed`
FROM
data_grouped_by_group_field_and_item_field -- Replace with name of table containing grouped values
, (SELECT @row_num := 0) n -- Initializes @row_num variable to 0
, (SELECT @Group_Variable := '') t -- Initializes @Group_Variable to empty string
ORDER BY
`Group_Field`
, `Measure_Field`
) sub_select
WHERE
/* Use this to indicate how many Item records to return for each Group (currently set to 5) */
sub_select.row_number <= 5
-- END OF QUERYThis query adds row numbers to records within each group in the subquery, allowing you to restrict the number of records coming back by setting the max row number you care about in the WHERE clause.
-----
I work for Domo.
**Say "Thanks" by clicking the thumbs up in the post that helped you.
**Please mark the post that solves your problem as "Accepted Solution"1 -
@AdamT I know this is super old but wanted to say thanks for posting that. I had a use-case where I needed to do something similar and was able to do it in ETL's rank and windows vs MySQL. But not sure if Rank&Window was an option in 2016, I can't remember. But posting this in case someone else sees this old post and needs some help too. Thanks again.
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
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 753 Beast Mode
- 61 App Studio
- 41 Variables
- 692 Automate
- 177 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive