您只能在外部选择中引用列别名,因此除非您重新计算每列的所有先前值,否则您需要嵌套每个级别,这有点难看:
select bt, cno, amt, ths, fivhun, hund, fif, twenty, tens, fives, twos,
trunc((amt-(ths*1000)-(fivhun*500)-(hund*100)-(fif*50)-(twenty*20)
-(tens*10)-(fives*5)-(twos*2))/1) as ones
from (
select bt, cno, amt, ths, fivhun, hund, fif, twenty, tens, fives,
trunc((amt-(ths*1000)-(fivhun*500)-(hund*100)-(fif*50)-(twenty*20)
-(tens*10)-(fives*5))/2) as twos
from (
select bt, cno, amt, ths, fivhun, hund, fif, twenty, tens,
trunc((amt-(ths*1000)-(fivhun*500)-(hund*100)-(fif*50)-(twenty*20)
-(tens*10))/5) as fives
from (
select bt, cno, amt, ths, fivhun, hund, fif, twenty,
trunc((amt-(ths*1000)-(fivhun*500)-(hund*100)-(fif*50)
-(twenty*20))/10) as tens
from (
select bt, cno, amt, ths, fivhun, hund, fif,
trunc((amt-(ths*1000)-(fivhun*500)-(hund*100)
-(fif*50))/20) as twenty
from (
select bt, cno, amt, ths, fivhun, hund,
trunc((amt-(ths*1000)-(fivhun*500)
-(hund*100))/50) as fif
from (
select bt, cno, amt, ths, fivhun,
trunc((amt-(ths*1000)-(fivhun*500))/100) as hund
from (
select bt, cno, amt, ths,
trunc((amt-trunc(ths*1000))/500) as fivhun
from (
select bt, cno, amt,
trunc(amt/1000) as ths from employer
)
)
)
)
)
)
)
);
...这给出了类似的东西:
BT CNO AMT THS FIVHUN HUND FIF TWENTY TENS FIVES TWOS ONES
--- --- ---------------- ------- ------ ---- --- ------ ---- ----- ---- ----
1 2 123,456,789 123456 1 2 1 1 1 1 2 0
3 4 87,654,321 87654 0 3 0 1 0 0 0 1
5 6 1,234,567 1234 1 0 1 0 1 1 1 0
没有那么漂亮,而是一个递归版本,主要是为了我自己的娱乐:
with t as (
select bt, cno, amt, x,
case x when 1 then 1000 when 2 then 500 when 3 then 100
when 4 then 50 when 5 then 20 when 6 then 10 when 7 then 5
when 8 then 2 when 9 then 1 end as bill
from employer
cross join (select level as x from dual connect by level < 10)
),
r (bt, cno, amt, x, y, running) as (
select t.bt, t.cno, t.amt, 0 as x, 0 as y, 0 as running
from t
where t.x = 1 -- could be any x, just want one row per bt/cno
union all
select t.bt, t.cno, t.amt, t.x,
trunc((t.amt - r.running)/t.bill) as y,
r.running + (t.bill * trunc((t.amt - r.running)/t.bill)) as running
from t
join r on r.bt = t.bt and r.cno = t.cno and r.x = t.x - 1
)
select bt, cno, amt,
max(case when x = 1 then y else 0 end) as ths,
max(case when x = 2 then y else 0 end) as fivhun,
max(case when x = 3 then y else 0 end) as hund,
max(case when x = 4 then y else 0 end) as fif,
max(case when x = 5 then y else 0 end) as twenty,
max(case when x = 6 then y else 0 end) as tens,
max(case when x = 7 then y else 0 end) as fives,
max(case when x = 8 then y else 0 end) as twos,
max(case when x = 9 then y else 0 end) as ones
from r
group by bt, cno, amt
order by bt, cno;
公用表表达式 (CTE) 只是将t
真实数据与生成数字 1-9 的虚拟表进行交叉连接,并将票据面额值(假设 Robert Co 是正确的)分配给每个级别以供以后使用。
CTE 是递归的r
,我认为它只适用于 11gR2。联合的第一部分建立了迄今为止账单加起来的“总和”,它为零,因为这是递归的第一步。其余列不使用,除了x
用于递归连接的虚拟零值。联合的第二部分从本级别减去上一级别的累计amt
,找到该面额的整张钞票的数量 - 这是我们实际想要报告的 - 并重新计算累计以包含该数字。每次循环,账单的大小都会减少,而运行的总数会增加。
所以最终会有很多行,每张账单的数量都是不同的行;这实际上需要被旋转以显示适当列下的值。这就是最后的max()
andgroup by
位所做的。
对于我的虚拟数据,它给出了相同的结果:
BT CNO AMT THS FIVHUN HUND FIF TWENTY TENS FIVES TWOS ONES
--- --- ---------------- ------- ------ ---- --- ------ ---- ----- ---- ----
1 2 123,456,789 123456 1 2 1 1 1 1 2 0
3 4 87,654,321 87654 0 3 0 1 0 0 0 1
5 6 1,234,567 1234 1 0 1 0 1 1 1 0
顺便说一句,我最初试图用mod()
(正如 AndriyM 建议的那样)来简化它,但你不能独立计算每个值:
select bt, cno, amt,
floor( amt/1000) as ths,
floor(mod(amt, 1000)/ 500) as fivhun,
floor(mod(amt, 500)/ 100) as hund,
floor(mod(amt, 100)/ 50) as fif,
floor(mod(amt, 50)/ 20) as twenty,
floor(mod(amt, 20)/ 10) as tens,
floor(mod(amt, 10)/ 5) as fives,
floor(mod(amt, 5)/ 2) as twos,
floor(mod(amt, 2)/ 1) as ones
from employer
order by bt, cno;
BT CNO AMT THS FIVHUN HUND FIF TWENTY TENS FIVES TWOS ONES
--- --- ---------------- ------- ------ ---- --- ------ ---- ----- ---- ----
1 2 123,456,789 123456 1 2 1 1 0 1 2 1
3 4 87,654,321 87654 0 3 0 1 0 0 0 1
5 6 1,234,567 1234 1 0 1 0 0 1 1 1
大多数值是相同的,但tens
are all0
和 the ones
are all 1
。后者很容易解释,尽管更多的是为什么它们不应该都是1
. 如果fives
值是1
,那么要拆分的剩余量变为偶数,因此ones
必须是0
。同样,该tens
值未考虑fif
在内。因此,这种简单查询无法处理的值之间存在依赖关系。当然,您可以调整问题列以将其考虑在内,但有引入细微错误的风险。