我可以通过查询给出一个选择组来给出一个regexp_substr吗?

时间:2022-09-15 20:07:43

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.

我有一个名为data_column的列,它有一些像“123123,12,123123”的值。我想计算按第二个分组的行。

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"

ORA-00932:incostintent数据类型:预期: - 得到:CLOB 00932. 00000 - “不一致的数据类型:预期%s得到%s”

Cant i group by a regex substring?

不能用正则表达式子串组合吗?

1 个解决方案

#1


2  

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

问题不是来自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

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

在这里,您可以看到该函数的行为正确,但是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

You can convert the function output to a VARCHAR2 to perform the GROUP BY:

您可以将函数输出转换为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

#1


2  

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

问题不是来自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

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

在这里,您可以看到该函数的行为正确,但是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

You can convert the function output to a VARCHAR2 to perform the GROUP BY:

您可以将函数输出转换为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