ORA-01722错误消息:SUM DECODE SUBSTR

时间:2018-03-02 06:51:34

标签: sql oracle

我需要总共(tran_qty * PRICE)买入和卖出 这是我想要的结果:

|CONTRACT| TRAN_QTY|PRICE
BUY         50    1.2
BUY         20    2.0
SELL        10    1.0
SELL        40    2.5

但是当我运行下面的查询时,我在第1行遇到了这个错误。

  第1行的

错误   ORA-01722:无效的数字

我该怎么做?

SELECT sum(decode(substr(O.tran_no,1,1), 'B', 'BUY')) BUY,
       sum(decode(substr(O.tran_no,1,1),'S', 'SELL')) SELL,
       O.TRAN_QTY,
       O.PRICE,
       o.client_no,
       c.client_name,
       sum(o.tran_qty*o.price)total,
       o.branch_code,
       r.descr,
       O.CURRENCY,
       O.STOCK_NO,
       O.CSTAMP 
from bos_m_ledger o,bos_m_para r,bos_m_client c,bos_cltype g 
WHERE r.para_type='BRANCH' AND 
       R.PARA_CODE = O.BRANCH_CODE and 
       o.client_no=c.client_no and 
       g.nbrk_rate=1 and 
       c.client_type=g.client_type and 
       o.tdr_code=c.tdr_code and 
       o.tran_type= 'CON' AND 
       C.CLIENT_TYPE='6' 
group by substr(o.tran_no,1,1),
       o.client_no,
       o.branch_code,
       r.descr,
       c.client_name,
       O.CURRENCY,
       O.STOCK_NO,
       O.CSTAMP,
       O.TRAN_QTY,
       O.PRICE 
ORDER BY O.BRANCH_CODE, O.CLIENT_NO'

1 个答案:

答案 0 :(得分:0)

总结一串'购买'或者'出售'没有任何意义。我怀疑你正试图找出ORA-00979: not a GROUP BY expression错误。此外,当您尝试获取汇总总数时,在投影和分组依据中包含TRAN_QTYPRICE是没有意义的。

所以你需要做的就是这样:

SELECT decode(substr(O.tran_no,1,1), 'B', 'BUY' ,'S', 'SELL') as contract,
       o.client_no,
       c.client_name,
       sum(o.tran_qty*o.price) as total,
       o.branch_code,
       r.descr,
       O.CURRENCY,
       O.STOCK_NO,
       O.CSTAMP 
from bos_m_ledger o,bos_m_para r,bos_m_client c,bos_cltype g 
WHERE r.para_type='BRANCH' AND 
       R.PARA_CODE = O.BRANCH_CODE and 
       o.client_no=c.client_no and 
       g.nbrk_rate=1 and 
       c.client_type=g.client_type and 
       o.tdr_code=c.tdr_code and 
       o.tran_type= 'CON' AND 
       C.CLIENT_TYPE='6' 
group by decode(substr(O.tran_no,1,1), 'B', 'BUY' ,'S', 'SELL'),
       o.client_no,
       o.branch_code,
       r.descr,
       c.client_name,
       O.CURRENCY,
       O.STOCK_NO,
       O.CSTAMP
ORDER BY O.BRANCH_CODE, O.CLIENT_NO;