Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i make a select group by query giving a regexp_substr?

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?

like image 734
oguzalb Avatar asked Nov 06 '22 03:11

oguzalb


1 Answers

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
like image 121
Vincent Malgrat Avatar answered Nov 09 '22 05:11

Vincent Malgrat