简体   繁体   English

用于收缩行的简单 SQL 语句

[英]Simple SQL Statement to Shrink Rows

I have a simple table that is grouped by the INTERVAL column.我有一个按INTERVAL列分组的简单表。 Each INTERVAL has three corresponding rows, and each row has one value that corresponds to the average of some type of weight losses while the other types of weight losses are NULL .每个INTERVAL有三个对应的行,每行有一个值对应于某种类型的重量损失的平均值,而其他类型的重量损失是NULL I would like to put all three types of weight losses into one row.我想把所有三种减肥方式放在一排。 Is there any simple way to achieve this?有什么简单的方法可以实现这一目标吗?

Here is the original table that I have:这是我拥有的原始表格:

桌子

Here is the table that I want it to look like:这是我希望它看起来像的表格:

 ---------------------------------------------------------------
| INTERVAL | AVG los MANAGED | AVG los NPIC | AVG los UNMANAGED |
 ---------------------------------------------------------------
|1000-1249 | 54.627118644067 | 52.485833333 | 46.379746835443   |
 ---------------------------------------------------------------
|1250-1499 | 39.734567901234 | 36.705544933 | 33.1023622047244  |
 ---------------------------------------------------------------
| ...      | ...             | ...          | ...               |
 ---------------------------------------------------------------
select `INTERVAL`, max(`AVG los MANAGED`), max(`AVG los NPIC`), max(`AVG los UNMANAGED`)
from your_table
group by `INTERVAL` 

Just aggregate by interval and take the max of each column:只需按间隔聚合并取每列的最大值:

SELECT
    "INTERVAL",
    MAX("AVG los MANAGED")   AS "MAX los MANAGED",
    MAX("AVG los NPIC")      AS "AVG los NPIC",
    MAX("AVG los UNMANAGED") AS "AVG los UNMANAGED"
FROM yourTable
GROUP BY
    "INTERVAL";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM