将繁重的asp代码转换为在sql中运行

将繁重的asp代码转换为在sql中运行,sql,optimization,asp-classic,Sql,Optimization,Asp Classic,一点asp代码,在上下文中不会;根据产品上线的起始年到活跃年数,确定产品销售业绩的时间线,例如,2000年发布的产品在几年内都会有销售高峰,但到2004年就没有了……但代码这是系统中所有产品的代码,并按年份进行分组……如果您有问题,我可以回答,或者我认为查看代码会对它的功能有一个公平的了解 首先,它运行此查询以获取我要更改的数据: SELECT Products.ProductID, Products.AnticipatedSalesPattern, Convert(Char(10),I

一点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;

如果您可以在SQL中完成这一切,那么肯定会减少服务器上的负载。您熟悉视图和存储过程吗?是的,我知道,也是的,我对它们都很熟悉,但我喜欢将结果输出到网页上。。。所以使用视图就可以了