You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL GROUP BY问题:需新增MachineId列并支持多机器ID查询

解决方案

要实现按**年月(MonthYear)和机器ID(MachineId)**双维度分组统计,同时支持查询多个机器ID,只需对原SQL做3处关键修改:

修改点说明

  • 在SELECT列表中新增"MachineId"字段,用于区分不同机器的统计数据
  • 将WHERE条件中的=改为IN,支持传入多个机器ID参数
  • 在GROUP BY子句中添加"MachineId",确保分组维度包含所有非聚合字段

修改后的完整SQL

select 
    "MachineId",
    to_char("CreateTime", 'YYYY-MM') as MonthYear,
    floor(sum("Time"))::integer / 60 as "#MinutesWorkouts",
    sum(case when "Type" = 29 then 1 else 0 end) as "#Streaming",
    (sum(case when "Type" = 29 then "Time" else 0 end))::integer / 60 as "StreamingMinutes",
    sum(case when "Type" = 9 then 1 else 0 end) as "#GuidedProgram",
    sum(case when "Type" = 28 then 1 else 0 end) as "#Tall"
from 
    match_history
WHERE
    "MachineId" IN ({{Machine_Id}})
GROUP by 
    "MachineId", to_char("CreateTime", 'YYYY-MM')

补充说明

  • 若你的参数工具支持数组格式输入,IN ({{Machine_Id}})可直接适配多ID场景(比如传入1,2,3或[1,2,3]格式的参数)
  • 修改后查询结果会按「机器ID+年月」的组合维度展示,每一行对应单台机器单个月份的训练统计数据

内容的提问来源于stack exchange,提问作者user20102590

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 05:35:21