我需要一些幫助。我希望對我的系統中用於這些列的用戶進行計數,並對其進行激活,停用和計數。當前輸出包含NAME列中兩次輸出的名稱,如下所示,其中包含NULLS。我想消除NULL,並獲得每列的小計。希望我能得到一些幫助。SQL CASE和JOINS:如何獲得不同的計數和總計?
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
哪些表結構? – Melanie 2013-03-13 15:53:08
這個sql腳本從各種表中獲取信息。確切地說3張桌子。桌上用於獲取案件的電子郵件,下一個用於查看用戶是否曾經被批准過,而另一個用於用戶第一次進入系統(已授權)。 – user1880670 2013-03-13 15:57:49