我有一张表,列出了员工名单和他们销售的单位数量。
我希望销售前25%的平均销售量和25%的平均销售量。
我创建了数据SLQ Fiddle
的表示我真的不知道如何开始这个?我看到的所有示例都是针对SQL Server而不是MySQL。这就是我的想法。
我想要25个百分点并且不能限制25个项目。基本上它会涉及:
1) #_of_employees = The number of total employees.
2) #_of_employees_in_25_percentile = #_of_employees*0.25
3) Calculate the sum of the units sold by the top/bottom 25 percentile (limit #_of_employees_in_25_percentile)
4) Divide the sum by #_of_employees_in_25_percentile to get the average.
如何在MySQL中有效地完成所有这些工作?
答案 0 :(得分:2)
这是一个使用我学到的狡猾技巧的解决方案from this question。
SELECT id, unit_sold, n * 100 / @total AS percentile
FROM (
SELECT id, unit_sold, @total := @total + unit_sold AS n
FROM mydata, (SELECT @total := 0) AS total
ORDER BY unit_sold ASC
) AS t
答案 1 :(得分:0)
如何使用这种逻辑:
这是您正在寻找的逻辑类型吗?
示例查询:
$q1 = mysql_query(SELECT * FROM table_name ORDER BY percentile DESC LIMIT 25)
$q2 = mysql_query(SELECT * FROM table_name ORDER BY percentile ASC LIMIT 25)
答案 2 :(得分:0)
这个怎么样?
SELECT
SUM(unit_sold) AS sum_tot, SUM(unit_sold)/count(id) AS average,
SUM(CASE WHEN percentile<25 THEN unit_sold ELSE 0 END) AS sum_top25,
SUM(CASE WHEN percentile<25 THEN 1 ELSE 0 END) AS count_top25,
SUM(CASE WHEN percentile<25 THEN unit_sold ELSE 0 END)/SUM(CASE WHEN percentile<25 THEN 1 ELSE 0 END) AS average_top25,
SUM(CASE WHEN percentile>75 THEN unit_sold ELSE 0 END) AS sum_bottom25,
SUM(CASE WHEN percentile>75 THEN 1 ELSE 0 END) AS count_bottom25,
SUM(CASE WHEN percentile>75 THEN unit_sold ELSE 0 END)/SUM(CASE WHEN percentile>75 THEN 1 ELSE 0 END) AS average_bottom25
FROM
(SELECT
id, unit_sold, c * 100 / @counter AS percentile
FROM
(SELECT
m.*, @counter:=@counter+1 AS c
FROM
(SELECT @counter:=0) AS initvar, mydata AS m
ORDER BY unit_sold desc
) AS t
WHERE
c <= (25/100 * @counter)
OR c >= (75/100 * @counter)
) AS t2
输出:
SUM_TOT AVERAGE SUM_TOP25 COUNT_TOP25 AVERAGE_TOP25 SUM_BOTTOM25 COUNT_BOTTOM25 AVERAGE_BOTTOM25
850 283.3333 500 1 500 350 2 175
请参阅SQL Fiddle。
我们的想法是使用MySQL: LIMIT by a percentage of the amount of records?解决方案来获得百分位数。基于此(以及pdw answer),我们创建了一个输出,其中我们只显示前25%和后75%。
最后,我们计算并求和以获得您请求的值。
请注意,这会在命令之上运行:
SELECT
id, unit_sold, c * 100 / @counter AS percentile
FROM
(SELECT
m.*, @counter:=@counter+1 AS c
FROM
(SELECT @counter:=0) AS initvar, mydata AS m
ORDER BY unit_sold desc
) AS t
WHERE
c <= (25/100 * @counter)
OR c >= (75/100 * @counter)
谁的输出是:
ID UNIT_SOLD PERCENTILE
d 500 20
a 250 80
e 100 100