一些在上下文中的asp代码;产品销售业绩的时间表基于该产品上线的起始年份到活跃年数,例如,2000 年发布的产品将在几年内达到最高销售量,但到 2004 年将没有......但是编码适用于系统中的所有产品并按年份分组...(如果您有问题我可以回答,或者我认为查看代码可以很好地了解它在做什么!!)
首先它运行这个查询来获取数据(我想改变):
SELECT Products.ProductID, Products.AnticipatedSalesPattern, Convert(Char(10),Invoices.Date,103) As [date], Orders.Cost
FROM (Orders INNER JOIN Products ON Orders.ProductID = Products.ProductID)
INNER JOIN Invoices ON Orders.Invoice = Invoices.InvoiceNum
WHERE (Products.IsResource=1 AND Orders.Returned<>1 AND Orders.Cost<>0)
ORDER BY Products.ProductID, Invoices.Date;
这是它处理数据的方式(在asp中),我知道它不是有效的,只是将它作为样本建模数据......
while not dbrecords.eof
RecordsCount = RecordsCount + 1
if dbrecords("ProductID") <> LastPID then
PIDsCount = PIDsCount + 1
LastPID = dbrecords("ProductID")
FirstSaleDate = dbrecords("Date")
if month(FirstSaleDate) < 9 then
FirstSchoolYear = Year(FirstSaleDate) - 1
else
FirstSchoolYear = Year(FirstSaleDate)
end if
end if
YearsSinceFirstSale = int(DateDiff("d",FirstSaleDate,dbrecords("Date"))/365)
MyArray(FirstSchoolYear-2000,YearsSinceFirstSale) = MyArray(FirstSchoolYear-2000,YearsSinceFirstSale) + dbrecords("Cost")
MyArrayTotals(FirstSchoolYear-2000) = MyArrayTotals(FirstSchoolYear-2000) + dbrecords("Cost")
TotalSales = TotalSales + dbrecords("Cost")
dbrecords.movenext
wend
现在我喜欢的是删除将数据放入数组和查询以返回年度数据的整个过程
我目前坚持的是编写 sql 来度过自首次销售以来的岁月并跟踪每种产品(如何在 sql 中实现),我相信参数是通过 CTE 语句接受的......
此外,如果您认为这可以通过其他方式更有效地实现,那也很棒
任何帮助将不胜感激...
到目前为止,我得到了;
这不提供由 asp 生成的相同数据......如果有更好的方法来做到这一点,请告诉我......
DROP VIEW inline_view;
GO
CREATE VIEW inline_view AS
SELECT p2.ProductID, p2.AnticipatedSalesPattern, Invoices.Date, Orders.Cost, case when MONTH(date) < 9 then YEAR(date)-1 else YEAR(date) end as year,
(select top 1 case when MONTH(i.date) < 9 then YEAR(i.date)-1 else YEAR(i.date) end from Invoices i inner join Orders o on i.InvoiceNum=o.Invoice
inner join Products p on o.ProductID = p.ProductID where p2.ProductID = p.productID order by i.Date asc) as startsale
FROM (Orders INNER JOIN Products p2 ON Orders.ProductID = p2.ProductID)
INNER JOIN Invoices ON Orders.Invoice = Invoices.InvoiceNum
WHERE (p2.IsResource=1 AND Orders.Returned<>1 AND Orders.Cost<>0)
;
GO
SELECT * FROM (
select SUM(cost) sum, datediff(y, startsale, year) as year, startsale from inline_view
group by year, startsale
) as data
PIVOT
(
sum(sum)
--years after product is online
FOR year IN ([1], [2], [3],[4],[5],[6],[7],[8],[9], [10],[11], [12])
) as pvt;