简体   繁体   English

SQL 基本:按 function 分组

[英]SQL Basic: Group by function

I am trying to get a table that would outline two group by functions but having some minor difficulty.我正在尝试获得一个按功能概述两组但有一些小困难的表格。

select 
    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" = {{Machine_Id}}
GROUP by 
    to_char("CreateTime", 'YYYY-MM')

Ideally - I'd like to showcase Machine ID in the column along with the dates.理想情况下 - 我想在列中展示机器 ID 以及日期。 Currently the result shows as (image link) as it only shows the monthyear as the group function.目前结果显示为(图片链接),因为它仅将月份显示为组 function。 I would like to ensure it shows machine ID on the column too as I'd like to add multiple machine IDs.我想确保它也在列上显示机器 ID,因为我想添加多个机器 ID。

在此处输入图像描述

Why not just add machine_id?为什么不只添加 machine_id?

select 
    to_char("CreateTime", 'YYYY-MM') as MonthYear,
    "MachineId",
    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
GROUP by 
    to_char("CreateTime", 'YYYY-MM'), "MachineId"

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

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