2011-12-14 83 views
0

我有一个结果集以下T-SQL拆分行?

Rank cardID  DespatchValue Spend SumSpend Spendtype 
0  468612  500    0   0   Despatch 
1  468612  500    -8500  -8500  Topup 
2  468612  500   -11500  -20000  Topup 
3  468612  500    -3500  -23500  Topup 

正如你可以看到每个花加到总支出(通过递归CTE),我很高兴的。

但是逻辑需要这样的工作

  • 迪斯派奇价值高达初始花费=花型平衡
  • 凡是=顶起来

所以后,

我会喜欢看:

Rank cardID DespatchValue Spend SumSpend Spendtype 
0  468612 500    0  0  Despatch 
?  468612 500    -500 -500  Balance 
1  468612 500    -8000 -8500  Topup 
2  468612 500    -11500 -20000  Topup 
3  468612 500    -3500 -23500  Topup 

有关如何实现此目的的任何想法?

+0

不多,我有点茫然如何去。在谷歌阅读一个视图“拆分行”搜索页面,但他们都看起来有点激烈,我需要什么。思考一个自我加入,但不知道从哪里开始 – 2011-12-14 10:02:30

回答

1

要做一个拆分行,你需要为你的CTE增加两个UNION子句。大多数递归CTE都有一个“锚点”和一个递归位。您需要扩展该递归位的工作方式。首先,您需要确定何时抑制“名义”情况,这对您而言是当余额从正值总量转换为负值时。在你的情况下,这是“DespatchValue + SumSpend”。

接下来的两个术语将是你的“拆分”行。使用上面任何条件的逆(即,只在这个“特殊”条件下才能使行恢复)。第一个联盟将成为“平衡”记录,将其降至0.第二个是余下的剩余部分。

下面是我想到的 - 注意,我添加了一个“subid”列来指示“平衡”(并防止在以后的递归中出现重复)。我还添加了一个“DespatchBalance”,假设你真的在检查什么。除了具体情况,一般格式应该是好的:

declare @txn as table (
    id int, 
    cardID int, 
    DespatchValue int, 
    Spend int 
    --SumSpend int, 
    --Spendtype varchar(50) 
) 

insert into @txn 
        select 0, 468612, 500, 0--, 0, 'Despatch' 
union all select 1, 468612, 500, -8500--, -8500, 'Despatch' 
union all select 2, 468612, 500, -11500--, -20000, 'Despatch' 
union all select 3, 468612, 500, -3500--, -23500, 'Despatch' 


;with x (id, subid, cardID, DespatchValue, Spend, SumSpend, DespatchBalance, Despatch) as 
(
    --Anchor - beginning record 
    select id, 0, cardID, DespatchValue, Spend 
    , Spend as SumSpend 
    , DespatchValue as DespatchBalance 
    , 'Despatch' as Despatch 
    from @txn 
    where id = 0 

    UNION ALL  
    -- primary CTE - identify all nominal 'Topup' records 
    select t.id, 0, t.cardID, t.DespatchValue, t.Spend 
    , x.SumSpend + t.Spend 
    , x.DespatchBalance + t.Spend 
    , 'Topup' 
    from @txn t 
    join x 
     on x.id + 1 = t.id 
     and x.subid = 0 
    where x.DespatchBalance <= 0 

    UNION ALL 

    -- These two UNIONs do a split record: 

    -- First half of split - the remaining amount to a balance of 0 
    select t.id, 1 -- special "subid" to indicate it's a split record 
    , t.cardID, t.DespatchValue 
    , - x.DespatchBalance 
    , x.SumSpend - x.DespatchValue -- only capture the remaing bit above balance 
    , 0 -- DespatchBalance should be 0 
    , 'Balanace' 
    from @txn t 
    join x 
     on x.id + 1 = t.id 
     and x.subid = 0 
    where x.DespatchBalance > 0 
     and x.DespatchBalance + t.Spend < 0 

    UNION ALL 
    -- Second half of split - record that this is an overflow after "Balance" reached 
    select t.id, 0 
    , t.cardID, t.DespatchValue 
    , t.Spend + x.DespatchBalance 
    , x.SumSpend + t.Spend 
    , x.DespatchBalance + t.Spend 
    , 'Topup' 
    from @txn t 
    join x 
     on x.id + 1 = t.id 
     and x.subid = 0 
    where x.DespatchBalance > 0 
     and x.DespatchBalance + t.Spend < 0 
) 

select * 
from x 
option (MAXRECURSION 100)