SQL参数WeekOfYear

时间:2015-08-20 09:31:39

标签: sql performance sql-server-2008 date parameters

我有这张表,你可以看到PreQuantity is null 这是因为我的Week of year参数只查看用户选择的周数,因此第一个prequantity每次都为null。
我不想要这个,我希望报告查看week before用户选择,以便他们可以看到Prequantity @WOY = week of year = 1 to 52

CREATE TABLE #salescore (
        [DivNo] NVARCHAR(max)
        ,[ProductCode] INT
        ,[Description] NVARCHAR(max)
        ,[CGNo] NVARCHAR(max)
        ,[SCGNo] NVARCHAR(max)
        ,Retail DECIMAL(38, 2)
        ,[WeekOfYear] TINYINT
        ,FirstDayOfWeek DATETIME
        ,[Quantity] INT
        ,[Sales] DECIMAL(38, 2)
        ,[NoOfStores] INT
        ,[USW] DECIMAL(38, 0)
        )

INSERT INTO #salescore
SELECT pos.DivNo
    ,pos.ProductCode
    ,[Description]
    ,[CGNo]
    ,[SCGNo]
    ,p.Retail
    ,w.WeekOfYear
    ,w.FirstDayOfWeek
    ,Sum(Quantity) [Quantity]
    ,sum(pos.Retail) [Retail2]
    ,w.NoOfStores
    ,(Sum(Quantity) / w.NoOfStores) USW
FROM .pos pos
LEFT JOIN #weeklystores w ON pos.PosDate = w.[DATETIME]
LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
    AND p.DivNo = pos.DivNo
    AND pos.DivNo = w.DivNo
WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
and w.Year >2013
    and w.Year IN (
        SELECT Item
        FROM dbo.ufnSplit(@YEAR, ',')
        )
    AND p.EmpNo IN (
        SELECT Item
        FROM dbo.ufnSplit(@BD, ',')
        )
    AND p.CGNo IN (
        SELECT Item
        FROM dbo.ufnSplit(@CGNo, ',')
        )
    AND p.SCGNo IN (
        SELECT Item
        FROM dbo.ufnSplit(@SCGNo, ',')
        )
    AND p.ProductClass IN (
        SELECT Item
        FROM dbo.ufnSplit(@ProductClass, ',')
        )
    AND p.ProductCode IN (
        SELECT Item
        FROM dbo.ufnSplit(@ProductCode, ',')
        ) 
    AND pos.DivNo IN (
        SELECT Item
        FROM dbo.ufnSplit(@Region, ',')
        )
    AND w.WeekOfYear IN (
        SELECT Item
        FROM dbo.ufnSplit(@WOY, ',')
        )
    AND w.WeekOfYear IS NOT NULL 
GROUP BY pos.DivNo
    ,pos.ProductCode
    ,w.WeekOfYear
    ,w.NoOfStores
    ,w.FirstDayOfWeek
    ,[Description]
    ,[CGNo]
    ,[SCGNo]
    ,p.Retail

enter image description here

0 个答案:

没有答案