码迷,mamicode.com
首页 > 其他好文 > 详细

MTD 门店统计

时间:2015-06-25 16:58:12      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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 

 

MTD 门店统计

标签:

原文地址:http://www.cnblogs.com/zjflove/p/4600236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!