CREATE PROCEDURE dbo.ProductPricePacketInfo @AFromDate DATETIME, @AEndDate DATETIME, @AProductNumber INT AS
SET NOCOUNT ON

SELECT dtRange.IndividualDate AS View_DateStamp, MAX(Id) AS View_Id, FK_ProductNumber AS View_FK_ProductNumber, PacketType AS View_PacketType
FROM ProductPricePacket WITH (NOLOCK) 
CROSS APPLY dbo.DateRange(ProductPricePacket.StartDate, ProductPricePacket.EndDate, ProductPricePacket.FK_PacketNumber) AS dtRange
WHERE dtRange.IndividualDate BETWEEN @AFromDate AND @AEndDate
AND (@AProductNumber = -2 OR ProductPricePacket.FK_ProductNumber = @AProductNumber)
GROUP BY dtRange.IndividualDate, FK_ProductNumber, PacketType