您好我有两个表,一个是tab_data,另一个是tbl_catg
我想要像图像中的结果。 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