gpt4 book ai didi

mysql聚合函数问题(如何将同一组内特定值前后的值相加)

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:41 24 4
gpt4 key购买 nike

我正在处理从工作中获得的一些数据,我正在尝试提出一个查询,让我的生活变得更加轻松(我花时间在 mysql 上导入这些数据。)

我有一堆在不同时间具有不同值(面积)的样本(有点像钟形曲线),所以我需要做的就是将最大峰之前的每个峰(面积)和每个峰(面积)相加在每个样本的最大峰值(总共两列)之后。这个任务听起来很简单,但我很难想出一个可行的查询。

我想到了类似的东西,但问题是我不能在 where 子句中进行“分组依据”,因为子查询中返回了多行,所以我无法比较 sample 。我尝试了几种不同的方法,但它们都无济于事。任何帮助将不胜感激。

SELECT Sample_name, sum(per_area) As '% area'/*For the areas before the peak.*/
FROM W_data.SEC_results
Where retention between /*retention = time */
0
AND
(( select retention
from W_data.SEC_results
where per_area = (
select max(per_area)
from W_data.SEC_results /* select the largest area in the entire set, instead of a specific samples */
)))
group by vial;

表:

+----------------------------------+------+-------------+----------+
| Sample_name | vial | retention | per_area |
+----------------------------------+------+-------------+----------+
| a | 74 | 14.146 | 0.08 |
| a | 74 | 16.624 | 99.79 |
| a | 74 | 20.343 | 0.13 |
| b | 75 | 12.438 | 0.16 |
| b | 75 | 13.653 | 1.85 |
| b | 75 | 16.588 | 97.95 |
| b | 75 | 20.316 | 0.04 |

+-------------+----------------+-------------+
| sample_name | Area( before) |Area (after) |
+-------------+----------------+-------------+
| a | 0.08 | 0.13 |
| b | 2.01 | 0.04 |

最佳答案

逻辑是:-首先找到所有小瓶的最大 per_area

select vial,max(per_area) maxarea from sec_results group by vial

| 74 | 99.79 |

| 75 | 97.95 |

然后为他们找到各自的时间

select sr.vial,sr.time,mt.maxarea from sec_results sr,
(select vial,max(per_area) maxarea from sec_results group by vial) mt

| 74 | 16.624 | 99.79 |

| 75 | 16.588 | 97.95 |

然后分别将低于和高于这些时间的值相加。

select a.sample_name,sum(if(a.time<temp.time,a.per_area,0)) Area_before,
sum(if(a.time>temp.time,a.per_area,0)) Area_after
from sec_results a, (select sr.vial,sr.time,mt.maxarea
from sec_results sr,(select vial,max(per_area) maxarea
from sec_results
group by vial) mt
where sr.vial = mt.vial
and sr.per_area = mt.maxarea
) temp
where a.vial = temp.vial
group by a.vial,a.sample_name;

关于mysql聚合函数问题(如何将同一组内特定值前后的值相加),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com