0

我需要一些帮助。我想计算这些列的激活、停用和系统中的用户总数。当前输出包括 NAME 列中的名称两次输出,如下所示,其中包括 NULLS。我想消除 NULL 并获得每列的小计。希望我能得到一些帮助。

  Current output:
- NAME   Activated  Deactivated 
- MAX     25          NULL
- MAX     NULL         5
- TAX     40          NULL
- TAX     NULL         10

    Desired output:
  - NAME   Activated  Deactivated 
   - MAX     25          5  
  - TAX     40          10
   -Total    65          15

谢谢。

select 
    case 
        when (upper(m.email) like '%max.com') then 'MAX'
        when (upper(m.email) like '%tax.com') then 'TAX'
        else 'OTHER'
        end Name, count(*) 'Activated', null '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.'
        )
        and substring(convert(varchar(8),n.created_on,112),1,6) = '201209'
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      

union 

select 
    case 
        when (upper(m.email) like '%max.com') then 'MAX'
        when (upper(m.email) like '%tax.com') then 'TAX'
        else 'OTHER'
        end Name,  
    null 'Activated', 
    count(*) '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 = 'SAAR received.'
        )
        and substring(convert(varchar(8),m.LastLockoutDate,112),1,6) = '201209'
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       
4

2 回答 2

0

将和分别更改为null 'Deactivated'和,然后执行null 'Activated'0'Deactivated'0'Activated'GROUP BY Name WITH ROLLUP

于 2013-03-13T15:53:41.480 回答
0

您应该能够将其合并为一个SELECT

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 = 'SAAR 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     

这个新版本使用带有CASE表达式的聚合函数来获取总数ActivatedDeactivatedCASE 检查您使用每个查询的日期。

然后,如果要包括总行,可以使用GROUP BY with ROLLUP

于 2013-03-13T15:55:00.130 回答