需要将去年的结果纳入库存使用情况报告

时间:2019-02-06 22:11:03

标签: tsql inventory sapb1

我有一个库存使用情况报告,其中列出了上一年的交易。现在,我获得了本年度的结果。我可以使用什么正确的日期语法?

以下是我今年的当前工作,我正在使用今年的get date函数,并需要它来计算相同的信息,但要按月细分去年的信息。

SELECT OITM.ItemCode, OITM.ItemName AS 'Description', OITM.CardCode AS 
'Vendor',
SUM(OITW.OnHand) AS 'On Hand', SUM(OITW.OnOrder) AS 'On Order', 
SUM(OITW.IsCommited) AS 'Committed',

(SUM(OITW.OnHand)+SUM(OITW.OnOrder)-SUM(OITW.IsCommited)) AS 'Available', 
OITM.AvgPrice AS 'Unit Cost',(SUM(OITW.OnHand)*OITM.AvgPrice) AS 'Value $',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
YEAR(OINM.DocDate)=((year, -1, GETDATE()) AND OINM.ItemCode = OITM.ItemCode 
GROUP BY

OINM.ItemCode) AS 'Prev. Year',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='1' AND YEAR(OINM.DocDate)=(year, -1, GETDATE()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'JAN',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='2' AND YEAR(OINM.DocDate)=(year, -1, GETDATE()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'FEB',  
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='3' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'MAR',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='4' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'APR',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='5' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'MAY',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='6' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'JUN',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='7' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'JUL',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='8' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'AUG',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='9' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'SEP',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='10' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'OCT',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='11' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'NOV',
(SELECT SUM(OINM.OutQty) FROM OINM WHERE OINM.TransType <> '67' AND 
MONTH(OINM.DocDate)='12' AND YEAR(OINM.DocDate)=YEAR(getdate()) AND 
OINM.ItemCode =

OITM.ItemCode GROUP BY OINM.ItemCode) AS 'DEC'
FROM OITM, OITW
WHERE OITM.ItemCode=OITW.ItemCode

1 个答案:

答案 0 :(得分:0)

我的建议是缩短原​​始查询的时间。如果我没有完全错,可以这样写:

library(igraph)
dd<-read.csv(file.choose(), header=FALSE)
gg<-graph.data.frame(dd, directed=FALSE)
plot(gg)

其工作原理如下:

WITH OINM_Base (ItemCode, [Month], OutQty) AS ( SELECT ItemCode, CASE GROUPING(MONTH(DocDate)) WHEN 1 THEN 0 ELSE MONTH(DocDate) END, SUM(OutQty) FROM OINM WHERE TransType <> '67' AND YEAR(DocDate) = YEAR(GETDATE()) -- change this for the previous year GROUP BY ItemCode, ROLLUP(MONTH(DocDate)) ), OINM_Data (ItemCode, [Year], JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, [DEC]) AS ( SELECT ItemCode, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] FROM OINM_Base PIVOT (SUM(OutQty) FOR [Month] IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) pvt ), OITW_Data (ItemCode, [On Hand], [On Order], [Committed]) AS ( SELECT ItemCode, SUM(OnHand), SUM(OnOrder), SUM(IsCommited) FROM OITW GROUP BY ItemCode ) SELECT tm.ItemCode, tm.ItemName AS [Description], tm.CardCode AS Vendor, tw.[On Hand], tw.[On Order], tw.[Committed], tw.[On Hand] + tw.[On Order] - tw.[Committed] AS Available, tm.AvgPrice AS [Unit Cost], tw.[On Hand] * tm.AvgPrice AS [Value $], nm.[Year], nm.JAN, nm.FEB, nm.MAR, nm.APR, nm.MAY, nm.JUN, nm.JUL, nm.AUG, nm.SEP, nm.OCT, nm.NOV, nm.[DEC] FROM OITM tm INNER JOIN OITW_Data tw ON tm.ItemCode = tw.ItemCode LEFT OUTER JOIN OINM_Data nm ON tm.ItemCode = nm.ItemCode; 中的数据仅限于相关年份,每个月将OINM值求和,并为该年份创建一个总计,标记为第0号月份(CTE OutQty )。使用OINM_Base查询,将出现在不同行上的总和转换为列(CTE PIVOT)。 OITW数据的汇总也要适当地完成(CTE OINM_Data)。

OITW_DataOINM_Data最终都与通过OITW_Data构建主要行的数据结合在一起。在这里,完成了最终计算(OITMAvailable)。

现在,要更改查询以返回上一年,只需更改一件事(请参阅我的注释“ 上一年对此进行更改”),即:

Value $必须替换为YEAR(GETDATE())