我正在尝试在MySQL中的数据库表上运行查询。我试图找出连续几秒钟内的最大瓦特平均值。因此,例如,我有一个表(提供图片),我想查询该表以查找特定人在表中连续60秒存在的最大平均瓦特。我附上一张表格的图片,以便您可以看到数据结构。在此先感谢您提供的任何帮助。
这是我尝试过的方法,但无法弄清楚连续的时间段。
select max(avg_watts)
from
(
SELECT AVG(watts) avg_watts FROM tblworkoutdata GROUP BY personid
) tmp
答案 0 :(得分:0)
那并不像看起来那么容易。假设您有以下数据:
minute watts 1 100 59 200 60 300 70 300 80 500 120 100
这给出了以下连续60分钟的范围:
minute avg watts 1-60 150 59-80 325 70-120 300
但是为什么用什么规则找到这些范围呢?我将使用以下算法:
然后
查询:
select max(avg_watts)
from
(
select
(
select avg(watts)
from tblworkoutdata data
where data.time between groups.t1 and groups.t2
) as avg_watts
from
(
select min(t1) as t1, t2
from
(
select
time as t1,
(
select max(tbl2.time)
from tblworkoutdata tbl2
where tbl2.time between tbl1.time and tbl1.time + 59
) as t2
from tblworkoutdata tbl1
) pairs
group by t2
) groups
) averages;
上个月的演示:http://rextester.com/OZT99379
当然,如果您愿意,可以将子查询从SELECT
子句移到FROM
子句。
(很容易获得每个人的结果。只需在每个地方都包含人员ID并按其将结果分组即可。)
答案 1 :(得分:-1)
您需要使用包含日期时间的任何列将数据表与其自身连接。像这样:
fetch=findViewById(R.id.fetchDocument);
firestore = FirebaseFirestore.getInstance();
upload =findViewById(R.id.uploadText);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
POJO pojo = new POJO();
pojo.setValue("Hello World");
//below code creates collection, creates a inner doc. and uploads the value (pojo)
firestore.collection("RootCollection").document("MyDoc")
.set(pojo).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//gets called if upload is successful
Toast.makeText(ichild.this,"Upload is successful", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ichild.this,"Upload is not successful" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
fetch.setOnClickListener(new View.OnClickListener() {