Home > database >  Please comment:
Please comment:

Time:10-05

How to group for the median? For example: the median wages all departments

CodePudding user response:

Don't know what is your definition of median, can only guess one,

 
WITH CTE_1
AS
(SELECT *,
DENSE_RANK () OVER (PARTITION BY department ORDER BY salary) AS SEQ
FROM the TABLE),

CTE_2
AS
(SELECT *
MAX (SEQ) OVER (PARTITION BY department) AS MAX_SEQ
The FROM CTE_1)


SELECT *
The FROM CTE_2
WHERE SEQ=between ((MAX_SEQ * 1.0)/2)

CodePudding user response:

Thanks for sharing, just tested the, if there are 6 a set of data, at the time of taking the median only took third, rather than take the average of the 3 or 4, do you have a method to optimize it for me
  • Related