按列分组,聚合和转换结果

huangapple go评论49阅读模式
英文:

GROUP BY on column, aggregation and transform results

问题

我有以下的表格:

日期         | 数据类型 | 数据数值
01-01-2023 |  最大      |    4
02-01-2023 |  最小      |    7
03-01-2023 |  平均      |    54
04-01-2023 |  最大      |    8
05-01-2023 |  最小      |    98
06-01-2023 |  平均      |    23
01-02-2023 |  最大      |    65
02-02-2023 |  最小      |    2
03-02-2023 |  平均      |    45
04-02-2023 |  最大      |    22
05-02-2023 |  最小      |    56
06-02-2023 |  平均      |    65
01-03-2023 |  最大      |    7
02-03-2023 |  最小      |    5
03-03-2023 |  平均      |    23
04-03-2023 |  最大      |    65
05-03-2023 |  最小      |    51
06-03-2023 |  平均      |    33

从上述表格中,我需要按当前月份分组,并从中获取数据类型为'max'的最大值,数据类型为'min'的最小值,以及数据类型为'avg'的平均值,如下所示:

月份 | 最大 | 最小 | 平均
1      |  8    |  7    |  38.5
2      |  65  |  2    |  55
3      |  65  |  5    |  28

因此,我想请问您最有效的SQL查询,以解决这个请求。

谢谢大家。

英文:

i have the following table:

date       | data_type | data_value
01-01-2023 |  max      |    4
02-01-2023 |  min      |    7
03-01-2023 |  avg      |    54
04-01-2023 |  max      |    8
05-01-2023 |  min      |    98
06-01-2023 |  avg      |    23
01-02-2023 |  max      |    65
02-02-2023 |  min      |    2
03-02-2023 |  avg      |    45
04-02-2023 |  max      |    22
05-02-2023 |  min      |    56
06-02-2023 |  avg      |    65
01-03-2023 |  max      |    7
02-03-2023 |  min      |    5
03-03-2023 |  avg      |    23
04-03-2023 |  max      |    65
05-03-2023 |  min      |    51
06-03-2023 |  avg      |    33

from the following table I would need to group by current month and from it take the max of its values ​​with data_type 'max', the lesser of those of type 'min' and finally the average of the types 'avg', as follows:

mounth | max | min |  avg
1      |  8  |  7  |  38,5
2      |  65 |  2  |  55
3      |  65 |  5  |  28

I therefore wanted to ask you which is the most performing SQL query in order to resolve the request.

Thank you all.

答案1

得分: 1

请注意,你提供的内容是SQL查询语句,我将为你提供翻译后的内容。以下是翻译后的内容:

使用条件聚合如下所示:

    选择 年份(date_) 作为 '年'
           月份(date_) 作为 '月'
           最小(case  data_type = 'min' 时则 data_value end) 作为 '最小'
           最大(case  data_type = 'max' 时则 data_value end) 作为 '最大'
           平均(case  data_type = 'avg' 时则 data_value end) 作为 '平均'
     tbl
    按年份(date_), 月份(date_) 分组

或者,如果你想要对特定年份进行聚合:

    选择 月份(date_) 作为 '月'
           最小(case  data_type = 'min' 时则 data_value end) 作为 '最小'
           最大(case  data_type = 'max' 时则 data_value end) 作为 '最大'
           平均(case  data_type = 'avg' 时则 data_value end) 作为 '平均'
     tbl
    其中 年份(date_) = 2023
    按月份(date_) 分组

[查看演示][1]

  [1]: https://dbfiddle.uk/Vmr6FAvn

请注意,我已经将SQL查询语句翻译成中文,如有其他问题,请随时提出。

英文:

Use conditional aggregation as the following:

select year(date_) as 'yaer',
       month(date_) as 'month',
       min(case when data_type = 'min' then data_value end) as 'min',
       max(case when data_type = 'max' then data_value end) as 'max',
       avg(case when data_type = 'avg' then data_value end) as 'avg'
from tbl
group by year(date_), month(date_)

Or, if you want to aggregate for a specific year:

select month(date_) as 'month',
       min(case when data_type = 'min' then data_value end) as 'min',
       max(case when data_type = 'max' then data_value end) as 'max',
       avg(case when data_type = 'avg' then data_value end) as 'avg'
from tbl
where year(date_) = 2023
group by month(date_)

See demo.

huangapple
  • 本文由 发表于 2023年2月19日 21:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定