14

I'm trying to combine the GROUP BY function with a MAX in oracle. I read a lot of docs around, try to figure out how to format my request by Oracle always returns:

ORA-00979: "not a group by expression"

Here is my request:

SELECT A.T_ID, B.T, MAX(A.V) 
FROM bdd.LOG A, bdd.T_B B
WHERE B.T_ID = A.T_ID
GROUP BY A.T_ID
HAVING MAX(A.V) < '1.00';

Any tips ?

EDIT It seems to got some tricky part with the datatype of my fields.

  • T_ID is VARCHAR2
  • A.V is VARCHAR2
  • B.T is CLOB
5
  • 4
    Don't compare apples (numbers) to oranges (strings). '1.00' is a string it is not a number - (1.00 or 1) is a number. Dec 19, 2013 at 10:20
  • @a_horse_with_no_name You're right, it's better now :) But i still got an 932 error, look slike oracle expected some datatype and get a CLOB insteab - which is the type of B.T
    – Xavier
    Dec 19, 2013 at 10:41
  • You can't do a group by on a CLOB column - why do you need that at all? If you need a max() then CLOB doesn't sound like the right data type. And if you store numbers in LOG.V then why don't you define it as a number?. It's a really bad design to store numbers in varchar columns. Dec 19, 2013 at 11:14
  • A.V is not a CLOB, only B.T is :) I know about bad design it's not mine and i can't change it for now
    – Xavier
    Dec 19, 2013 at 11:18
  • Why did I know your answer would be "it's not my design and I cannot change it". Apparently no one ever creates tables like that but nearly everybody has to use them - a mystery of database design. Dec 19, 2013 at 11:24

7 Answers 7

17

I'm very familiar with the phenomenon of writing queries for a table designed by someone else to do something almost completely different from what you want. When I've had this same problem, I've used.

GROUP BY TO_CHAR(theclob)

and then of course you have to TO_CHAR the clob in your outputs too.

Note that there are 2 levels of this problem... the first is that you have a clob column that didn't need to be a clob; it only holds some smallish strings that would fit in a VARCHAR2. My workaround applies to this.

The second level is you actually want to group by a column that contains large strings. In that case the TO_CHAR probably won't help.

1
  • 3
    Does not work if the clob is more than 4000 characters. Nov 29, 2018 at 10:16
1

Try this:

SELECT A.T_ID, B.T, MAX(A.V) 
FROM bdd.LOG A, bdd.T_B B
WHERE B.T_ID = A.T_ID
GROUP BY A.T_ID, B.T
HAVING MAX(A.V) < 1;
4
  • 2
    It returns another error : ORA-00932 inconsistent datatypes. Does it refer to the comment above ?
    – Xavier
    Dec 19, 2013 at 10:25
  • @x_vi_r Yes It should be. I had updated answer check it Dec 19, 2013 at 10:28
  • Still get the 932 error, as i said above the A.V is a VARCHAR2
    – Xavier
    Dec 19, 2013 at 10:30
  • @x_vi_r Try to cast in float or decimal or int Dec 19, 2013 at 10:32
1

After some fixes it seems that the major issue was in the group by

YOu have to use the same tables in the SELECT and in the GROUP BY

I also take only a substring of the CLOB to get it works. THe working request is :

    SELECT TABLE_A.ID,
       TABLE_A.VA,
       B.TRACE
FROM
(SELECT A.T_ID ID,
          MAX(A.V) VA
   FROM BDD.LOG A
   GROUP BY A.T_ID HAVING MAX(A.V) <= '1.00') TABLE_A,
                                                                BDD.T B
WHERE TABLE_A.ID = B.T_id;
1

This response is a little late, but for those who need values besides the grouping value and the Maximum criteria column, you can use ROW_NUMBER() over a partition to get what you want:

SELECT T_ID, T, V
FROM 
(
 SELECT A.T_ID, B.T, A.V, ROW_NUMBER() OVER (PARTITION BY A.T_ID ORDER BY to_number(A.V) DESC) rownumber
 FROM bdd.LOG A, bdd.T_B B
 WHERE B.T_ID = A.T_ID
)
WHERE rownumber = 1

Don't forget the DESC modifier on the ORDER BY to get the Maximum value; without it you get the Minimum value. If A.V is nullable you will also need to wrap it in NVL() or you'll just get NULLs; NULL values always sort first (at least in Oracle SQL) regardless of if you choose ascending or descending order.

0

you can if possible transform the clob into the PK if possible and than to an select on the PK. This is even faster according to execution plan then rowid. I this case i need the first not empty clob. So i say if clob is not empty use the pk else null. The result is not an clob and i can fetch the clob in an outer query.

select a.* ,r.DESCRIPTION 
from (
select distinct
FIRST_VALUE(
  case 
     when a.DESCRIPTION is null then null 
     else PK_COL 
  end 
  IGNORE NULLS) 
  OVER (ORDER BY a.sort_col desc ROWS between UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) DESCRIPTION_PK, 
group_column
from reporting a where group_column='xyz) a
join reporting r on (r.PK_COL=a.DESCRIPTION_PK);
0

This doesn't solve OP's problem, but may help in some cases.

Let we have

CREATE TABLE "TEST" (   
  ID NUMBER, 
  DATA CLOB
);

If actual CLOB data doesn't exceed 4000 chars, we can convert it to VARCHAR32 using TO_CHAR and then perform grouping we need:

SELECT 
  DATA_AS_CHAR,
  COUNT(*)
FROM
(
  SELECT
    ID,
    TO_CHAR(DATA) DATA_AS_CHAR
  FROM TEST
)
GROUP BY
  DATA_AS_CHAR;

If it exceeds, we have to apply some heuristics, e.g. substring first 4000 chars:

SELECT 
  DATA_AS_CHAR,
  COUNT(*)
FROM
(
  SELECT
    ID,
    TO_CHAR(DBMS_LOB.SUBSTR(DATA, 4000)) DATA_AS_CHAR
  FROM TEST
)
GROUP BY
  DATA_AS_CHAR;

Like any other hashing technique, this may lead to collisions (having less number of groups than expected).

-1
WITH foo as (
  SELECT A.T_ID, B.T, MAX(A.V) maxav
  FROM bdd.LOG A, bdd.T_B B
  WHERE B.T_ID = A.T_ID
  GROUP BY A.T_ID, B.T
)
SELECT * FROM foo WHERE maxav < 1
4
  • Thx for helping! Same error as before :ORA-00932 inconssitent datatype expected : - got CLOB. (CLoB is the datatype of B.T)
    – Xavier
    Dec 19, 2013 at 10:45
  • Then obviously, grouping by a CLOB column is a bad idea. Dec 19, 2013 at 10:47
  • Should i use DBMS_LOB ?
    – Xavier
    Dec 19, 2013 at 10:52
  • 2
    You should rethink your query design. LOB columns are not meant to be grouped by. One way to go could be to exclude your LOB column from the select and the group by, and later select the appropriate LOB value for your already-grouped rows using a subselect. Still, bad design though. Dec 19, 2013 at 10:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.