这是我的查询
Select SalesPerson.SalesPersonName, TimeTable.month,
sum(SalesPerson_sales) SUM_OF_SALES
From SalesPerson
Join SalesRecordTable on SalesRecordTable.SalesPerson_id=SalesPerson.SalesPerson_id
Join TimeTable on TimeTable.time_id = SalesRecordTable.time_id
Group by SalesPersonName , TimeTable.month with rollup
order by SalesPersonName
正在制作
SalesPersonName month TotalSales
NULL NULL 565238.13
Andy 1 15966.75
Andy 2 10723.32
Andy 3 17174.77
Andy 4 9911.38
Andy 5 12380.12
Andy 6 12194.56
Andy 7 18964.82
(more records not pasted here, it basically goes upto month 12, then the records for other sales people follow)
我实际上要做的是得到这样的东西
SalesPersonName month TotalSales
Andy 3 71762
Andy 6 34486
Andy 9 31738
Andy 12 21532
John 3 23323
John 6 32873
基本上,我想使用SQL OLAP功能对每个销售人员每3个月的销售额求和,并像上面那样显示。
我正在尝试使用Range或Rows子句,但我在这里遗漏了一些东西:(如下所示
Select SalesPerson.SalesPersonName, TimeTable.month,
sum(SalesPerson_sales) SUM_OF_SALES OVER ( order by month rows 2 preceding)
From SalesPerson
Join SalesRecordTable on SalesRecordTable.SalesPerson_id=SalesPerson.SalesPerson_id
Join TimeTable on TimeTable.time_id = SalesRecordTable.time_id
Group by SalesPersonName , TimeTable.month with rollup
order by SalesPersonName
但是这给了我
SalesPersonName month (No column name)
Andy 1 1
Andy 1 2.06
Andy 1 3.14
Andy 1 3.26
, thousands of records follow :(
如何修复上面的查询以获得所需的结果?我想我需要以某种方式修复前面部分的第2行,它将根据需要起作用。
非常感谢您的意见
答案 0 :(得分:0)
您可以使用窗口功能,但我认为您可以只使用GROUP BY CEIL(month / 3)
。