Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.30 KB | None | 0 0
  1. DECLARE @Company nvarchar(4)
  2. DECLARE @Partition bigint
  3.  
  4. SELECT @Partition  = Par.RECID FROM dbo.PARTITIONS AS Par WHERE Par.PARTITIONKEY = 'initial'
  5. SET @Company='USMF'
  6.  
  7. select INVENTTABLE.itemid ,
  8.                                ECORESPRODUCTTRANSLATION.NAME,
  9.                                CAST( isnull( BOHI.BegOnHand,0) AS DECIMAL(18,2))BegOnHand ,  
  10.                                CAST( isnull(P.QtyPurchased,0)AS DECIMAL(18,2)) QtyPurchased ,
  11.                                CAST( isnull(s.QtySold,0)AS DECIMAL(18,2)) QtySold ,
  12.                                CAST( isnull( EOHI.EndOnHand,0)AS DECIMAL(18,2)) EndOnHand
  13. from INVENTTABLE                
  14.                --Beginning  on hand in 2016
  15.                left outer join (                                  
  16.                                select itemid,  isnull(SUM(qty),0)BegOnHand
  17.                                from INVENTTRANS
  18.                                where  DATEPHYSICAL < '2016-1-1' and DATAAREAID = @Company and PARTITION = @Partition AND STATUSISSUE <= 3 and STATUSRECEIPT <= 3
  19.                                group by ITEMID )BOHI
  20.                                                on bohi.ITEMID = INVENTTABLE.ITEMID
  21.                --SOLD
  22.                left outer join (                  
  23.                                select ITEMID,  isnull(SUM(QTY),0)*-1 QtySold
  24.                                from INVENTTRANS
  25.                                where DATEPHYSICAL >= '2016-1-1' AND  DATEPHYSICAL <  '2017-1-1' and QTY  < 0 and DATAAREAID = @Company and PARTITION = @Partition AND STATUSISSUE <= 3 and STATUSRECEIPT <= 3
  26.                                GROUP BY ITEMID )S
  27.                                                on s.ITEMID = INVENTTABLE.ITEMID
  28.                --PURCHASED
  29.                left outer join (                  
  30.                                select ITEMID,  isnull(SUM(QTY),0) QtyPurchased
  31.                                from INVENTTRANS
  32.                                where  DATEPHYSICAL >= '2016-1-1' AND  DATEPHYSICAL <  '2017-1-1' and QTY  > 0 and DATAAREAID = @Company and PARTITION = @Partition AND STATUSISSUE <= 3 and STATUSRECEIPT <= 3
  33.                                GROUP BY ITEMID )P
  34.                                                ON P.ITEMID = INVENTTABLE.ITEMID
  35. --Ending on hand in 2016
  36.                left outer join (
  37.                                select itemid, ISNULL( SUM(qty),0)EndOnHand
  38.                                from INVENTTRANS
  39.                                where  DATEPHYSICAL < '2017-1-1' and STATUSISSUE <> 7 and DATAAREAID = @Company and PARTITION = @Partition AND STATUSISSUE <= 3 and STATUSRECEIPT <= 3
  40.                                group by ITEMID)EOHI
  41.                                                                ON EOHI.ITEMID = INVENTTABLE.ITEMID
  42.                join ECORESPRODUCT
  43.                                on inventtable.PRODUCT = ecoresproduct.RECID and inventtable.PARTITION = ecoresproduct.PARTITION
  44.                join ECORESPRODUCTTRANSLATION
  45.                                on ECORESPRODUCTTRANSLATION.PRODUCT = ecoresproduct.RECID and ECORESPRODUCTTRANSLATION.PARTITION = ecoresproduct.PARTITION
  46. where ( BOHI.BegOnHand <> 0) or ( P.QtyPurchased <> 0) or( s.QtySold <> 0) or (EOHI.EndOnHand <> 0 ) and DATAAREAID = @Company and INVENTTABLE.PARTITION = @Partition
  47. Order by inventtable.ITEMID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement