Challenge: Top Sales per Region
To solve this task, you need to identify the highest sale amount and the corresponding employee for each region in the sales table. You will use the ROW_NUMBER() window function to achieve this efficiently.
Step-by-step approach
-
Assign a row number within each region:
- Use
ROW_NUMBER()as a window function to assign a unique number to each sale within its region. - Partition the data by the
regioncolumn so that numbering restarts for each region. - Order the rows within each region by the
amountcolumn in descending order; this places the highest sale at the top for each region.
- Use
-
Select only the top sale per region:
- After assigning row numbers, the row with
ROW_NUMBER() = 1in each region will represent the highest sale for that region. - Use a subquery or common table expression (CTE) to filter and keep only those rows where the row number is 1.
- After assigning row numbers, the row with
-
Return the required columns:
- Select the
region,employee_id, andamountcolumns for the top sale in each region.
- Select the
Swipe to start coding
Write a query to find the highest sale amount and corresponding employee for each region using window functions.
- Assign a row number to each sale within its region, ordering by the sale amount in descending order.
- Select the region, employee ID, and sale amount for the row with the highest sale in each region.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 4.55
Challenge: Top Sales per Region
Svep för att visa menyn
To solve this task, you need to identify the highest sale amount and the corresponding employee for each region in the sales table. You will use the ROW_NUMBER() window function to achieve this efficiently.
Step-by-step approach
-
Assign a row number within each region:
- Use
ROW_NUMBER()as a window function to assign a unique number to each sale within its region. - Partition the data by the
regioncolumn so that numbering restarts for each region. - Order the rows within each region by the
amountcolumn in descending order; this places the highest sale at the top for each region.
- Use
-
Select only the top sale per region:
- After assigning row numbers, the row with
ROW_NUMBER() = 1in each region will represent the highest sale for that region. - Use a subquery or common table expression (CTE) to filter and keep only those rows where the row number is 1.
- After assigning row numbers, the row with
-
Return the required columns:
- Select the
region,employee_id, andamountcolumns for the top sale in each region.
- Select the
Swipe to start coding
Write a query to find the highest sale amount and corresponding employee for each region using window functions.
- Assign a row number to each sale within its region, ordering by the sale amount in descending order.
- Select the region, employee ID, and sale amount for the row with the highest sale in each region.
Lösning
Tack för dina kommentarer!
single