SQL over 子句 - 将分区划分为编号的子分区
SQL over clause - dividing partition into numbered sub-partitions
我有一个挑战,我在多个场合遇到过,但一直未能找到有效的解决方案。想象一下,我有一个很大的 table,其中包含有关例如银行账户及其从借方到贷方的可能循环移动:
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
我真的不知道如何快速有效地做到这一点。该操作必须每天在具有数百万行的 table 上完成。
在此示例中,我们保证所有帐户都有连续的行。但是,客户当然可能会在当月的 15 日开立账户 and/or 在 26 日关闭账户。
挑战将在 MSSQL 2016 服务器上解决,但适用于 2012(甚至可能是 2008r2)的解决方案会很好。
正如您想象的那样,无法判断是只有借方行还是贷方行,或者账户是否每天都在循环。
你可以使用密集排名
select *,dense_rank() over(partition by AccountId order by DebitCredit desc) as PartNo
from t
你可以用递归 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
如果你有 sql 服务器 2012+,你可以使用 lag() 和 window 求和来得到这个:
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
检查此帐户以前的借记卡是否相同。如果是,则为 returns 0(我们不应该添加任何内容),否则为 returns 1.
然后外部查询求和该帐户的所有 PartNoAdd
。
我有一个挑战,我在多个场合遇到过,但一直未能找到有效的解决方案。想象一下,我有一个很大的 table,其中包含有关例如银行账户及其从借方到贷方的可能循环移动:
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
我真的不知道如何快速有效地做到这一点。该操作必须每天在具有数百万行的 table 上完成。
在此示例中,我们保证所有帐户都有连续的行。但是,客户当然可能会在当月的 15 日开立账户 and/or 在 26 日关闭账户。
挑战将在 MSSQL 2016 服务器上解决,但适用于 2012(甚至可能是 2008r2)的解决方案会很好。
正如您想象的那样,无法判断是只有借方行还是贷方行,或者账户是否每天都在循环。
你可以使用密集排名
select *,dense_rank() over(partition by AccountId order by DebitCredit desc) as PartNo
from t
你可以用递归 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
如果你有 sql 服务器 2012+,你可以使用 lag() 和 window 求和来得到这个:
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
检查此帐户以前的借记卡是否相同。如果是,则为 returns 0(我们不应该添加任何内容),否则为 returns 1.
然后外部查询求和该帐户的所有 PartNoAdd
。