在Oracle SQL中透视列和应用格式掩码的不同方法

y3bcpkx1  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(96)

我使用SQL Developer和BIRT Viewer,在应用格式掩码'999G999G999G999G999G999G990'时出现此错误:

ORA-01722: μη αποδεκτός αριθμός
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

查询:

select
        *
    from

(   select  pc.description production_category,
                decode(pph.allocation,1,'GR-70B',2,'BG',3,'GR-70A',4,'GR-70Δ',6,'UK',8,'USA-KY',9,'USA-OR') alloc,   
          to_number(to_char(sum(ppd.pcs),'999G999G999G999G999G999G990')) pcs
        
from prd_production_hd pph,
            prd_production_details ppd,
            prod_categories pc
        where pph.id = ppd.production_id
        and ppd.prd_category_id in (14,15,16,17,18,20,21,51,52,56)
        and ppd.pcs is not null
        and ppd.prd_category_id = pc.id
        and pph.prod_date = :P_DATE
        group by pc.description,pph.allocation
        )

    pivot (
        sum(pcs)
        for alloc in ('GR-70B' GR_70Β, 'BG' BG, 'GR-70A' GR_70Α, 'GR-70Δ' GR_70Δ,'UK' UK,'USA-KY' USA_KY,'USA-OR' USA_OR)
    )`

是否有其他方法来应用格式掩码?

gk7wooem

gk7wooem1#

您似乎想要聚合并格式化数字:

SELECT production_category,
       TO_CHAR(GR_70Β,'999G999G999G999G999G999G990') AS GR_70B,
       TO_CHAR(BG,    '999G999G999G999G999G999G990') AS BG,
       TO_CHAR(GR_70Α,'999G999G999G999G999G999G990') AS GR_70Α,
       TO_CHAR(GR_70Δ,'999G999G999G999G999G999G990') AS GR_70Δ,
       TO_CHAR(UK,    '999G999G999G999G999G999G990') AS UK,
       TO_CHAR(USA_KY,'999G999G999G999G999G999G990') AS USA_KY,
       TO_CHAR(USA_OR,'999G999G999G999G999G999G990') AS USA_OR
FROM   (
  SELECT pc.description AS production_category,
         pph.allocation,
         ppd.pcs
  FROM   prd_production_hd pph
         INNER JOIN prd_production_details ppd
         ON pph.id = ppd.production_id
         INNER JOIN prod_categories pc
         ON ppd.prd_category_id = pc.id
  WHERE  ppd.prd_category_id in (14,15,16,17,18,20,21,51,52,56)
  AND    ppd.pcs is not null
  AND    pph.prod_date = :P_DATE
)
PIVOT (
  SUM(pcs)
  FOR allocation in (
    1 AS GR_70Β,
    2 AS BG,
    3 AS GR_70Α, 
    4 AS GR_70Δ,
    6 AS UK,
    8 AS USA_KY,
    9 AS USA_OR
  )
);
  • 注意:在PIVOT之前不需要DECODEallocation s;你可以在PIVOT中实现。你也不需要聚合两次;在PIVOT中重复一次。*
qybjjes1

qybjjes12#

在我看来,这是错误的:

to_number(to_char(sum(ppd.pcs),'999G999G999G999G999G999G990'))

由内而外:

  • sum(ppd.pcs)导致number数据类型值
  • 您将to_char应用于该数值并提供格式模型(这是可以的)
  • 但是,然后你应用to_number到它-为什么?你就是从那里开始的!

因此:

  • 删除to_number(to_char(并保留sum(ppd.pcs)
  • 结果是number,或者
  • 删除to_number(
  • 结果是一个 string

相关问题