仅选择75%磁贴中的值并高于约束

时间:2016-03-07 03:19:08

标签: postgresql return limit

我试图让这个查询正常工作......

(S [(zero) $1]
   [(L) $1])
(zero [(ZERO) 0])

   **(L [(T) $1]
     [(T D) (* (expt 10 $2) $1)]
      [(T D L) (+ (* (expt 10 $2) $1) $3) ])**

(T [(H) $1]
   [(A HUNDRED H) (+ (* $1 100) $3)]
   [(A HUNDRED) (* $1 100)])
(H [(B) $1]
   [(C) $1]
   [(C B) (+ $1 $2)]) 
(A [(UNDERTEN) (undertwenty-number $1)])
(B [(UNDERTWENTY) (undertwenty-number $1)]
   [(A) $1])
(C [(TENS) (tens-number $1)])
(D [(THOUSANDS) (thousands-number $1)])
)
 )
   )

有关行的一些附加信息:

  • 166行 - 25%
  • 331行 - 50%
  • 497行 - 75%
  • 662行 - 100%

这些行的工资为75,000加:

235/662 =〜。35
.35 * 662 = ~235行。

我试图让上面的查询返回所有薪水大于75,000但仍然在前497行的行。当我运行上面的查询时,它返回从75,000开始并受497行返回约束限制的所有行。

我不确定如何才能返回限制约束的前497行中超过75,000的工资。

2 个答案:

答案 0 :(得分:0)

您可以将总行数除以当前行数来获取:

select salary
from (
  select salary,
         count(*) over () as total_count,
         row_number() over (order by salary) as rn
  from agent
  where salary > 75000
) t
where (rn / total_count::numeric) <= 0.75
order by salary asc

答案 1 :(得分:0)

使用row_number:

select salary, row_number() over (order by salary) row_num
from agent
where row_num < (select ROUND(count(salary) * .75) from agent)
and salary > 75000