我想要每个人的销售总额的百分比。我希望每个SalesPersonID有4条记录,每年有1条。我每个SalesPersonID都有16条记录。总计结果显示每年有四个不同的金额。当我单独运行查询时,它会得到正确的结果。
WITH gt as (
Select sum(subtotal + taxamt) as YearlyGrandTotal
, YEAR(OrderDate) as gtYear
From AdventureWorks2017.Sales.SalesOrderHeader
group by YEAR(OrderDate)
),
it as (
Select SalesPersonID, sum(subtotal + taxamt) as IndividualSales
, year(OrderDate) as SalesYear
From AdventureWorks2017.Sales.SalesOrderHeader
where SalesPersonID is not null
Group by SalesPersonID, year(OrderDate)
)
Select SalesPersonID,SalesYear, IndividualSales,YearlyGrandTotal
, 100*(IndividualSales/YearlyGrandTotal) as PercentageTotalRevenue
From gt,it
Group by SalesYear,SalesPersonID,IndividualSales,YearlyGrandTotal
order by SalesPersonID;