【问题标题】:GROUP BY for specific rows针对特定行的 GROUP BY
【发布时间】:2017-09-22 12:10:36
【问题描述】:

我有一个按人分组的 sql server 结果。并且 SUMS 是他们的每日数据和每月数据。

    Person  Team    Daily Figure    Month To Date
    Tony    Team 1          53              635
    Dan     Team 2         47               172
    Tom     Team 3         17               232
    Simon   Team 2          16              655
    Ben     Team 3         17               232
    Fred    Team 2          16              655

如何仅按团队 2 对这些结果进行分组?例如,我仍然希望显示其他人的个人结果,但我只想将团队 2 组合在一起。

示例结果

   Person   Team    Daily Figure    Month To Date
    Tony    Team 1          53              635
    Tom     Team 3         17               232
    Ben     Team 3         17               232
   Team 2   Team 2         79              1482

谢谢 更新:

    SELECT 

     Person = case when Team = 'Team 2' 
     then 'Team 2' else Person  END,
     Team,
     SUM(ISNULL(Figure,0)) AS 'Daily Figure',
     SUM(Month To Date) AS 'Month To Date'

这是我的选择,但错误

【问题讨论】:

    标签: sql sql-server group-by


    【解决方案1】:

    您可以在selectgroup by 中为Person 使用case 表达式:

    select 
        case when Team = 'Team 2' then 'Team 2' else Person end as Person
      , Team
      , sum([Daily Figure]) as [Daily Figure]
      , sum([Month To Date]) as [Month To Date]
    from t
    group by
        case when Team = 'Team 2' then 'Team 2' else Person end
      , Team
    

    rextester 演示:http://rextester.com/XHQ24032

    返回:

    +--------+--------+--------------+---------------+
    | Person | Team   | Daily Figure | Month To Date |
    +--------+--------+--------------+---------------+
    | Tony   | Team 1 |           53 |           635 |
    | Team 2 | Team 2 |           79 |          1482 |
    | Ben    | Team 3 |           17 |           232 |
    | Tom    | Team 3 |           17 |           232 |
    +--------+--------+--------------+---------------+
    

    如果您想使用cross apply() 以避免重复case 表达式:

    select 
        x.Person
      , t.Team
      , sum(t.[Daily Figure]) as [Daily Figure]
      , sum(t.[Month To Date]) as [Month To Date]
    from t
      cross apply (
        select case when t.Team = 'Team 2' then 'Team 2' else t.Person end as Person
        ) as x 
    group by
        x.Person
      , t.Team
    

    或者cross apply(values()):

    select 
        x.Person
      , t.Team
      , sum(t.[Daily Figure]) as [Daily Figure]
      , sum(t.[Month To Date]) as [Month To Date]
    from t
      cross apply (values 
       (case when t.Team = 'Team 2' then 'Team 2' else t.Person end)
        ) as x (Person)
    group by
        x.Person
      , t.Team
    

    【讨论】:

    • CROSS APPLY 将使查询更好一点
    • 我在 Team LIKE 'Team 2' 然后 ''Team 2' else Person END 时尝试了 Person= case,但这只是 select 和 group by 中的错误
    • @RyanGadsdon 不要对字符串使用双引号。当您的样本数据表明Team 是整数时,为什么要使用like 'Team 2',而您只使用Team = 2,或者如果它是字符类型,那么Team = '2'
    • 它也不适用于等于 - “'=' 附近的语法不正确。”
    • @RyanGadsdon 我已经更新了我的演示和代码以使用您更新的问题示例数据,并且使用Team = 'Team 2' 一切正常。如果您使用第一个解决方案和两个 case 表达式,请确保您没有在 group by 中包含别名 Person =,仅使用 group by 中的表达式。
    猜你喜欢
    • 2021-03-15
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多