Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:20:55+00:00 2026-05-24T12:20:55+00:00

Is it possible, in Oracle, to group data on the output of a user

  • 0

Is it possible, in Oracle, to group data on the output of a user defined function? I get errors when I try to, and it best illustrated by the below example:

I am trying to interrogate results in table structure similar to below:

id   | data
1000 | {abc=123, def=234, ghi=111, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=123, def=234, ghi=222, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=123, def=434, ghi=333, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=123, def=434, ghi=444, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=123, def=634, ghi=555, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=923, def=634, ghi=666, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=923, def=434, ghi=777, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=923, def=434, ghi=888, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=923, def=234, ghi=999, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}
1000 | {abc=923, def=234, ghi=000, jkl=456, mno=567, pqr=678, stu=789, vwx=890, yza=901}

There are other columns, just not shown. The id column can have different values, but in this example, does not. In the data column, only the fields abc, def, and ghi differ, all the others are the same. Again this is only illustrative for this data example.

I have written a function to extract the value assigned to fields in the data column, and it is used in the following query:

select id
      ,extract_data(data,abc) as abc
      ,extract_data(data,def) as def
from   table

giving results:

id   | abc | def
1000 | 123 | 234
1000 | 123 | 234
1000 | 123 | 434
1000 | 123 | 434
1000 | 123 | 634
1000 | 923 | 634
1000 | 923 | 434
1000 | 923 | 434
1000 | 923 | 234
1000 | 923 | 234

For reporting purposes, I would like to be able to display the amount of each type of record. There are 6 types in the above example, and ideally the output would be:

id   | abc | def | count
1000 | 123 | 234 | 2
1000 | 123 | 434 | 2
1000 | 123 | 634 | 1
1000 | 923 | 634 | 1
1000 | 923 | 434 | 2
1000 | 923 | 234 | 2

I expected to achieve this by writing SQL like so (and I’m convinced I have done so in the past):

select id
      ,extract_data(data,abc) as abc
      ,extract_data(data,def) as def
      ,count(1)
from   table
group by id
        ,abc
        ,def

This however, will not work. Oracle is giving me an error of:

ORA-00904: “ABC”: invalid identifier
00904. 00000 – “%s: invalid identifier”

From my initial research on “the google”, I have seen that I should perhaps be grouping on the column I am passing into my user defined function. This would be due to SQL requiring all columns not part of an aggregate function needing to be part of the group by clause.

This will work for some records, however in my data example, the field ghi in the data column is different for every record , thus making the data column unique, and ruining the group by clause, as a count of 1 is given for each record.

I’ve used sybase and db2 in the past, and (setting myself up for a fall here…) I’m pretty sure in both that I was able to group by on the output of a user defined function.

I thought that there might be an issue with the naming of the columns and how they can be referenced by the group by? Referencing by column number hasn’t worked.

I’ve tried various combinations of what I have, and can’t get it to work, so I’d appreciate any insight you guys out there could give.

If you need any more information I’ll edit as required or clarify in the comments.

Thanks,
GC.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:20:56+00:00Added an answer on May 24, 2026 at 12:20 pm

    You should be able to group by the functions themselves, not by the aliases

    select id
          ,extract_data(data,abc) as abc
          ,extract_data(data,def) as def
          ,count(*)
    from   table
    group by id
            ,extract_data(data,abc)
            ,extract_data(data,def) 
    

    Note that this does not generally involve executing the function multiple times. You can see that yourself with a simple function that increments a counter in a package every time it is called

    SQL> ed
    Wrote file afiedt.buf
    
      1  create or replace package pkg_counter
      2  as
      3    g_cnt integer := 0;
      4* end;
    SQL> /
    
    Package created.
    
    SQL> create or replace function f1( p_arg in number )
      2    return number
      3  is
      4  begin
      5    pkg_counter.g_cnt := pkg_counter.g_cnt + 1;
      6    return mod( p_arg, 2 );
      7  end;
      8  /
    
    Function created.
    

    There are 16 rows in the EMP table

    SQL> select count(*) from emp;
    
      COUNT(*)
    ----------
            16
    

    so when we execute a query that involves grouping by the function call, we hope to see the function executed only 16 times. And that is, in fact, what we see.

    SQL> select deptno,
      2         f1( empno ),
      3         count(*)
      4    from emp
      5   group by deptno,
      6            f1( empno );
    
        DEPTNO  F1(EMPNO)   COUNT(*)
    ---------- ---------- ----------
                        1          1
            30          0          4
            20          1          1
            10          0          2
            30          1          2
            20          0          4
            10          1          1
                        0          1
    
    8 rows selected.
    
    SQL> begin
      2    dbms_output.put_line( pkg_counter.g_cnt );
      3  end;
      4  /
    16
    
    PL/SQL procedure successfully completed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get ref cursor from oracle stored procedure by using DAAB
Possible Duplicate: Printing Table's structure/schema oracle has 'DESCRIBE' to get all the details of
Is this possible in (Oracle) SQL? I have a varchar for example This is
Is is possible to join to an Oracle table valued function? SELECT * FROM
Possible Duplicate: oracle insert if row not exists I am using the query below
Possible Duplicate: how to retrieve two columns data in A,B format in Oracle Suppose
In Oracle, it's possible to get a count of distinct values in multiple columns
Possible Duplicate: Oracle: get list of all tables? How do I list all tables
Possible Duplicate: How to best split csv strings in oracle 9i can you help
Possible Duplicate: Rename Oracle Table or View I know you can use ALTER TABLE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.