SQLPLUS中令人讨厌的聚合

时间:2014-11-11 12:18:43

标签: oracle group-by sqlplus aggregation

SQLPLUS中的讨厌聚合

大家好, 我有下一张桌子:

   col.1   col.2
   2       1-V
   4       2-VV
   6       3-EC
   8       2-GD
   A       5-V
   C       2-EC
   E       2-GD
   10      6-V
   12      2-V
   14      1-GD
   14      1-V

我需要的是在桌子上进行一些聚合并得到类似下一个表格的东西(仅使用在&#39之后的值将col.2行分组 - '像[V,VV, EC,GD]):

   Type    count
   EC      2
   GD      3
   V       5
   VV      1

我不知道如何使用" GROUP BY"执行此任务的子句。

BR Hosen的

2 个答案:

答案 0 :(得分:0)

您问题的一个可能解决方案是:

select regexp_replace(col_2, '^\d+-', null), count(1)
from the_next_table
group by regexp_replace(col_2, '^\d+-', null);

享受。

PS:当然,此解决方案很大程度上取决于您的col.2数据始终为<some number><the dash symbol><anything>形式的假设。对于这种情况,此解决方案剥离<some number><the dash symbol>部分,仅留下<anything>聚合。

答案 1 :(得分:0)

尝试:

SELECT 
  substr( "col2", instr( "col2", '-' )+1) as "type",
  count(*) as "count"
from table1
GROUP BY substr( "col2", instr( "col2", '-' )+1);

演示:http://sqlfiddle.com/#!4/70db1/3