我似乎遇到了SQL的最终部分问题。 在我的SQL NULL中使用roll up函数实现组时出现。如何修改NULL以便在此聚合SQL中将其替换为TOTAL?
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
答案 0 :(得分:9)
ROLLUP
在总计行中放置了null
,因此如果您想要替换它,我建议您使用现有查询并将其放在子查询中,然后使用CASE
NAME
将null
替换为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 :(得分:4)
直接使用此名称参考:
case when grouping(Name) then 'Total' else Name end as Name
如果只使用一个级别的分组,则对null的测试就足够了;但是,分组(...)测试允许为每个不同的分组级别生成不同的描述。
答案 2 :(得分: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
答案 3 :(得分:0)
代替这部分
案例 当(上方(m.email)如'%max.com')然后'MAX' 当(上方(m.email)如'%tax.com')然后是'TAX' 其他“ OTHER” 姓
只需使用
upper(REPLACE(COALESCE(m.email,'other'),'.com',''))作为名称
无需编写子查询并将其进一步复杂化。