需要在一个字段中计算值

时间:2018-10-03 12:11:37

标签: sql oracle

我的SQL查询是

SELECT *
  FROM fchost.iv_stock_details
 WHERE cod_stock = '16' AND cod_stock_branch = '1134'

它将返回多列结果,但我担心的是一个特定列,即库存状态,其值为UUUUIIII,总共会有200个数字

我需要计算I和U的数量

例如

  

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUIUUIUUUUUUUUUUUUUUIUUUIUUUUUUUUUUUUUUUUUUUUUUIIUUUIUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUIIUUUUUUUUUUUUUUUUUUUUUUUUUIUUUUUUUUUUUUUUUUUUUUUIUUUUUUUUUUUUUUUIUUUUUIIIIIIII

在库存状态列中,结果将是这样,我需要分别计算该值并显示

例如,如果“ U”为180,我想显示位置值(从1到200) 另外,如果“我”为20,则需要放置值并显示

1 个答案:

答案 0 :(得分:0)

您可以使用the regexp_count() function,它具有由您要查找的字符组成的非常简单的模式:

SELECT regexp_count(stock_status, 'U') as u_count,
       regexp_count(stock_status, 'I') as i_count
  FROM fchost.iv_stock_details
 WHERE cod_stock = '16' AND cod_stock_branch = '1134';

   U_COUNT    I_COUNT
---------- ----------
       180         20