我有一张表,上面有员工名单以及他们出售的单位数量。+ q$ y, L+ M/ h- s9 e* b5 C
我想获得售出的前25%的平均单位和售出的下25%的平均单位。 0 T9 O8 J6 e1 x+ {6 g我创建了我的数据SLQ小提琴的表示形式9 x) q5 w1 ~7 M5 X+ k' o" s: w
我真的不知道该如何开始?我看到的所有示例都是针对SQL Server而非MySQL。这就是我的想法。/ k/ N8 s+ L; q
我想要25个百分点,不能限制为25个项目。基本上它将涉及:# X7 s6 @( b4 ~7 ^' N( _
1) #_of_employees = The number of total employees.0 F5 a2 x$ e2 ^! r" b1 Z8 s/ k
2) #_of_employees_in_25_percentile = #_of_employees*0.251 c( j q+ n& z j9 R: Q) d" t* T
3) Calculate the sum of the units sold by the top/bottom 25 percentile (limit #_of_employees_in_25_percentile)) a R2 V" }" I
4) Divide the sum by #_of_employees_in_25_percentile to get the average. 3 M, P8 A! [3 `3 Q- a如何在MySQL中有效地完成所有这些工作? 7 X2 u1 g @# t/ X( s 2 v# \$ W! j' u: D3 b解决方案:9 Y- y, }* b0 w6 S
2 T0 K$ w% s# r, Z- h# L( u
& l7 h: L; \7 F |$ ~ K- o% A' d# g) A" I1 ~1 N 这是一个解决方案,它使用了我从这个问题中学到的一个诡计。* s" T) I+ R X
SELECT id, unit_sold, n * 100 / @total AS percentile , j5 G, R2 n2 JFROM (8 Z4 R+ ~5 G7 o9 \
SELECT id, unit_sold, @total := @total + unit_sold AS n ( e6 h! `+ m3 d2 J FROM mydata, (SELECT @total := 0) AS total& [+ J) k8 e* Q; n! @! Y/ ~' e1 X- s
ORDER BY unit_sold ASC * L; q& @' z9 X* i) AS t+ C& W0 F& T7 o. Y* Z: \
SQL提琴。