SQL 到 'group' 某些记录
SQL to 'group' certain records
我不完全确定这在 SQL 中是否可行(我绝不是专家)。
我在 TABLEA 中有如下数据行:
我希望有一个 SQL 输出,它将 'group' 任何记录放在一起 Activity!=D。输出结果需要类似于下面的 table:
任何 'merged' 活动将被分组为 'Merged'。在示例中,这将是 A 和 B。
我开始了
select
cycle_start,
cycle_end,
activity_start,
activity_end,
case when (Activity="D") then "D" else "Merged" end
但后来我很难让聚合正确
是的,您可以在 group by
:
中使用 case
select cycle_start, cycle_end,
min(activity_start) as activity_start,
max(activity_end) as activity_end,
(case when Activity = 'D' then 'D' else 'Merged' end)
from table t
group by cycle_start, cycle_end,
(case when Activity = 'D' then 'D' else 'Merged' end)
我不完全确定这在 SQL 中是否可行(我绝不是专家)。
我在 TABLEA 中有如下数据行:
我希望有一个 SQL 输出,它将 'group' 任何记录放在一起 Activity!=D。输出结果需要类似于下面的 table:
任何 'merged' 活动将被分组为 'Merged'。在示例中,这将是 A 和 B。
我开始了
select
cycle_start,
cycle_end,
activity_start,
activity_end,
case when (Activity="D") then "D" else "Merged" end
但后来我很难让聚合正确
是的,您可以在 group by
:
case
select cycle_start, cycle_end,
min(activity_start) as activity_start,
max(activity_end) as activity_end,
(case when Activity = 'D' then 'D' else 'Merged' end)
from table t
group by cycle_start, cycle_end,
(case when Activity = 'D' then 'D' else 'Merged' end)