- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:postgresql
我有一些员工的销售数据......表格看起来像这样:
SalesRep # of sales/day NTILE(2)
-------- -------------- ----------
Johnny 1 1
Johnny 1 1
Johnny 4 1
Johnny 5 2
Johnny 5 2
Johnny 5 2
Sara 2 1
Sara 2 1
Sara 2 1
Sara 3 2
Sara 4 2
Sara 5 2
... ... ...
我想找出每个销售代表在 50% 表现最差的日子以及 50% 表现最好的日子里每天的平均销售额
例如,我希望输出表看起来像这样:
SalesRep #ofSales Bottom50% #ofSales Top50%
-------- -------------- ----------
Johnny 2 5
Sara 2 4
... ... ...
到目前为止我有:
select
salesrep,
case when ntile = 1 then avg(numsales) end,
case when ntile = 2 then avg(numsales) end,
...
...
case when ntile = 10 then avg(numsales) end
from (
select
salesrep,
numsales,
NTILE(10) over (PARTITION BY salesrep order by numsales asc) as ntile
from XXX
) as YYY
group by salesrep, ntile
这给了我一个奇怪的错误,输出包括一堆 NULL...见下表:
SalesRep #ofSales Bottom50% #ofSales Top50%
-------- -------------- ----------
Johnny NULL 5
Sara 2 NULL
... ... ...
最佳答案
所需输出中的一行代表单个 SalesRep
– 因此,您必须按 SalesRep
仅 分组。然后 case
表达式将作为参数进入 inside avg
调用,如下所示:
select
salesrep,
<b>avg(</b>case when ntile = 1 then <s>avg(</s>numsales<s>)</s> end<b>)</b>,
<b>avg(</b>case when ntile = 2 then <s>avg(</s>numsales<s>)</s> end<b>)</b>,
...
...
<b>avg(</b>case when ntile = 10 then <s>avg(</s>numsales<s>)</s> end<b>)</b>
from (
select
salesrep,
numsales,
NTILE(10) over (PARTITION BY salesrep order by numsales asc) as ntile
from XXX
) as YYY
group by salesrep<s>, ntile</s>
;
或者,重写上面的内容而不使用那些花哨的格式:
select
salesrep,
avg(case when ntile = 1 then numsales end),
avg(case when ntile = 2 then numsales end),
...
...
avg(case when ntile = 10 then numsales end)
from (
select
salesrep,
numsales,
NTILE(10) over (PARTITION BY salesrep order by numsales asc) as ntile
from XXX
) as YYY
group by salesrep
;
关于sql - 如何在 SQL 中取 NTiles 的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17208370/
我正在使用 MYSQL NTILE 函数,并且在大多数情况下它正在做我需要它做的事情,但是在一种情况下,我需要不同的行为,但我不知道如何去做。这种情况是当我的存储桶多于记录时。 假设我在名为data
我正在尝试申请 ntile在一些嵌套的小标题上,但我似乎无法让它工作。 你能看出我哪里出错了吗? data(iris) iris %>% group_by(Species) %>% mutat
想用NTILE以林地占总土地面积的百分比查看国家的分布。我想使用的列中值的范围是从 0.00053 到非常接近 98.25,并且国家/地区在该范围所隐含的四分位数之间分布不均匀,即 0 到 25、25
此查询: SELECT min( "x" ) as "From", max( "x" ) as "To", sum("quantity") FROM ( SELECT t
我正在阅读 ms sql 的 RANKING 函数。我了解除 NTILE() 之外的其他函数。假设我有这些数据: StudentID MARKS S1
我的数据是 my_basket <- data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Ve
我需要在 C# Linq 中实现以下 SQL: SELECT NTILE (3) OVER (ORDER BY TransactionCount DESC) AS A... 除了 this 之外,我找
需要计算每个学生单次测试分数的百分位排名(第 1 - 99 个百分位)。 我对 NTILE 的 msdn 定义有点困惑,因为它没有明确提及百分位数排名。我需要某种保证 NTILE 是用于计算百分位数排
摘要:我正在尝试按前五分位数和后五分位数对这些股票因素进行排名,以构建多头/空头投资组合。 表: 期望的结果: 使用的代码: UPDATE MOMENTUM_Quintile SET [2006-12
我正在尝试对 BigQuery 上的 Google Merchandise Store 示例数据集执行 RFM 分段。在我的 SQL 查询中,NTILE(5) 根据行顺序将行划分为 5 个存储桶,并返
实际上我需要在 mysql 中执行 ntile(x) over(partition by ) 查询,但我没有得到任何好的链接或 Material 来执行此操作。 所以请帮助我如何通过 partitio
我有一个 csv 文件,内容如下: a,b,c,2 d,e,f,3 g,h,i,3 j,k,l,4 m,n,o,5 p,q,r,6 s,t,u,7 v,w,x,8 y,z,zz,9 我想为这个数据分配
我有一个场景,我使用下面的 SQL Server NTILE 函数将多个结果拆分为四分位数。目标是每个类中的行数相同 case NTILE(4) over (order by t2.TotalStd)
编辑:postgresql 我有一些员工的销售数据......表格看起来像这样: SalesRep # of sales/day NTILE(2) --------
我正在尝试使用 NTILE 函数在使用 Navicat for MySQL 的数据集中创建十分位数。我的查询是: select NTILE(10) over (order by 'amount') d
我的理解是 dplyr::ntile 和 statar::xtile 正在尝试做同样的事情。但有时输出是不同的: dplyr::ntile(1:10, 5) # [1] 1 1 2 2 3 3 4 4
我曾尝试根据类似 questions 寻找答案 对 tidyverse 绝对陌生,我有以下问题:如何估计每个 ntile() 的中位数使用 dplyr # Data library(surviv
我有一个查询,我试图为每个 ID 获取与第 99%ile 数据关联的最大成本。我首先计算 ntile 桶,然后尝试过滤出特定 %ile 的最大值。这适用于在 where 子句中指定百分位数但不适用于
我注意到在大表上使用 postgres ntile 会出现奇怪的行为。它并没有真正正确排序。例子是: select tile, min(price) as minprice, max(price) a
我正在尝试使用 NTILE 函数从 pandas 查询 SQLite 数据库,但我没有成功,尽管我已经多次重新检查了语法。 下面的独立示例。设置: import pandas as pd from s
我是一名优秀的程序员,十分优秀!