0

助けが必要です。これらの列について、システム内のアクティブ化されたユーザー、非アクティブ化されたユーザー、および合計ユーザーの数を取得したいと考えています。現在の出力には、以下に示すように NAME 列内の名前が 2 回含まれており、NULL が含まれています。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

これを1つに統合できるはずです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式を含む集計関数を使用して、合計Activatedとを取得しDeactivatedます。チェックは、CASE 各クエリを使用していた日付をチェックします。

次に、合計行を含める場合は、次を使用できます。GROUP BY with ROLLUP

于 2013-03-13T15:55:00.130 に答える
0

null 'Deactivated'null 'Activated'0'Deactivated'と に0'Activated'それぞれ変更してから、GROUP BY Name WITH ROLLUP

于 2013-03-13T15:53:41.480 に答える