列别名引用

时间:2012-10-20 12:53:48

标签: sql oracle ora-00904

此查询将数字分解为数千,数百,五十等等受尊重的地方。问题是我无法通过其别名来引用该列。在Oracle中,我收到此错误:

  

ora-00904:“twos”:标识符无效

但代码在MS Access中运行良好

查询:

SELECT 
    BT, 
    CNO, 
    AMT,  
    TRUNC(AMT/1000) AS THS, 
    TRUNC((AMT-(THS*1000))/500) AS FIVHUN, 
    TRUNC((AMT-((THS*1000)+(FIVHUN*500)))/100) AS HUND, 
    TRUNC((AMT-(((THS*1000)+(FIVHUN*500))+(HUND*100)))/50) AS FIF, 
    TRUNC((AMT-(((THS*1000)+(FIVHUN*500))+(HUND*100)+(FIF*50)))/20) AS TWENTY, 
    TRUNC((AMT-(((THS*1000)+(FIVHUN*500))+(HUND*100)+(FIF*50)+(TWENTY*20)))/10) AS TENS, 
    TRUNC((AMT-(((THS*1000)+(FIVHUN*500))+(HUND*100)+(FIF*50)+(TWENTY*20)+(TENS*10)))/5) AS FIVES, 
    TRUNC((AMT-(((THS*1000)+(FIVHUN*500))+(HUND*100)+(FIF*50)+(TWENTY*20)+(TENS*10)+(FIVES*5)))/2) AS TWOS, 
    TRUNC((AMT-(((THS*1000)+(FIVHUN*500))+(HUND*100)+(FIF*50)+(TWENTY*20)+(TENS*10)+(FIVES*5)+(TWOS*2)))/1) AS ONES 
FROM 
    EMPLOYER;

1 个答案:

答案 0 :(得分:5)

你只能引用外部选择中的列别名,所以除非你重新计算每个列的所有先前值,否则你需要嵌套每个级别,这有点难看:

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;

t公用表表达式(CTE)只是将实际数据与生成数字1-9的虚拟表交叉连接,并为每个数据分配票据面额值(假设Robert Co是正确的)水平供以后使用。

r CTE是递归的,我认为只能从11gR2开始。联盟的第一部分建立了到目前为止账单加起来的“运行总额”,这是零,因为这是递归的第一步。除了用于递归连接的x的虚拟零值之外,不使用其余列。联合的第二部分从此级别的amt中减去前一级别的运行总计,找到该面额的整个账单数量 - 这是我们想要实际报告的 - 并重新计算运行总计到包括那个数字。每次循环时,账单的大小都会减少,而且运行总额会增加。

所以最终会有很多行,每个帐单的数量都是不同的行;实际上需要旋转以显示适当列下的值。这就是最后的max()group 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全部为0ones均为1。后者很容易解释,但更多的问题是为什么他们不应该都是1。如果fives值为1,则剩余的拆分金额将变为均匀,因此ones必须为0。同样,tens值未考虑fif。因此,这种简单查询无法处理的值之间存在依赖关系。您可以调整问题列以考虑到这一点,当然,存在引入细微错误的风险。