我有一个名为data_column的列,它有一些像“123123,12,123123”的值。我想计算按第二个分组的行。
但是当我跑步时
select count(*) from table group by regexp_substr(data_column,'[^,]+',1,2);
它给出了
ORA-00932:incostintent数据类型:预期: - 得到:CLOB 00932. 00000 - “不一致的数据类型:预期%s获得%s”
不能用正则表达式子字符串分组吗?
答案 0 :(得分:2)
问题不是来自regexp_substr
函数,而是来自您的列数据类型:
SQL> CREATE TABLE t (data_column CLOB);
Table created
SQL> INSERT INTO t VALUES ('123123,12,123123');
1 row inserted
SQL> INSERT INTO t VALUES ('123124,12,123123');
1 row inserted
SQL> INSERT INTO t VALUES ('123125,11,123123');
1 row inserted
SQL> SELECT regexp_substr(data_column,'[^,]+',1,2) FROM t;
REGEXP_SUBSTR(DATA_COLUMN,'[^,
--------------------------------------------------------------------------------
12
12
11
在这里您可以看到该函数的行为正确,但是Oracle(使用10.2测试)不允许您使用clob列进行分组:
SQL> select count(*) from t group by data_column;
select count(*) from t group by data_column
ORA-00932: inconsistent datatypes: expected - got CLOB
您可以将函数输出转换为VARCHAR2以执行GROUP BY:
SQL> SELECT dbms_lob.substr(regexp_substr(data_column,'[^,]+',1,2), 4000),
2 COUNT(*)
3 FROM t
4 GROUP BY dbms_lob.substr(regexp_substr(data_column,'[^,]+',1,2), 4000);
DBMS_LOB.SUBSTR(REGEXP_SUBSTR( COUNT(*)
------------------------------- ----------
12 2
11 1