我遇到了一个挑战,我曾多次遇到过,但从未找到有效的解决方案。想象一下,我有一张大桌子,上面有关于银行帐户及其从借方到贷方的可能循环移动:
AccountId DebitCredit AsOfDate
--------- ----------- ----------
aaa d 2018-11-01
aaa d 2018-11-02
aaa c 2018-11-03
aaa c 2018-11-04
aaa c 2018-11-05
bbb d 2018-11-02
ccc c 2018-11-01
ccc d 2018-11-02
ccc d 2018-11-03
ccc c 2018-11-04
ccc d 2018-11-05
ccc c 2018-11-06
在上面的示例中,我想将子分区号分配给AccountId和DebitCredit的组合,其中,每次DebitCredit移动时,分区号都会递增。换句话说,在上面的示例中,我想要这个结果:
AccountId DebitCredit AsOfDate PartNo
--------- ----------- ---------- ------
aaa d 2018-11-01 1
aaa d 2018-11-02 1
aaa c 2018-11-03 2
aaa c 2018-11-04 2
aaa c 2018-11-05 2
bbb d 2018-11-02 1
ccc c 2018-11-01 1
ccc d 2018-11-02 2
ccc d 2018-11-03 2
ccc c 2018-11-04 3
ccc d 2018-11-05 4
ccc c 2018-11-06 5
我真的不知道如何快速有效地做到这一点。该操作必须每天在具有数百万行的表上完成。
在此示例中,保证我们所有帐户都具有连续的行。但是,客户当然可以在当月15日开立帐户,并/或在26日关闭帐户。
挑战是要在MSSQL 2016服务器上解决,但是可以在2012年(甚至是2008r2)上运行的解决方案也不错。
您可以想象,无法判断是否只有借方或贷方行,或者帐户是否每天都在循环。
答案 0 :(得分:6)
您可以通过递归cte来实现
; with
-- the purpose of `cte` is to generate running number in the order of AsOfDate
cte as
(
select AccountId, DebitCredit, AsOfDate, rn = row_number() over (partition by AccountId order by AsOfDate)
from tbl
),
-- this is the recursive CTE
rcte as
(
-- anchor member. Starts with `PartNo 1`
select AccountId, DebitCredit, AsOfDate, rn, PartNo = 1
from cte
where rn = 1
union all
-- recursive member. Incrememt `PartNo` if there is a change in debitcredit
select c.AccountId, c.DebitCredit, c.AsOfDate, c.rn,
PartNo = case when r.DebitCredit = c.DebitCredit then r.PartNo else r.PartNo + 1 end
from rcte r
inner join cte c on r.AccountId = c.AccountId
and r.rn = c.rn - 1
)
select *
from rcte
order by AccountId, AsOfDate
答案 1 :(得分:6)
如果您使用的是SQL Server 2012+,则可以使用lag()和窗口求和来获取此信息:
select *,sum(PartNoAdd) over (partition by AccountId order by AsOfDate asc) as PartNo_calc
from
(
select *,
case when DebitCredit=lag(DebitCredit,1) over (partition by AccountId order by AsOfDate asc) then 0 else 1 end as PartNoAdd
from t
)t2
order by AccountId asc, AsOfDate asc
在内部查询中,PartNoAdd
检查该帐户的先前借记卡是否相同。如果是,则返回0(我们不应添加任何内容),否则返回1。
然后,外部查询对该帐户的所有PartNoAdd
求和。
答案 2 :(得分:1)
您可以使用密集排名
select *,dense_rank() over(partition by AccountId order by DebitCredit desc) as PartNo
from t