查询不在 - 显示"所有其他"除特定值外

时间:2017-12-01 16:07:35

标签: sql-server-2008

您好我有两个表,一个是tab_data,另一个是tbl_catg

enter image description here

我想要像图像中的结果。 tbl_Catg中可用的内容应该显示为原样。不可用的内容应显示为"所有其他"

以下查询显示错误的结果。

select 
   Future_Product_Category,
   sum(TYDOLLAR) ty 
from [dbo].[tbl_DATA] a
inner join [tbl_CATG] b on 
ISNULL(a.Future_Division,'NULLDIV')=ISNULL(b.Division,'NULLDIV') 
and IS NULL(a.Future_Product_Category,'NULLCATG')=ISNULL(b.CatgName,'NULLCATG')
group by Future_Product_Category

union All

select 
   'All Other', 
   A.TYDOllar 
from
   (select  
       Future_Division, 
       Future_Product_Category,
       sum(TYDOLLAR) TYDOllar
    from [dbo].[tbl_DATA] 
    group by Future_Division, Future_Product_Category) a 
   ,[tbl_CATG] b 
where 
    a.Future_Division=b.Division 
    and (a.Future_Product_Category not in (select  CatgName from [tbl_CATG] where a.Future_Division=b.Division ))
order by a.Future_Product_Category

0 个答案:

没有答案