【问题标题】:SQL to 'group' certain recordsSQL 对某些记录进行“分组”
【发布时间】:2015-04-14 20:35:02
【问题描述】:

我不完全确定这在 SQL 中是否可行(我绝不是专家)。

我在 TABLEA 中有如下几行数据:

我希望有一个 SQL 输出,它将 Activity!=D 的任何记录“分组”在一起。输出结果需要如下表所示:

任何“合并”的活动都将被归为“合并”。在示例中,这将是 A 和 B。

我开始了

select 
  cycle_start,
  cycle_end,
  activity_start,
  activity_end,
  case when (Activity="D") then "D" else "Merged" end

但是我很难得到正确的聚合

【问题讨论】:

    标签: sql oracle11g group-by


    【解决方案1】:

    是的,您可以在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)
    

    【讨论】:

    • @user2407394 。 . .谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多