每个月的FIFO余额用sql结束

时间:2015-01-19 06:07:00

标签: sql sql-server

我试图在每个月末获得余额数量,即仅在每个月结束时使用FIFO运行总计,但它没有显示任何结果。请帮忙 这是我的询问。

declare @Stock table (Item char(3) not null,Date date not null,TxnType varchar(3) not null,Qty int not null,Price decimal(10,2) null)
insert into @Stock(Item ,  [Date] ,        TxnType, Qty,  Price) values
('ABC','20120401','IN',    200, 750.00),
('ABC','20120402','OUT',   100 ,null  ),
('ABC','20120403','IN',     50, 700.00),
('ABC','20120404','IN',     75, 800.00),
('ABC','20120405','OUT',   175, null  ),
('XYZ','20120406','IN',    150, 350.00),
('XYZ','20120407','OUT',   120 ,null  ),
('XYZ','20120408','OUT',    10 ,null  ),
('XYZ','20120409','IN',     90, 340.00),
('ABC','20120510','IN',    200, 750.00),
('ABC','20120511','OUT',   100 ,null  ),
('ABC','20120512','IN',     50, 700.00),
('ABC','20120513','IN',     75, 800.00),
('ABC','20120514','OUT',   175, null  ),
('XYZ','20120515','IN',    150, 350.00),
('XYZ','20120516','OUT',   120 ,null  ),
('XYZ','20120517','OUT',    10 ,null  ),
('XYZ','20120518','IN',     90, 340.00);

;WITH OrderedIn as (
    select *,ROW_NUMBER() OVER (PARTITION BY month(date) ORDER BY DATE) as rn
    from @Stock
    where TxnType = 'IN'
), RunningTotals as (
    select Item,Qty,Price,Qty as Total,0 as PrevTotal,rn from OrderedIn where rn = 1
    union all
    select rt.Item,oi.Qty,oi.Price,rt.Total + oi.Qty,rt.Total,oi.rn
    from
        RunningTotals rt
            inner join
        OrderedIn oi
            on
                rt.Item = oi.Item and
                rt.rn = oi.rn - 1
), TotalOut as (
    select Item,SUM(Qty) as Qty from @Stock where TxnType='OUT' group by Item
)
select
    rt.Item,SUM(CASE WHEN PrevTotal > out.Qty THEN rt.Qty ELSE rt.Total - out.Qty END * Price)
from
    RunningTotals rt
        inner join
    TotalOut out
        on
            rt.Item = out.Item
where
    rt.Total > out.Qty
group by rt.Item

输出

Month     Item   (No column name(qty*price))
4      ABC        40000
4      XYZ        37600
5      ABC        77500
5      XYZ        76100

2 个答案:

答案 0 :(得分:0)

随着你的样本数据。你正在寻找这个,否则清楚地描绘你想要的输出。

;WITH CTE AS
(
select Item ,[Date] ,TxnType ,s.Qty
,Price ,ROW_NUMBER() OVER (PARTITION BY month(date),TxnType ORDER BY DATE) as rn
from @Stock S 
)
,
 CTE1 AS
(
SELECT ITEM ,[DATE] ,TXNTYPE ,QTY,MONTH(DATE) MONTHDT,RN FROM CTE WHERE RN=1
UNION ALL
SELECT S.ITEM ,S.[DATE] ,S.TXNTYPE ,S.QTY+A.QTY,A.MONTHDT ,S.RN
FROM CTE S INNER JOIN  CTE1 A 
ON S.TXNTYPE=A.TXNTYPE AND MONTH(S.[DATE])=A.MONTHDT AND S.RN-A.RN=1


)
,CTE2 AS
(
SELECT  *
,ROW_NUMBER() OVER (PARTITION BY month(date),TxnType  ORDER BY QTY DESC )RN1 
FROM CTE1 S 
)

SELECT A.DATE,A.QTY-(SELECT B.Qty from CTE2 B 
where a.MONTHDT=b.MONTHDT and b.RN1=1 AND b.TxnType='OUT')
from CTE2 A
WHERE A.RN1=1 AND a.TxnType='IN'

答案 1 :(得分:0)

试试这个。

;WITH cte
     AS (SELECT Row_number()OVER(partition BY item, Datepart(mm, date) ORDER BY date DESC) rn,
                item,
                Month(date)             AS [month],
                TxnType,
                Price,
                date
         FROM   #Stock
         WHERE  TxnType = 'IN'),
     cte1
     AS (SELECT Row_number() OVER(partition BY item, Datepart(mm, date) ORDER BY date DESC)       rn,
                item,
                Month(date)                   [month],
                (SELECT Sum(CASE
                              WHEN TxnType = 'out' THEN -1 * qty
                              ELSE qty
                            END) AS qty
                 FROM   #Stock b
                 WHERE  a.item = b.item
                        AND a.Date >= b.Date) qty
         FROM   #Stock a)
SELECT a.[month],
       a.Item,
       ( a.Price * b.qty ) running_tot
FROM   cte a
       JOIN cte1 b
         ON a.Item = b.Item
            AND a.[month] = b.[month]
WHERE  a.rn = 1
       AND b.rn = 1