任务是选择每年利润最低的子类别。下一个查询,每年选择几个子类别:
select
min (Profit),
CalendarYear,
EnglishProductSubcategoryName
from (
select
SUM(fis.SalesAmount-fis.TotalProductCost) Profit,
t.CalendarYear,
sc.EnglishProductSubCategoryName
from FactInternetSales fis
inner join DimProduct p
on fis.ProductKey = p.ProductKey
inner join DimProductSubcategory sc
on p.ProductSubcategoryKey = sc.ProductSubcategoryKey
inner join DimTime t
on fis.DueDateKey = t.TimeKey
group by CalendarYear, EnglishProductSubcategoryName) aa
--Order by CalendarYear
) aa
group by CalendarYear, EnglishProductSubcategoryName
order by CalendarYear