我正在尝试找出如何通过唯一ID(每个ID仅表示一个)来求和时间。这是一些数据的标记。我需要GROUP BY f_name,l_name和area。我还需要组计数(唯一编号的id)和组参与者(仅简单编号的id)。
+----+------+-------+-------+------+--+ | ID | Time | fname | lname | Area | | +----+------+-------+-------+------+--+ | 1 | 3:30 | Jeff | Chose | LA | | | 1 | 3:30 | Jeff | Chose | LA | | | 1 | 3:30 | Jeff | Chose | LA | | | 2 | 4:00 | Jeff | Chose | LA | | +----+------+-------+-------+------+--+
数据应如下所示:
+------+-------+-------+------+-------------+--------------------+ | Time | fname | lnam | Area | Group Count | Group Participants | +------+-------+-------+------+-------------+--------------------+ | 7:30 | Jeff | Chose | LA | 2 | 4 | | | | | | | | +------+-------+-------+------+-------------+--------------------+
奖金:如果您可以将3:30转换为3.5
答案 0 :(得分:0)
WITH cte as (
SELECT fname, lname, Area,
COUNT(DISTINCT id) as "Group Count",
COUNT(id) as "Group Participants",
SUM(DISTINCT "time"::time) as "Time"
FROM Table1
GROUP BY fname, lname, Area, id
)
SELECT fname, lname, Area,
SUM("Group Count") as "Group Count",
SUM("Group Participants") as "Group Participants",
SUM("Time"::time) as "Time"
FROM cte
GROUP BY fname, lname, Area;
输出