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

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

GROUP BY on column, aggregation and transform results

问题

我有以下的表格:

  1. 日期 | 数据类型 | 数据数值
  2. 01-01-2023 | 最大 | 4
  3. 02-01-2023 | 最小 | 7
  4. 03-01-2023 | 平均 | 54
  5. 04-01-2023 | 最大 | 8
  6. 05-01-2023 | 最小 | 98
  7. 06-01-2023 | 平均 | 23
  8. 01-02-2023 | 最大 | 65
  9. 02-02-2023 | 最小 | 2
  10. 03-02-2023 | 平均 | 45
  11. 04-02-2023 | 最大 | 22
  12. 05-02-2023 | 最小 | 56
  13. 06-02-2023 | 平均 | 65
  14. 01-03-2023 | 最大 | 7
  15. 02-03-2023 | 最小 | 5
  16. 03-03-2023 | 平均 | 23
  17. 04-03-2023 | 最大 | 65
  18. 05-03-2023 | 最小 | 51
  19. 06-03-2023 | 平均 | 33

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

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

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

谢谢大家。

英文:

i have the following table:

  1. date | data_type | data_value
  2. 01-01-2023 | max | 4
  3. 02-01-2023 | min | 7
  4. 03-01-2023 | avg | 54
  5. 04-01-2023 | max | 8
  6. 05-01-2023 | min | 98
  7. 06-01-2023 | avg | 23
  8. 01-02-2023 | max | 65
  9. 02-02-2023 | min | 2
  10. 03-02-2023 | avg | 45
  11. 04-02-2023 | max | 22
  12. 05-02-2023 | min | 56
  13. 06-02-2023 | avg | 65
  14. 01-03-2023 | max | 7
  15. 02-03-2023 | min | 5
  16. 03-03-2023 | avg | 23
  17. 04-03-2023 | max | 65
  18. 05-03-2023 | min | 51
  19. 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:

  1. mounth | max | min | avg
  2. 1 | 8 | 7 | 38,5
  3. 2 | 65 | 2 | 55
  4. 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查询语句,我将为你提供翻译后的内容。以下是翻译后的内容:

  1. 使用条件聚合如下所示:
  2. 选择 年份(date_) 作为 '年'
  3. 月份(date_) 作为 '月'
  4. 最小(case data_type = 'min' 时则 data_value end) 作为 '最小'
  5. 最大(case data_type = 'max' 时则 data_value end) 作为 '最大'
  6. 平均(case data_type = 'avg' 时则 data_value end) 作为 '平均'
  7. tbl
  8. 按年份(date_), 月份(date_) 分组
  9. 或者,如果你想要对特定年份进行聚合:
  10. 选择 月份(date_) 作为 '月'
  11. 最小(case data_type = 'min' 时则 data_value end) 作为 '最小'
  12. 最大(case data_type = 'max' 时则 data_value end) 作为 '最大'
  13. 平均(case data_type = 'avg' 时则 data_value end) 作为 '平均'
  14. tbl
  15. 其中 年份(date_) = 2023
  16. 按月份(date_) 分组
  17. [查看演示][1]
  18. [1]: https://dbfiddle.uk/Vmr6FAvn

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

英文:

Use conditional aggregation as the following:

  1. select year(date_) as 'yaer',
  2. month(date_) as 'month',
  3. min(case when data_type = 'min' then data_value end) as 'min',
  4. max(case when data_type = 'max' then data_value end) as 'max',
  5. avg(case when data_type = 'avg' then data_value end) as 'avg'
  6. from tbl
  7. group by year(date_), month(date_)

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

  1. select month(date_) as 'month',
  2. min(case when data_type = 'min' then data_value end) as 'min',
  3. max(case when data_type = 'max' then data_value end) as 'max',
  4. avg(case when data_type = 'avg' then data_value end) as 'avg'
  5. from tbl
  6. where year(date_) = 2023
  7. 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:

确定