Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Fungsi rollup - Ganti NULL

ROLLUP menempatkan null di baris total jadi jika Anda ingin menggantinya, saya sarankan untuk mengambil kueri yang ada dan menempatkannya di subkueri dan kemudian menggunakan CASE pada NAME untuk mengganti null ke Total .

Kode Anda akan mirip dengan ini:

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

Jika Anda tidak membungkus kueri Anda dalam subkueri, maka Anda dapat menggunakan sesuatu seperti ini:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara Memperbaiki "Permintaan TRANSAKSI KOMIT tidak memiliki TRANSAKSI AWAL yang sesuai" di SQL Server

  2. Bagaimana kueri SQL dapat memiliki dua dari klausa?

  3. SET versus SELECT saat menetapkan variabel?

  4. Fungsi String T-SQL:perbedaan antara menggunakan Kiri/Kanan dan Substring dan perilaku aneh

  5. Hubungkan R ke mesin database SQL Server