/*
You are a data analyst at an e-commerce company. Your task is to analyze the distribution of product sales across different regions.
Each sale has a unique `sale_id`, and the dataset includes the region where the sale occurred and the total sales amount.  

Schema:
Table Name:`Sales`  
| Column Name  | Data Type   | Description                     |  
|--------------|-------------|---------------------------------|  
| sale_id      | INT         | Unique identifier for each sale |  
| region       | VARCHAR(10) | Region where the sale occurred  |  
| sales_amount | INT         | Total sales amount for the sale |  

Task: 

The company wants you to write a query to:  
1. Divide the sales into quartiles.
2. Calculate the relative rank of each sale in its region. 
3. Calculate the cumulative distribution of each sale.  
4. Display the following columns in the result:  
   - `region`  
   - `sale_id`  
   - `sales_amount`  
   - `relative_rank`  
   - `quartile`  
   - `cum_dist`  

Expected Output:
| sale_id | region | sales_amount | quartile | relative_rank | cum_dist |
|---------|--------|--------------|----------|---------------|----------|
| 1       | North  | 5000         | 1        | 0             | 0.25     |
| 2       | North  | 7000         | 2        | 0.33          | 0.5      |
| 3       | North  | 8000         | 3        | 0.67          | 0.75     |
| 4       | North  | 10000        | 4        | 1             | 1        |
| 5       | South  | 6000         | 1        | 0             | 0.25     |
| 6       | South  | 9000         | 2        | 0.33          | 0.5      |
| 7       | South  | 10000        | 3        | 0.67          | 0.75     |
| 8       | South  | 12000        | 4        | 1             | 1        |
| 9       | East   | 4000         | 1        | 0             | 0.5      |
| 10      | East   | 4000         | 2        | 0             | 0.5      |
| 11      | East   | 6000         | 3        | 0.67          | 0.75     |
| 12      | East   | 7000         | 4        | 1             | 1        |

*/
CREATE TABLE Sales (  
    sale_id INT PRIMARY KEY,  
    region VARCHAR(10),  
    sales_amount INT 
);  

INSERT INTO Sales (sale_id, region, sales_amount) VALUES  
(1, 'North', 5000.00),  
(2, 'North', 7000.00),  
(3, 'North', 8000.00),  
(4, 'North', 10000.00),  
(5, 'South', 6000.00),  
(6, 'South', 9000.00),  
(7, 'South', 10000.00),  
(8, 'South', 12000.00),  
(9, 'East', 4000.00),  
(10, 'East', 4000.00),  
(11, 'East', 6000.00),  
(12, 'East', 7000.00);  

SELECT
   sale_id,
   region,
   sales_amount,
   NTILE(4) OVER(PARTITION BY region ORDER BY sales_amount) AS quartile,
   ROUND(PERCENT_RANK() OVER(PARTITION BY region ORDER BY sales_amount),2) relative_rank,
   CUME_DIST() OVER(PARTITION BY region ORDER BY sales_amount) cum_dist
FROM Sales
ORDER BY sale_id;

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: