我有一个非常简单的表格,如下所示:
CREATE TABLE IF NOT EXISTS LuxLog (
Sensor TINYINT,
Lux INT,
PRIMARY KEY(Sensor)
)
它包含来自不同传感器的数千条日志
我希望所有传感器都有Q1和Q3
我可以对每个数据进行一次查询,但最好对所有传感器进行一次查询(通过一次查询返回Q1和Q3)
我认为这将是一个相当简单的操作,因为四分位数被广泛使用,并且是频率计算中的主要统计变量之一。事实上,我发现了大量过于复杂的解决方案,而我希望找到一些简洁明了的解决方案
谁能给我一个提示
编辑:这是我在网上找到的一段代码,但它对我不起作用:
SELECT SUBSTRING_INDEX(
SUBSTRING_INDEX(
GROUP_CONCAT( -- 1) make a sorted list of values
Lux
ORDER BY Lux
SEPARATOR ','
)
, ',' -- 2) cut at the comma
, 75/100 * COUNT(*) -- at the position beyond the 90% portion
)
, ',' -- 3) cut at the comma
, -1 -- right after the desired list entry
) AS `75th Percentile`
FROM LuxLog
WHERE Sensor=12
AND Lux<>0
我得到1作为返回值,而它应该是一个可以除以10的数字(10,20,30…..1000)
请参见SQLFIDLE:http://sqlfiddle.com/#!9/accca6/2/6 注意:对于SQLFIDLE,我已经生成了100行,1到100之间的每个整数都有一行,但它是一个随机顺序(在excel中完成)
代码如下:
编辑:
基本推理如下: 对于四分位1,我们希望从顶部得到25%,因此我们希望知道有多少行,即:
现在我们知道了行数,我们想知道其中25%是多少,这是这一行:
然后,为了找到一个四分位数,我们要按Lux对LuxLog表进行排序,然后获得行号“@quartile”,为了做到这一点,我们将偏移量设置为@quartile,表示我们要从行号@quartile开始选择,我们说limit 1表示我们只想检索一行。那就是:
对于另一个四分位数,我们的做法(几乎)相同,但我们不是从顶部开始(从较高的值到较低的值),而是从底部开始(这解释了ASC)
但是现在我们只在变量@sql_q1和@sql_q3中存储了字符串,所以将它们连接起来,我们合并查询结果,准备查询并执行它
像这样的东西应该可以做到:
以下是完整的示例:
使用NTILE很简单,但它是一个Postgres函数。你基本上就是这样做的:
下面是我在SQLFIDLE上为您制作的一个简单示例:http://sqlfiddle.com/#!15/7f05a/1
在MySQL中,您将使用RANK。。。以下是SQLFIDLE:http://www.sqlfiddle.com/#!2/d5587/1(来自下面链接的问题)
MySQL RANK()的使用来自这里回答的Stackoverflow:Rank function in MySQL
寻找萨尔曼A.的答案
相关问题 更多 >
编程相关推荐