5

I have a column named data_column, it has some value like "123123,12,123123". I want to count rows grouped by the second one.

But when i run

select count(*) from table group by regexp_substr(data_column,'[^,]+',1,2);

It gives

ORA-00932: incostintent datatypes: expected: - got: CLOB 00932. 00000 - "inconsistent datatypes: expected %s got %s"

Cant i group by a regex substring?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
oguzalb
  • 428
  • 4
  • 15

1 Answers1

2

the problem doesn't come from the regexp_substr function but from your column data type:

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

Here you see that the function behaves correctly, however Oracle (tested with 10.2) doesn't allow you to group with a clob column:

SQL> select count(*) from t group by data_column;

select count(*) from t group by data_column

ORA-00932: inconsistent datatypes: expected - got CLOB

You can convert the function output to a VARCHAR2 to perform the 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
Vincent Malgrat
  • 66,725
  • 9
  • 119
  • 171