Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

subquery in select with group by

donovan7800Aug 29 2013 — edited Aug 29 2013

Hi,

We have a process that runs hourly and processes a certain amount of records across a set of different business objects.  I am trying to write a small view to see the total records and number of objects processed daily.

SELECT

    TO_CHAR(PL1.process_start_date, 'YYYY-MM-DD') log_date,

    PL1.process_id,

    SUM(PL1.ods_count) rec_count,

    (       

        SELECT COUNT(plmwh_type_reln_master_seq_id) obj_count

        FROM process_log PL2

        WHERE TO_CHAR(PL2.process_start_date,'YYYY-MM-DD') = TO_CHAR(PL1.process_start_date,'YYYY-MM-DD')

    ) sub

FROM process_log PL1

GROUP BY TO_CHAR(PL1.process_start_date,'YYYY-MM-DD'), process_id

;

Is there a way to add the subquery to the group by?  Because if I don't, I get a 'not a GROUP BY expression' error.  If I do add sub to the group by I get 'sub: invalid identifier'

Here's a more generic example:

SELECT

    tbls.owner,

    tbls.table_name,

      (select COUNT(column_name) as total_columns

       from all_tab_columns cols

       where cols.owner = tbls.owner

       AND cols.table_name = tbls.table_name) subquery2

FROM all_tables tbls

group by tbls.owner, tbls.table_name, subquery2;

Remove the group by and it will run fine.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 26 2013
Added on Aug 29 2013
12 comments
1,004 views