MTD 门店统计

DROP TABLE #MTD
DECLARE @startDate date = '20150601'
       ,@endDate date = cast(getdate() as date)
CREATE TABLE #MTD(bydate date)
DECLARE @count int = DATEDIFF(day,@startDate,@endDate) 
WHILE @COUNT >= 0
    BEGIN
        INSERT INTO #MTD(bydate)
        SELECT DATEADD(day,@count,@startDate)
        SET @COUNT = @count - 1;
    END
-------------------------------------------  
DROP TABLE #Shop
CREATE TABLE #Shop(ShopCode nvarchar(50))
INSERT INTO #Shop(ShopCode)
SELECT '00000287'
UNION ALL
SELECT '00003845'
UNION ALL
SELECT '00002415' 
------------------------------------------
SELECT a.bydate 日期
      ,ISNULL(a.ShopCode,'')门店代码
      ,SUM(CASE b.ProductGiftId
             WHEN 60 THEN-b.Quantity
             ELSE 0
           END)趣味轨道游戏套装
      ,SUM(CASE b.ProductGiftId
             WHEN 61 THEN-b.Quantity
             ELSE 0
           END)飞利浦净水壶
      ,SUM(CASE b.ProductGiftId
             WHEN 62 THEN-b.Quantity
             ELSE 0
           END)游戏垫三件套
      ,SUM(CASE b.ProductGiftId
             WHEN 63 THEN-b.Quantity
             ELSE 0
           END)儿童玩具钢琴
      ,SUM(CASE b.ProductGiftId
             WHEN 64 THEN-b.Quantity
             ELSE 0
           END)宝宝时尚滑步车
  FROM
       (SELECT *
          FROM #MTD,#Shop)a LEFT JOIN XXX b ON a.bydate = CAST(b.CreateTime AS date)
                                                            AND TransactionType = 2
                                                            AND CampaignId = 9 
                                                                --and b.ShopCode='00000287' 
                                                            AND a.shopcode = b.shopcode
  GROUP BY a.bydate
          ,a.ShopCode
  ORDER BY a.bydate 

 

posted @ 2015-06-25 16:18  哈哈2222  阅读(321)  评论(0编辑  收藏  举报