Skip to content

PERCENTILE_DISC

PERCENTILE_DISC(numeric_literal) WITHIN GROUP (ORDER BY expr1 [ASC | DESC] [NULLS {FIRST | LAST}]) OVER ([query_partition_clause] order_by_clause)

Section titled “PERCENTILE_DISC(numeric_literal) WITHIN GROUP (ORDER BY expr1 [ASC | DESC] [NULLS {FIRST | LAST}]) OVER ([query_partition_clause] order_by_clause)”
  • numeric_literal: This is a numeric value that falls between 0 and 1 to represent the percentile that will be calculated. The number specified is treated as a Continuous Distribution measure to calculate observations or numbers in a distribution.
  • order by expr1 [asc | desc]: This is used to determine the order in which values within the group will be sorted. It sorts based on ‘expr1’, which is a column or expression that data should be ordered by. ‘ASC’ means data is sorted in ascending order whereas ‘DESC’ means data is sorted in descending order.
  • nulls {first | last}: This is optional and specifies the position of null values in the ordered list. NULLS FIRST will place nulls at the start of the list whereas NULLS LAST will place the nulls at the end of the list.
  • query_partition_clause: It distributes the result set into partitions or groups of rows to which the percentile_disc function is applied. If not specified, the function treats all rows of the query result set as a single group.
  • order_by_clause: This is the ORDER BY clause in the OVER function. It sorts the rows in each partition. If not specified, the function treats all rows of the partition as a single group.
SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY Employee_Salary) AS Median
FROM Employee_Salary_Table
45000

In this example, the PERCENTILE_DISC function is used to calculate the median salary (0.5 corresponds to the median) of all employees from the Employee_Salary_Table. The ORDER BY clause ensures the salaries are ordered in ascending order before percentile calculation. The result, 45000, represents the median salary.