如何在SQL

时间:2015-11-17 18:46:25

标签: sql-server tsql

我试图找出过去4周没有销售的商店数量。由于网格在DevExpress中的结构方式,我需要将周显示为Wk1,Wk2,Wk3,Wk4作为列(这些列中的数据将是零销售商店的商店数)。

现在我的数据已连续销售财政周。我需要它看起来基本上就像:

Vendor        | Store  | City   | State | Category     | Wk1 | Wk2 | Wk3| Wk4
------------------------------------------------------------------------    
Prairie Farms | #16141 | Adrian | MI    | 2% Gallon    |  1  |  0  |  1  |  1
Prairie Farms | #16141 | Adrian | MI    | Whole Gallon |  1  |  1  |  0  |  0

而不是

Vendor        | Store  | City   | State | Category     | Sale Cal Wk | Sale Fiscal Wk
--------------------------------------------------------------------------    
Prairie Farms | #16141 | Adrian | MI    | 2% Gallon    | 2015-10-23  | 38
Prairie Farms | #16141 | Adrian | MI    | Whole Gallon | 2015-10-23  | 38

我已经包含了迄今为止有效的代码。我有一个计数(存储)功能作为' ZeroStore'在最终的select语句中,将商店指示为零销售商店。

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    IF 1=0 
    BEGIN
        SET FMTONLY OFF
    END

    DECLARE @4WeeksAgo date
    DECLARE @Today date     

    SET @4WeeksAgo = DATEADD(day,-28,getdate())
    SET @Today = cast(GETDATE() as date)

    DECLARE @maxweek as tinyint
    SET @maxweek = case when (SELECT DGFiscalWeek 
                        FROM scans.dbo.DimDate 
                        WHERE CAST(Actualdate as DATE)=dateadd(dd,0,CAST(getdate() as DATE))) = 1 THEN 52 ELSE
                            (SELECT DGFiscalWeek FROM scans.dbo.DimDate WHERE CAST(Actualdate as DATE)=dateadd(dd,0,CAST(getdate() as DATE)))-1                             END


    DECLARE @LYear as int
    SET @LYear =  cast ((select  max(FiscalYr) from Scans.dbo.Scans)-1 as varchar(6))

    DECLARE @Year as int
    SET @Year =  @LYear+1

    CREATE TABLE #Initialize    
    (
       [Master Vendor] varchar (100),
       [Dairy Vendor] varchar (100),
       Store float,
       City varchar (50),
       State varchar(5),
       District int,
       Category varchar(20),
       [Sale Cal Wk] date,
       SaleYear int,
       [Sale Fiscal Week] tinyint,
       Units int,
       Sales money,
       Grade int
    )

    INSERT INTO #Initialize
        SELECT DISTINCT
            d.[Master Vendor], d.[Dairy Vendor], d.[Store],
            City, State, District,
            '2% Gallon',
            CAST(MAX(ActualDate) AS DATE) AS [Sale Cal Wk],
            DGFiscalYear,
            DGFiscalWeek,
            Units = 0,
            Sales = 0,
            Grade = 0
    FROM
        Scans.dbo.DGStores s
    FULL JOIN 
        Scans.dbo.DimDate dd ON s.State <> 'XX'
    FULL JOIN 
        Tableau.dbo.DollarGeneralDairyDistributors d ON d.Store = s.Store
    WHERE
        ((DGFiscalYear = @LYear AND DGFiscalWeek >= @maxweek) OR  
         (DGFiscalYear = @Year AND DGFiscalWeek BETWEEN 1 AND @maxweek))    
        AND d.Store IN (SELECT Store 
                        FROM DollarGeneralDairyDistributors)    
    GROUP BY 
        d.[Master Vendor], d.[Dairy Vendor], District, d.Store, 
        DGFiscalYear, DGFiscalWeek, City, State


INSERT INTO #Initialize
SELECT DISTINCT 
    d.[Master Vendor] ,
    d.[Dairy Vendor] ,
    d.[Store],
    City,
    [State],    
    District,
    Category = 'Whole Gallon',
    cast(max(ActualDate) as DATE) as [Sale Cal Wk],
    DGFiscalYear,
    DGFiscalWeek,
    Units=0,
    Sales=0,
    Grade=0
FROM
    Scans.dbo.DGStores s
FULL JOIN 
    Scans.dbo.DimDate dd
ON
    s.State <> 'XX'
FULL JOIN 
    Tableau.dbo.DollarGeneralDairyDistributors d
ON
    d.Store = s.Store
WHERE
    ((DGFiscalYear = @LYear AND DGFiscalWeek >= @maxweek) OR (DGFiscalYear = @Year AND DGFiscalWeek BETWEEN 1 AND @maxweek))    
AND
    d.Store IN 
    (SELECT Store 
      FROM DollarGeneralDairyDistributors)
GROUP BY d.[Master Vendor], d.[Dairy Vendor] ,District, d.Store, DGFiscalYear,  DGFiscalWeek, City, State


CREATE TABLE #Update
(Store varchar(15),
 Category  varchar(25),
  FiscalYr int,
  FiscalWk tinyint,
  Units     int,
  Sales     money)


INSERT #Update
SELECT DISTINCT Store,  Category, FiscalYr, FiscalWk,
    isnull(sum(isnull(SumUnits,0)),0) as Units,
    isnull(sum(isnull(SumSales,0)),0) as Sales
FROM scans.dbo.Scans sc
JOIN [Scans].[dbo].DollarGeneralDairyCategory  c
ON sc.ItemSku = c.ItemSku
WHERE   
FiscalYr >= @LYear  
GROUP BY[Store], FiscalYr, FiscalWk, Category

UPDATE #Initialize
SET Units = u.Units,
    Sales = u.Sales,
    Grade=100
FROM #Update u 
WHERE #Initialize.[Sale Fiscal Week] = u.FiscalWk AND #Initialize.SaleYear = u.FiscalYr 
    AND #Initialize.Store=u.Store AND #Initialize.Category = u.Category


SELECT *
, COUNT(store) AS 'ZeroStore' 
FROM #Initialize 
GROUP BY [Sale Cal Wk], [Master Vendor], [Dairy Vendor], Store, City, State, District, Category, SaleYear, [Sale Fiscal Week], Units,                   Sales, Grade
HAVING SUM( Units) = 0 AND [Sale Cal Wk] BETWEEN @4WeeksAgo AND @Today


    drop table #Initialize,#Update
END

非常感谢您的任何意见或帮助。

0 个答案:

没有答案