2

SQLの最後の部分で問題が発生しているようです。group by with roll up関数を実装すると、SQLNULLが表示されます。この集約されたSQL内でNULLがTOTALに置き換えられるように、NULLを変更するにはどうすればよいですか?

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を配置するので、それを置き換えたい場合は、既存のクエリをサブクエリに配置してから、onを使用しCASEて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ステートメントを次の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 に答える
0

この部分の代わりに

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

単に使用する

upper(REPLACE(COALESCE(m.email、'other')、'.com'、''))as Name

サブクエリを記述してさらに複雑にする必要はありません。

于 2018-09-26T04:55:41.737 に答える