2

我的 SQL 的最后部分似乎有问题。在我的 SQL NULL 中使用汇总功能实现 group by 时出现。如何修改 NULL 以便在此聚合 SQL 中将其替换为 TOTAL?

Current table returned
Name Activate Deactivate
Max      5       2
TAX     12       5
NULL    17       8

select 
    case 
    when (upper(m.email) like '%max.com') then 'MAX'
    when (upper(m.email) like '%tax.com') then 'TAX'
    else 'OTHER'
end Name, 
SUM(case when substring(convert(varchar(8),n.created_on,112),1,6) = '201209' then 1 else 0         end) 'Activated',
   SUM(case when substring(convert(varchar(8),m.LastLockoutDate,112),1,6)='201209' then 1 else 0              end) 'Deactivated'
           from membership.user_details d
          inner join membership.aspnet_membership m 
on m.userid = d.userid
        inner join membership.user_notes n 
         on n.userid = d.userid
        and n.CREATED_ON = (select min(created_on) 
                    from membership.user_notes 
                    where userid = n.userid
                        and note = 'received.')  
         where approved = 1
         group by case when (upper(m.email) like '%max.com') then 'MAX'
      when (upper(m.email) like '%tax.com') then 'TAX'
      else 'OTHER' end     
   with RollUp 
4

4 回答 4

9

ROLLUPanull放在总计行中,因此如果您想替换它,我建议您将现有查询放在子查询中,然后在 aCASE上使用toNAME替换。nullTotal

您的代码将与此类似:

select 
  case when name is null then 'Total' else name end Name,
  sum(Activated) Activated,
  sum(Deactivated) Deactivated
from
(
  select 
    case 
      when (upper(m.email) like '%max.com') then 'MAX'
      when (upper(m.email) like '%tax.com') then 'TAX'
      else 'OTHER'
    end Name, 
  SUM(case when substring(convert(varchar(8),n.created_on,112),1,6) = '201209' then 1 else 0         end) 'Activated',
  SUM(case when substring(convert(varchar(8),m.LastLockoutDate,112),1,6)='201209' then 1 else 0              end) 'Deactivated'
  from membership.user_details d
  inner join membership.aspnet_membership m 
    on m.userid = d.userid
  inner join membership.user_notes n 
    on n.userid = d.userid
    and n.CREATED_ON = (select min(created_on) 
                        from membership.user_notes 
                        where userid = n.userid
                          and note = 'received.')  
  where approved = 1
  group by case when (upper(m.email) like '%max.com') then 'MAX'
        when (upper(m.email) like '%tax.com') then 'TAX'
        else 'OTHER' end     
) src
group by name with rollup

如果您不将查询包装在子查询中,则可以使用以下内容:

select
   case when 
     (case 
        when (upper(email) like '%max.com') then 'MAX'
        when (upper(email) like '%tax.com') then 'TAX'
        else 'OTHER'
      end) is null then 'Total'
      else case 
        when (upper(email) like '%max.com') then 'MAX'
        when (upper(email) like '%tax.com') then 'TAX'
        else 'OTHER'
      end end Name,
  SUM(case when substring(convert(varchar(8),n.created_on,112),1,6) = '201209' then 1 else 0 end) 'Activated',
  SUM(case when substring(convert(varchar(8),m.LastLockoutDate,112),1,6)='201209' then 1 else 0 end) 'Deactivated'
from membership.user_details d
inner join membership.aspnet_membership m 
  on m.userid = d.userid
inner join membership.user_notes n 
  on n.userid = d.userid
  and n.CREATED_ON = (select min(created_on) 
                     from membership.user_notes 
                     where userid = n.userid
                       and note = 'received.')  
where approved = 1
group by case when (upper(m.email) like '%max.com') then 'MAX'
        when (upper(m.email) like '%tax.com') then 'TAX'
        else 'OTHER' end with rollup
于 2013-03-13T22:28:15.860 回答
6

使用这个直接的名称参考:

case when grouping(Name) then 'Total' else Name end as Name

如果只使用单一级别的分组,则对 null 的测试就足够了;但是 grouping(...) 测试允许为每个不同的分组级别生成不同的描述。

于 2013-03-13T22:16:31.860 回答
0

而不是这部分

case when (upper(m.email) like '%max.com') then 'MAX' when (upper(m.email) like '%tax.com') then 'TAX' else 'OTHER' end Name

只需使用

上(替换(COALESCE(m.email,'其他'),'.com',''))作为名称

无需编写子查询并将其进一步复杂化。

于 2018-09-26T04:55:41.737 回答
0

你可以用一个函数包装你的case语句:isnull

select 
    isnull
        (
        case 
            when (upper(m.email) like '%max.com') then 'MAX'
            when (upper(m.email) like '%tax.com') then 'TAX'
            else 'OTHER'
        end,
        'Total'
        ) Name, 
    SUM(case
            when substring(convert(varchar(8),n.created_on,112),1,6) = '201209'
            then 1
            else 0
        end) 'Activated',
    SUM(case
            when substring(convert(varchar(8),m.LastLockoutDate,112),1,6)='201209'
            then 1
            else 0
        end) 'Deactivated'
from membership.user_details d
    inner join membership.aspnet_membership m 
        on m.userid = d.userid
    inner join membership.user_notes n 
        on n.userid = d.userid
        and n.CREATED_ON =
            (
            select min(created_on) 
            from membership.user_notes 
            where userid = n.userid
                and note = 'received.'
            )  
where approved = 1
group by
    case
        when (upper(m.email) like '%max.com') then 'MAX'
        when (upper(m.email) like '%tax.com') then 'TAX'
        else 'OTHER'
    end     
with rollup
于 2015-08-07T22:00:33.220 回答