sql >> データベース >  >> RDS >> Sqlserver

ロールアップ機能 - NULL を置換

    ROLLUP null を配置します 合計行にあるので、それを置き換えたい場合は、既存のクエリを取得してサブクエリに配置し、 CASE を使用することをお勧めします NAMEnull を置き換える Totalまで .

    コードは次のようになります:

    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
      

    1. 最初の4000文字のみをOracleNCLOBからNVARCHAR2(4000)値にコピーする方法は?

    2. MySQL.DateTimeを0000-00-0000:00:00の値でSystem.DateTimeに変換できません

    3. 複数の分割とorder_idの割り当て

    4. MySQLは一致する単語を数えます