改为解码或

时间:2018-05-17 00:47:28

标签: sql oracle

我需要将此代码更改为解码或任何解决方案,以确保SELL(-ve值)和BUY(+ ve值)的结果。它应该显示在1行中,所以在我的oracle报告中,该列将与BUY |并列SELL。

sum(case when (o.tran_qty * o.price) < 0 
then (o.tran_qty * o.price) else 0    end) as sell_t,
sum(case when (o.tran_qty * o.price) > 0 
then (o.tran_qty * o.price) else 0 end) as buy_t,

现在在报告生成器中我得到了这个结果。 enter image description here

所以我需要合并客户端而不是只变成一行。 完整的代码,以获得上面的图片的结果。

SELECT 
COUNT(decode(substr(o.tran_no,1,1), 'B', 'BUY')) Buy,
COUNT(decode(substr(o.tran_no,1,1),'S', 'SELL')) sell,
nvl('D.EXCH_CODE','01') AS CODE,
sum(case when (o.tran_qty * o.price) < 0 then
   (o.tran_qty * o.price) else   0 end) as sell_t,
sum(case when (o.tran_qty * o.price) > 0 then 
   (o.tran_qty * o.price) else 0 end) as buy_t,
o.client_no,c.client_name,
sum(o.tran_qty*o.price)total,o.branch_code,r.descr,
C.TDR_CODE
from bos_m_ledger o,bos_m_para r,bos_m_client c,
bos_cltype g,BOS_M_STOCK   D
WHERE
r.para_type='BRANCH'
AND R.PARA_CODE = O.BRANCH_CODE
and o.client_no=c.client_no
AND O.STOCK_NO =D.STOCK_NO
AND D.CSTAMP_EXPIRY_DATE IS NOT NULL
AND C.CLIENT_NO='3JF0391'
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 TO_DATE(TO_CHAR(O.TRAN_DATE,'MON'),'MON')=TO_DATE(:P_MONTH,'MON')
AND TO_DATE(TO_CHAR(O.TRAN_DATE,'YYYY'),'YYYY') = TO_DATE(:P_YEAR,'YYYY')
group by  
substr(o.tran_no,1,1),o.client_no,o.branch_code,r.descr,c.client_name,
C.TDR_CODE
ORDER BY O.BRANCH_CODE, C.TDR_CODE,O.CLIENT_NO

1 个答案:

答案 0 :(得分:0)

将以下内容与decode

一起使用
select ClientNo "Client No", 
       ClientName "Client Name", 
       sum(decode(contract_type,'Sell',-tran_qty,tran_qty)) "No_Of_Contract", 
       sum(decode(contract_type,'Sell',-tran_qty * price,tran_qty * price)) "Total" 
  from Sales
 group by ClientNo, ClientName
 order by 1;