Write a query to find the difference between the maximum and minimum values in a dataset where the maximum element always appears at a higher index than the smaller element.
Use dataset_values and evaluate the condition independently for each dataset_id. Ignore NULL values and exclude datasets without at least one valid ordered pair.
dataset_id and max_min_difference.dataset_id ascending.| Column | Type | Description |
|---|---|---|
| dataset_idPK | INT | Unique dataset identifier |
| dataset_name | VARCHAR(100) | Human-readable dataset name |
| Column | Type | Description |
|---|---|---|
| value_idPK | INT | Unique value row identifier |
| dataset_id | INT | Dataset containing the value |
| element_index | INT | Position of the value within its dataset |
| element_value | INT | Numeric value at the specified index |