2016-08-30 1 views
0
 Stocktaking Table 
StoreID  Date ProductCode  Qty 
    1  2016-07-30 11    58 
    1  2016-09-30 11    97 
    2  2016-08-30 12    15 
    2  2016-09-22 55    10 
    3  2016-09-05 55    10 


    Sale Table 
    StoreID Date ProductCode Qty 
    1  2016-08-30 11  40 
    2  2016-08-30 12  3 
    2  2016-08-30 55  4 
    3  2016-08-30 55  6 

Desired Output 


    StoreID Date Productcode Qty 
    1  2016-08-30 11  18 
    2  2016-08-30 12  12 
    2  2016-08-30 55  6 
    3  2016-08-30 55  4 

パラメータ毎日棚卸在庫が負の値は、次の棚卸を選択し、それから

@ date1 and @date2 @storeid 

を引く表示されている場合、私は毎日の在庫を表示する必要があります。私は私がここに負の値を示すことができないことができ、この

StoreID Date Productcode Qty 
    1 2016-08-30  11  18 
    2 2016-08-30  12  12 
    2 2016-08-30  55  -4 
    3 2016-08-30  55  -6 

ようになったstocktakingQty -saleQty = Todayinventory

を表示することができます。 ここで問題は8th monthstore ID 3,2の間に在庫を取るのを忘れていたが、product Code 552016-08-30に売った。来月は在庫が9th monthの間にproduct Code 55になっています。 私はそれを行う方法がわかりません!bcoz唯一の負のqtyの値は、私は翌月の在庫を取った日付を選択する必要がある場合、または他の私は8日の株式を取った日付を選択している。

多分誰かがそれを手伝うことができました!

Declare 
      @date1 date = '2015-06-01' 
       ,@date2 date = '2015-06-30' 
       ,@StoreNo Nvarchar(Max)=' AND S.StoreNo IN (61,68,450,451,430,917,918,919,921,923,925,930,935)' 


--AS 
BEGIN 
      IF Object_ID(N'tempdb..#calender') IS NOT NULL DROP TABLE #calender 
       IF Object_ID(N'tempdb..##Temp') IS NOT NULL DROP TABLE ##Temp 
       IF Object_ID(N'tempdb..##Inv1') IS NOT NULL DROP TABLE ##Inv1 
       IF Object_ID(N'tempdb..##Inv2') IS NOT NULL DROP TABLE ##Inv2 
        IF Object_ID(N'tempdb..#Stock') IS NOT NULL DROP TABLE #Stock 
        IF Object_ID(N'tempdb..##StoreList') IS NOT NULL DROP TABLE ##StoreList 
        IF Object_ID(N'tempdb..##final') IS NOT NULL DROP TABLE ##final 
        IF Object_ID(N'tempdb..##product') IS NOT NULL DROP TABLE ##product 

    Declare @sql_store nvarchar(max) 
      SET @sql_store = 
          'SELECT StoreNo,StoreName INTO ##StoreList FROM Store S WHERE S.StoreNo IN (61,68,450,451,430,917,918,919,921,923,925,930,935)' + @StoreNo 
      EXECUTE sp_executesql @sql_store 


      Declare 

       @date_s char(10) = cast(@date1 as varchar) 
       ,@date_e char(10) = cast(@date2 as varchar) 
       ,@date_index date 


        create Table #calender (Date date) 
        SET @date_index = @date1 

        WHILE @date_index<[email protected] 
        BEGIN 
        INSERT INTO #calender 
        SELECT @date_index 

        SET @date_index = dateadd(day,1,@date_index) 

        IF @date_index>@date2 
        Break 
        ELSE 
        Continue 
        END 


     BEGIN 
       SELECT 
          ProductNo as ProductBarCode,Date 

         INTO ##product 
            FROM Product,#calender 

        WHERE StartUseDate <= @date_s AND EndUseDate >[email protected]_e and IsInUsed = 1 

     END 

create table #inventory (StoreNo int ,Date date, ProductBarCode varchar(14),ProductQty int) 



     BEGIn 

        Select 
             STD.StoreNo As StoreNo 
             ,CheckDate as Date 
             ,ProductBarCode as ProductBarCode 
             , SUM(StocktakingQty)AS ProductQty 
          INTO ##Temp 
            From StockTakingDetail STD 
       Inner Join 
          (Select  
             StoreNo 
             ,CheckNo 
             ,CheckDate 
            From StockTakingMain SM)StocktakingMain 
           On STD.CheckNo =StockTakingMain.CheckNo 
           where STD.StoreNo IN (select StoreNo from ##StoreList) 
          group By STD.StoreNo,STD.ProductBarCode,CheckDate 




       SELECT A.StoreNo as StoreNo ,C.[date] as Date,A.ProductBarCode as ProductBarCode,A.ProductQty as ProductQty 

          INTO ##inv1 
           FROM #calender C 
           OUTER APPLY 
           (
            SELECT TOP 100 percent * FROM ##Temp I WHERE I.Date < C.DATE and StoreNo IN (select StoreNo from ##StoreList) ORDER BY I.Date 
           ) A 
           OPTION (maxrecursion 0) 



      INSERT INTO #inventory 
         Select S.StoreNo,s.Date,s.ProductBarCode,ISNULL (sum(s.ProductQty),0) as ProductQty 

         From 
         (Select StoreNo,Date,ProductBarCode,ProductQty from ##inv1 where Date between @date_s and @date_e and StoreNo IN (select StoreNo from ##StoreList) 


         Union all 
     --Buy 
          Select 
            BuyStore as StoreNo 
            ,BuyDate as Date 
            ,ProductBarCode as ProductBarCode 
            ,SUM(BuyQty)as Qty 

              from BuyDetail BD 
         Inner Join 
           (Select 
            BuySerialNo 
            ,BuyDate 
             from BuyMain BM) BuyMain ON BD.BuySerialNo = BuyMain.BuySerialNo 
          where 
            BuyDate Between @date_s and @date_e and BuyStore IN (select StoreNo from ##StoreList) 
          Group BY 
            BuyDate,BuyStore,ProductBarCode,BuyDate 
               )S 
         GROUP BY s.StoreNo,s.Date,s.ProductBarCode 

      END 




     BEGIN 

      select 
       S.Date as Date,S.StoreNo As StoreNo,(S.ProductBarCode) as ProductBarCode,sum(S.Qty) as ProductQty 
     INTO #Stock 
      from 
       --- sale 
       (
        select 
           StoreNo As StoreNo, 
           PluCode as ProductBarCode, 
           cast (sum(BuyPoint/100)as int)*(-1) as Qty, 
           Date as Date 
        from POS_ItemTran p 
        where 
          Date between @date_s and @date_e and p.StoreNo IN(select StoreNo from ##StoreList) 
        Group by 
          PluCode,StoreNo,Date 

       union all 

       --Transfer IN 
         select 
           TD.TargetStoreNo as StoreNo 
           ,TD.ProductBarCode as ProductBarCode 
           ,SUM(TD.AcceptQty) as Qty 
           ,AcceptDate as Date 
              from TransferDetail TD 
       Inner join 
          (Select TransferSerialNo 
          ,AcceptDate 
            from TransferMain)TM 

         ON TM.TransferserialNo = TD.TransferserialNo 
          where 
            AcceptDate between @date_s and @date_e and TargetStoreNo IN (select StoreNo from ##StoreList) 
           Group by 
            TargetStoreNo,TD.ProductBarcode,AcceptDate 

       union all 

       -- Transfer out 
          Select 
           TD.TransferStoreNo as StoreNo 
           , TD.ProductBarCode as ProductBarCode 
           ,SUM(TD.TransferQty)*(-1) As Qty 
           ,TransferDate as Date 
              from TransferDetail TD 
       Inner Join 
          (Select TransferSerialNo 
             ,TransferDate 
              from TransferMain)TMO 
           On TMO.TransferSerialNo = TD.TransferSerialNo 
         where 
           TransferDate between @date_s and @date_e and TransferStoreNo IN (select StoreNo from ##StoreList) 
         Group by 
           TD.TransferStoreNo,TD.ProductBarCode,TransferDate 

       --Loss 
       UNION ALL 

        SELECT 
          StoreNo 
          ,LossProductBarCode 
          ,SUM(LossQty)*(-1) as Qty, 
          LossDate as Date 

           FROM LossDetail LD 
       INNER JOIN 
        (SELECT 
         StoreNo 
         ,LossSerialNo 
         ,LossDate 
          FROM LossMain LM)LossMain ON LD.LossSerialNo = LossMain.LossSerialNo 
         where 
          LossDate between @date_s and @date_e and StoreNo IN (select StoreNo from ##StoreList) 
        GROUP BY 
          StoreNo,LossProductBarCode,LossDate 

     Union All 
        --RCVBackDetail 
         Select 
          RcvBackStoreNo as StoreNo 
          ,ProductBarCode as ProductBarCode 
          ,SUM(RcvBackQty) as Qty 
          ,RcvBackDate as Date 
         from RcvBackDetail RBD 
        Inner Join 
         (SELECT RcvBackSerialNo 
           ,RcvBackDate 
            FROM RcvBackMain)RcvBackMain 
         ON RcvBackMain.RcvBackSerialNo = RBD.RcvBackSerialNo 
         Where 
          RcvBackDate Between @date_s and @date_e and RcvBackStoreNo IN (select StoreNo from ##StoreList) 
         Group By 
           RcvBackDate,RcvBackStoreNo,ProductBarCode 


       )S 
         Group by S.StoreNo,S.ProductBarCode,S.Date 

     END 

     BEGIN 

     Select 
       f.StoreNo,f.Date,f.ProductBarCode,SUM(f.ProductQty) as ProductQty 
     INTO ##Inv2 
     From 
     (Select StoreNo,Date,ProductBarCode,ProductQty From #inventory i 
    union all 
     Select StoreNo,Date,ProductBarCode,ProductQty From #Stock st)f 
     Group By f.Date,f.ProductBarCode,f.StoreNo 
     End 


    BEGIN 
    DECLARE @cols AS NVARCHAR(MAX), 
@query AS NVARCHAR(MAX) 

SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME([StoreNo]) 
        FROM ##StoreList 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

    SELECT @query = 
'SELECT * FROM 
(SELECT  
    [StoreNo], 
     p.Date as Date 
    ,p.ProductBarCode 
    ,(ProductQty) as productQty 
FROM ##inv2 f 
Right Join ##product P ON p.ProductBarCode =f.ProductBarCode and p.Date =f.Date 
where 
p.ProductBarCode IN(2300028813032,2221400000393,2300011512096,2300021714060) 
)X 
PIVOT 
( 
    SUM(ProductQty) 
    for [StoreNo] in (' + @cols + ') 
) P' 

EXEC SP_EXECUTESQL @query 
END 
Drop Table #inventory 
--Drop Table #calender 
IF Object_ID(N'tempdb..#inventory') IS NOT NULL DROP TABLE #inventory 
IF Object_ID(N'tempdb..#calender') IS NOT NULL DROP TABLE #calender 
IF Object_ID(N'tempdb..##Temp') IS NOT NULL DROP TABLE ##Temp 
IF Object_ID(N'tempdb..##Inv1') IS NOT NULL DROP TABLE ##Inv1 
       IF Object_ID(N'tempdb..##Inv2') IS NOT NULL DROP TABLE ##Inv2 
        IF Object_ID(N'tempdb..#Stock') IS NOT NULL DROP TABLE #Stock 
        IF Object_ID(N'tempdb..##StoreList') IS NOT NULL DROP TABLE ##StoreList 
        IF Object_ID(N'tempdb..##final') IS NOT NULL DROP TABLE ##final 
        IF Object_ID(N'tempdb..##product') IS NOT NULL DROP TABLE ##product 

END 
+0

あなたがこれまでに試したものを投稿してください。 – StackUser

+0

@StackUserは私のコードを掲載しましたので、試してみました – mad

答えて

0
SELECT A.StoreNo as StoreNo ,C.[date] as Date,A.ProductBarCode as ProductBarCode,A.ProductQty as ProductQty 
            INTO ##inv1 

            FROM #calender C 
            OUTER APPLY 
            (
             SELECT DISTINCT TOP 100 percent * FROM ##Temp I WHERE I.Date <= C.DATE and StoreNo IN (select StoreNo from ##StoreList) ORDER BY I.Date 
            ) A 
            --where A.productBarcode =2300007115072 
            OPTION (maxrecursion 0) 

        SELECT B.StoreNo as StoreNo ,C.[date] as Date,B.ProductBarCode as ProductBarCode,B.ProductQty as ProductQty 
           INTO ##inv2  

            FROM #calender C 
            OUTER APPLY 
            (
             SELECT DISTINCT TOP 100 percent * FROM ##Temp I WHERE I.Date >= C.DATE and StoreNo IN (select StoreNo from ##StoreList) ORDER BY I.Date 
            ) B 
            --where A.productBarcode =2300007115072 
            OPTION (maxrecursion 0) 
BEGIN 
          Select 
             i.StoreNo 
             ,i.Date 
              ,i.ProductBarCode 
              ,ISNULL(i.ProductQty - (Select SUM(Productqty) from #Stock st where st.Date<=i.Date and i.StoreNo =st.StoreNo and i.ProductBarCode= st.ProductBarCode),i.ProductQty) as ProductQty 
          INTO ##final1 
             From #inv1 i 
             left join #Stock s 
              on s.storeno=i.storeno 
               and s.productbarcode=i.productbarcode 
               and s.date=i.date 
               --where i.ProductBarCode IN(2221400000393) 
               Order By StoreNo,Date ,productBarcode 
           Select 
             i.StoreNo 
             ,i.Date 
              ,i.ProductBarCode 
              ,ISNULL(i.Qty - (Select SUM(Productqty) from #Stock st where st.Date<=i.Date and i.StoreNo =st.StoreNo and i.ProductBarCode= st.ProductBarCode),i.Qty) as ProductQty 
          INTO ##final2 
             From #inv2 i 
             left join #Stock s 
              on s.storeno=i.storeno 
               and s.productbarcode=i.productbarcode 
               and s.date=i.date 
               --where i.ProductBarCode IN(2221400000393) 
               Order By StoreNo,Date ,productBarcode 

     END 



     BEGIN 

      Select f1.StoreNo as StoreNo,f1.Date as Date,f1.ProductBarCode as ProductBarCode, 
        case when f1.ProductQty <0 then f2.ProductQty else f1.ProductQty end as ProductQty 
      INTO ##final3 
      From ##final1 f1 
      Full Outer Join ##final2 f2 
      ON f2.Date = f1.Date and f2.ProductBarCode = f1.ProductBarCode and f1.StoreNo = f2.StoreNo 
     order By Date 
0

このソリューションは、30/08の時点で在庫と注文を処理し、salesqtyを計算して残りの在庫を2段階で計算します。注 - 入力データがあなたと全く同じではありません - 私は、クエリが完全にあなたの問題を解決するだろう以下の場合は、しかし、ケースと日付要素の機能があるべき

declare @st table(StoreID int , Dte date, ProductCode int,  Qty int) 
insert into @st values 
( 1,  '2016-07-30', 11,    58), 
( 1,  '2016-09-30', 11,    97), 
( 2,  '2016-08-30', 12,    15), 
( 2,  '2016-08-22', 55,    2), 
( 2,  '2016-09-22', 55,    10), 
( 3,  '2016-09-05', 55,    10) 

declare @s table (StoreID int, Dte date, ProductCode int, Qty int) 
insert into @s values 
( 1,  '2016-08-30', 11,  40), 
( 2,  '2016-08-30', 12,  15), 
( 2,  '2016-08-30', 55,  4), 
( 3,  '2016-08-30', 55,  6) 


drop table #temp 
;with cte as 
(
select t.storeid,t.stockstatusdate,t.productcode,sum(t.instock) instock ,sum(t.onorder) onorder 
from 
(
select st.storeid 
     , year(st.dte) * 12 + month(st.dte) yyyymm 
     , 2016 * 12 + 8 as stockstatusdate 
     , st.productcode, 
     isnull(case when year(st.dte) * 12 + month(st.dte) <= 2016 * 12 + 8 then sum(st.qty) end ,0)as instock, 
     isnull(case when year(st.dte) * 12 + month(st.dte) > 2016 * 12 + 8 then sum(st.qty) end ,0) as onorder 
from @st st 
group by st.storeid 
     , year(st.dte) * 12 + month(st.dte) 
     ,st.productcode 
) t 
group by t.storeid,t.stockstatusdate,t.productcode 
) 
select cte.storeid,cte.stockstatusdate,cte.productcode,cte.instock,cte.instock as instockcalc,cte.onorder, cte.onorder as onordercalc, 
     t.saleqty,t.saleqtycalc 
into #temp 
from cte 
join  
(select s.storeid,year(s.dte) * 12 + month(s.dte) as salesdate,s.productcode ,sum(s.qty) as saleqty, sum(s.qty) as saleqtycalc 
from @s s 
group by s.storeid,year(s.dte) * 12 + month(s.dte),s.productcode 
) t on t.storeid = cte.storeid and t.salesdate = cte.stockstatusdate and t.productcode = cte.productcode 

--select * from #temp 

update #temp 
    set instockcalc = 
     case 
      when instockcalc >= saleqtycalc then instockcalc - saleqtycalc 
      else 0 
     end, 
     saleqtycalc = 
     case 
      when instockcalc >= saleqtycalc then 0 
      else saleqtycalc - instockcalc 
     end 
where saleqtycalc > 0 

update #temp 
    set onordercalc = 
     case 
      when onordercalc >= saleqtycalc then onordercalc - saleqtycalc 
      else 0 
     end, 
     saleqtycalc = 
     case 
      when onordercalc >= saleqtycalc then 0 
      else saleqtycalc - onordercalc 
     end 
where saleqtycalc > 0 

--select * from #temp 

select storeid,stockstatusdate,productcode, 
     case 
      when instockcalc <> 0 then instockcalc 
      else onordercalc 
     end as qty 
from #temp 
+0

ありがとうございました。 – mad

0

わからない、シナリオのカップルをテストなるように、これはあります役に立った

我々は、クエリの下に、前月の30日と今月の前月と30日の30日間を取っ在庫数量が元のために望ましい結果に

を与えるだろうと考えることができと仮定:在庫服用は9月22日に行われた場合、またはその店のその特定の製品の5番目、それは実際には8月30日に起こったはずだと仮定することができます。

SELECT st.StoreID, 
CASE 
WHEN st.date < (CAST(CAST(DATEPART(yyyy,st.date) AS VARCHAR(5)) + '-' + CAST(DATEPART(mm, st.date) AS VARCHAR(5)) + '-30' AS DATE)) THEN 
CAST(CAST(DATEPART(yyyy,st.date) AS VARCHAR(5)) + '-' + CAST((DATEPART(mm, st.date)-1) AS VARCHAR(5)) + '-30' AS DATE) 
WHEN st.date = (CAST(CAST(DATEPART(yyyy,st.date) AS VARCHAR(5)) + '-' + CAST(DATEPART(mm, st.date) AS VARCHAR(5)) + '-30' AS DATE)) THEN st.date END AS [stockdate], 
st.ProductCode, 
s.date AS salesDate, 
st.Qty - s.Qty AS InventoryQuantity 
FROM dbo.stocktaking st 
LEFT JOIN dbo.SALE s 
ON st.StoreID =s.StoreID 
AND st.ProductCode = s.ProductCode 
WHERE CASE 
WHEN st.date < (CAST(CAST(DATEPART(yyyy,st.date) AS VARCHAR(5)) + '-' + CAST(DATEPART(mm, st.date) AS VARCHAR(5)) + '-30' AS DATE)) THEN 
CAST(CAST(DATEPART(yyyy,st.date) AS VARCHAR(5)) + '-' + CAST((DATEPART(mm, st.date)-1) AS VARCHAR(5)) + '-30' AS DATE) 
WHEN st.date = (CAST(CAST(DATEPART(yyyy,st.date) AS VARCHAR(5)) + '-' + CAST(DATEPART(mm, st.date) AS VARCHAR(5)) + '-30' AS DATE)) THEN st.date END <= s.date 
関連する問題