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 8831093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:14:09+00:00 2026-06-14T08:14:09+00:00

I do struggle with a GROUP BY — again. The basics I can handle,

  • 0

I do struggle with a GROUP BY — again. The basics I can handle, but there it is: How do I get to different columns I named in the group by, without destroying my grouping? Note that group by is only my own idea, there may be others that work better. It must work in Oracle, though.

Here is my example:

create table xxgroups (
  groupid int not null primary key,
  groupname varchar2(10)
);
insert into xxgroups values(100, 'Group 100');
insert into xxgroups values(200, 'Group 200');

drop table xxdata;
create table xxdata (
  num1 int,
  num2 int,
  state_a int,
  state_b int,
  groupid int,
    foreign key (groupid) references xxgroups(groupid)
);
-- "ranks" are 90, 40, null, 70:
insert into xxdata values(10, 10, 1, 4, 100);
insert into xxdata values(10, 10, 0, 4, 200);
insert into xxdata values(11, 11, 0, 3, 100);
insert into xxdata values(20, 22, 5, 7, 200);

The task is to create a result-row for each distinct (num1, num2) and print that groupname with the highest calculated “rank” from state_a and state_b.

Note that the first two rows have the same nums and thus only the higher ranking should be selected — with the groupname being “Group 200”.

I got quite far with the basic group by, I think.

SELECT xd.num1||xd.num2 nummer, max(ranking.goodness)
FROM xxdata xd
    , xxgroups xg
    ,( select state_a, state_b, r as goodness
       from dual
       model return updated rows
       dimension by (0 state_a, 0 state_b) measures (0 r)
       rules (r[1,4]=90, r[3,7]=80,r[5,7]=70, r[4,7]=60, r[0,7]=50, r[0,4]=40)
       order by goodness desc
      ) ranking
WHERE xd.groupid=xg.groupid
  and ranking.state_a (+) = xd.state_a
  and ranking.state_b (+) = xd.state_b
GROUP BY xd.num1||xd.num2
ORDER BY nummer
;

The result is 90% of what I need:

nummer   ranking
----------------
1010     90
1111      
2022     70

100% perfect would be

nummer   groupname
-------------------
1010     Group 100
1111     Group 100
2022     Group 200
  • The tricky part is, that I want the groupname in the result. And I can not include it in the select, because then I would have to put it into the group by as well — which I do not want (then I would not select the best ranking entry from over all groups)
  • In my solution a use a model table to calculate the “rank”. There are other solution I am sure. The point is, that it is a non-trivial calculation that I do not want to do twice.
  • I know from other examples that one could use a second query to get back to the original row to get to the groupname, but I can not see how I could to this here,
    without duplicating my ranking calculation.
  • A nice suggestion was to replace the group by with a LIMIT 1/ORDER BY goodness and use this calculating select as a filtering subselect. But a) there is no LIMIT in Oracle, and I doubt a rownum<=1 would do in a subselect and b) I can not wrap my brain around it anyway. Maybe there is a way?
  • 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-06-14T08:14:10+00:00Added an answer on June 14, 2026 at 8:14 am

    You can use the FIRST aggregation modifier to selectively apply your function over a subset of rows of a group — here a single row (SQLFiddle demo):

    SELECT xd.num1||xd.num2 nummer, 
           MAX(xg.groupname) KEEP (DENSE_RANK FIRST 
                                   ORDER BY ranking.goodness DESC) grp,
           max(ranking.goodness)
    FROM xxdata xd
        , xxgroups xg
        ,( select state_a, state_b, r as goodness
           from dual
           model return updated rows
           dimension by (0 state_a, 0 state_b) measures (0 r)
           rules (r[1,4]=90, r[3,7]=80,r[5,7]=70, r[4,7]=60, r[0,7]=50, r[0,4]=40)
           order by goodness desc
          ) ranking
    WHERE xd.groupid=xg.groupid
      and ranking.state_a (+) = xd.state_a
      and ranking.state_b (+) = xd.state_b
    GROUP BY xd.num1||xd.num2
    ORDER BY nummer;
    

    Your method with analytics works as well but since we already use aggregations here, we may as well use the FIRST modifier to get all columns in one go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I struggle a little with the understanding of generics and how they can and
I'm trying to create a virtual wiki but I already struggle with In your
[I appreciate this is a little subjective - but there must be people out
After a long struggle with jQuery countdown plugin I still cannot solve it, but
I struggle to show a second form above the main form without losing focus.
I struggle with JSON and jQuery so I'm hoping to get some help figuring
Everyday I struggle with algorithm questions and try to ask here which I can't
I struggle with dates and times in R, but I am hoping this is
I almost feel embarrassed to ask, but I always struggle with how to organize
I always struggle to keep all these macros straight in my head. Is there

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.