Sure!Base view, let's call it Invoices, it looks like this:LicenseNo Organisation InvoiceNo TypeOfCost Company DateOfPurchase Cost DescriptionABC 123 Org1 2342 Fuel Chevron 4/12/04 12.34 RegularABC 123 Org1 2342 Fuel Chevron 6/12/04 23.45 RegularABC 123 Org1 53422 Fuel Exxon 7/12/04 34.23 Regular
The database contains 2700 vehicles so it's quite a lot of data. I want to create a stored proc that returns the sum of each vehicle during a specific period of time. The sum must be grouped on Month and Company.A procedure could look like this:SELECT DATEPART(month, DateofPurchase) AS 'Month', Company, SUM(Cost) AS 'MonthCost'FROM Invoices WHERE DateOfPurchase >= @date_from AND DateOfPurchase <= @date_toAND TypeOfCost = 'Fuel'AND LicenseNo = @license_noGROUP BY DATEPART(month, DateofPurchase), Company
The problem for me using this procedure is performance. I have a report and it might contain 80 vehicles and it just take too long time to use this procudure 80 times. What I'm trying to do is make a second view based on the first on that already has calculated the Sum, month etc. My problem is that I need to enter the criteria for DateOfPurchase and that ruins the group by clause.