SQL PnL库存查询

时间:2011-04-14 14:59:27

标签: sql inventory-management

我有一张包含交易清单的表格:

Security ;   Quantity ; Price ;  Consid
 1. IBM  ;    +1,000 ;   20  ;  -20k
 2. IBM  ;    +2,000 ;   22  ;  -44k
 3. IBM  ;    -1,000 ;   30  ;  +30k
 4. IBM  ;    -2,000 ;   20  ;  +40k
 5. IBM  ;    -2,000 ;   20  ;  -20k

所以PnL基本上是Consid列的总和所以在添加Trade#5之前,PnL将是+ 6k。

在交易#5之后,这显示PnL为-14k,这并不能反映我们的立场。

我想要的是过滤掉未结算交易的某种方式?因此,当我们将2k股IBM股票加入到表中时,只允许交易#5进入总和。

我对此的尝试是:

set @Ret = @Ret + isnull((SELECT SUM(GC) AS GS
FROM  (SELECT SUM(GrossConsid) * - 1 AS GC
                     FROM   Trades AS CT
                     WHERE  (SpecialCond = 'Prop') AND (SettType <> 'Futures') AND (TrdDt <= @Date) AND (TrdDt >=@StartDate) AND (Name = 'my_Comp')
                     GROUP BY ABS(Quantity)
                     HAVING (SUM(Quantity) = 0)) AS dt),0)

但是我没有想到有一个边缘条件,如果我有一个数量为+ 5,+ 5,-5的交易它不会被计算,因为(SUM(Quantity) = 0))评估为false。 / p>

关于如何纠正这个问题的任何想法?

谢谢Chris

3 个答案:

答案 0 :(得分:0)

您可以让您的应用在其他“已关闭”列中设置标记,而不是使用SQL来匹配您的广告资源吗?然后你可以这样做:

SELECT Security, SUM(Consid)
FROM mytable
WHERE Closed = 1
GROUP BY Security

答案 1 :(得分:0)

不能说出你的逻辑,只是解决你所说的错误:

HAVING (case when SUM(Quantity) = 0 then 1 else 0 end)

答案 2 :(得分:0)

Runnable Example

DECLARE @tbl AS TABLE (Seq int, Security varchar(3), Quantity int, Price int, Consid int) ;
INSERT INTO @tbl VALUES
(1, 'IBM', 1000, 20, -20000)
,(2, 'IBM', 2000, 22, -44000)
,(3, 'IBM', -1000, 30, 30000)
,(4, 'IBM', -2000, 20, 40000)
,(5, 'IBM', -2000, 20, -20000);

WITH RunningInventory AS (
SELECT l.Seq, SUM(r.Quantity) AS Inv
FROM @tbl AS l
LEFT JOIN @tbl r
ON r.Seq <= l.Seq
GROUP BY l.Seq
)
SELECT *
FROM @tbl AS trx
INNER JOIN RunningInventory
    ON trx.Seq = RunningInventory.Seq
WHERE RunningInventory.Inv >= 0 ;