mysql 选择前三行_一个mysql查询,每行获取前三行的平均值(one mysql query getting, per each row, the average of the previous...

一个mysql查询,每行获取前三行的平均值(one mysql query getting, per each row, the average of the previous three rows)

我有这样的事情:

id | value

---------------

201311 | 10

201312 | 15

201401 | 20

201402 | 5

201403 | 17

我需要这样的结果:

201311 | NULL or 0

201312 | 3.3 // 10/3

201401 | 8.3 // (15+10)/3

201402 | 15 // (20+15+10)/3

201403 | 13.3 // (5+20+15)/3

到目前为止,我已经达到了这样的程度:我可以获得前三行的AVG,如下所示:

select AVG(c.value) FROM (select b.value from table as b where b.id < 201401 order by b.id DESC LIMIT 3) as c

手动传递id。 我不能为每个id做这件事。

任何想法将不胜感激!

非常感谢。

问候

i have something like this:

id | value

---------------

201311 | 10

201312 | 15

201401 | 20

201402 | 5

201403 | 17

and i need a result like this:

201311 | NULL or 0

201312 | 3.3 // 10/3

201401 | 8.3 // (15+10)/3

201402 | 15 // (20+15+10)/3

201403 | 13.3 // (5+20+15)/3

So far, i got to the point where i can get the AVG of the last three previous rows like this:

select AVG(c.value) FROM (select b.value from table as b where b.id < 201401 order by b.id DESC LIMIT 3) as c

passing the id manually. I'm not able to do it for each id.

Any ideas would be much appreciated!

thanks a lot.

regards

原文:https://stackoverflow.com/questions/26274590

2019-07-13 10:01

满意答案

我认为你必须编写一个存储过程,使用游标,遍历表并使用游标循环中计算的值填充新表。 如果您在编写游标循环时需要帮助,只需删除注释,我就可以为您举个例子。

i got to this now:

SELECT a.id, (select AVG(b.value) FROM table as b where b.id < a.id AND str_to_date(CONCAT(b.id,'01'), '%Y%m%d') >= DATE_SUB(str_to_date(CONCAT(a.id,'01'), '%Y%m%d'), INTERVAL 3 MONTH)) FROM `table` as a WHERE 1

But i'm quite sure there should be a better/cleaner solution

2014-10-09

相关问答

显然是 SELECT AVG(P1_Score)

Obviously it is SELECT AVG(P1_Score)

我认为你必须编写一个存储过程,使用游标,遍历表并使用游标循环中计算的值填充新表。 如果您在编写游标循环时需要帮助,只需删除注释,我就可以为您举个例子。 i got to this now: SELECT a.id, (select AVG(b.value) FROM table as b where b.id < a.id AND str_to_date(CONCAT(b.id,'01'), '%Y%m%d') >= DATE_SUB(str_to_date(CONCAT(a.id,'01'), ...

这是非常基本的,应该可以通过搜索找到。 SELECT COUNT(field) as `count`, AVG(field) as `average` FROM table_name

This is pretty basic and should have been discoverable via search. SELECT COUNT(field) as `count`, AVG(field) as `average` FROM table_name

这个怎么样? 好又简单...... SELECT

AVG(var1) AS var1,

AVG(var2) AS var2,

AVG(var3) AS var3,

(

SUM(ifnull(var1,0))+

SUM(ifnull(var2,0))+

SUM(ifnull(var3,0))

) / (COUNT(var1)+COUNT(var2)+COUNT(var3))

AS metric_total

FROM test

How about this? nice and simple....

是的,它可以在MySQL中完成。 我认为它甚至可以作为SELECT语句来做(但这将非常复杂,难以维护并且可能非常慢)。 既然您目前还不确定是否可以在MySQL中执行此操作并且在此处询问,我建议使用PHP实现解决方案可能会更好地利用您的时间 - 您可以从答案中剪切和粘贴代码,这可能会出现给出正确的结果,但您是否能够评估解决方案的细微差别? 你能在破裂时修好它吗? 升级它以适应新功能? 如果是我,我会使用MySQL程序将其实现为有限状态机 (实际上是3个FSM,每个条件一个)。 你没有说出满足条件时你...

第一个SELECT真的有必要吗? SELECT

AVG(time)

FROM

(

SELECT

UNIX_TIMESTAMP(max(datelast)) - UNIX_TIMESTAMP(min(datestart)) AS time

FROM

table

WHERE

product_id = 12394 AND datelast > '2011-04-13 00:26:59'

GROUP BY

id

)

我现在无法测试,我认为它也会起作用...

不是我承认的最优雅,但它应该得到你想要的。 我不确定你想如何处理那些小时平均为NULL的NULL值。 在下面的示例中,这些将更新为-1。 create table myTable

(myDate datetime not null,

P_f decimal(10,5) default null

);

insert into myTable(myDate,P_f) values ('2001-01-01 20:20:00',1.88);

insert into myTable(myDate,P_f...

下面的代码大致显示了你可以做什么 $averages = array();

while($row = $result->fetch_assoc())

{

$sum = 0;

$arraySize = array_push($averages,$row['close']);

if($arraySize > 20)

{

array_shift($averages)

}

foreach($averages as $value)

...

相关文章

用source c:\xxx.sql,出现Ignoring query to other databa

...

Hadoop版本:0.20.2 Hive版本:0.9.0 mysql版本: 5.6.11 1) 在my

...

原文出处:http://blog.chenlb.com/2010/08/solr-use-custom

...

原文出处:http://blog.chenlb.com/2010/08/solr-use-custom

...

初步接触solr是对其query的各个参数都不是很了解,现在做一个总结,以便日后查看使用 各个参数及意

...

Solr与Mysql集成指南 chuanliang于 2011-9-24,20:28 Comm

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值