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

  • Home
  • SEARCH
  • 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 8816311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:35:34+00:00 2026-06-14T04:35:34+00:00

I’ve been banging my head on this one. I think I’m close. (Oracle, SQL)

  • 0

I’ve been banging my head on this one. I think I’m close. (Oracle, SQL)

I’ve got a table that looks like the following.

Company    Code
Apple      A
Google     A
Microsoft  B
Apple      C
Google     B
Microsoft  B
Apple      C
Google     C
Microsoft  B

Each company can resolve to multiple codes.
What I want to do is create a SQL statement that for each company will give me the company with code that appears the most frequently. So in my example I’d get

Apple      C
Google     <nothing since there's no clear max>
Microsoft  B

What I’ve put together so far is the following. This query returns to me the company with the code that appears the most, however if I have a tie between two codes for a company, I get both back. For Google in my example I’d get (Google, A), (Google, B), (Google, C). I want nothing returned.

I believe that I can join the whole thing again with itself and some additional where clauses to filter out the duplicate companies, however I was wondering if there is a better way to do this. What’s killing me is the aggregation functions along with the group by since sometimes I get an Oracle single-group group error. Any suggestions are appreciated.

SELECT m1, c.company sp1, b.code ul1 FROM
  (SELECT MAX(c1) m1, company FROM
    (SELECT COUNT(company||code) c1, company, code FROM table GROUP BY company, code ) a
  GROUP BY company) c
left OUTER JOIN
  (SELECT COUNT(company||code) c1, company, code FROM table GROUP BY company, code ) b
ON c.company=b.company and
m1=b.c1;

mj

  • 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-14T04:35:35+00:00Added an answer on June 14, 2026 at 4:35 am

    you can do it with this:

    select company, case when c > 1 then null else code1 end code1
      from (select company, code1, recs, count(*) over (partition by company, recs ) c, 
               row_number() over (partition by company order by recs desc) rn
      from (select company, code1, count(*) recs
              from table
             group by company, code1))
     where rn = 1
     order by 1
    

    breaking this down:

    select company, code1, count(*) recs
     from table
     group by company, code1
    

    this gets us each company with their code count:

    COMPANY   C       RECS
    --------- - ----------
    Google    A          1
    Google    B          1
    Microsoft B          3
    Apple     A          1
    Apple     C          2
    Google    C          1
    

    from this we want the most popular. we do this with an analyic:

    select company, code1, recs,
           row_number() over (partition by company order by recs desc) rn
      from (select company, code1, count(*) recs
              from t1
             group by company, code1)
    

    giving:

    COMPANY   C       RECS         RN
    --------- - ---------- ----------
    Apple     C          2          1 <- we want all rn= "1" rows
    Apple     A          1          2
    Google    A          1          1<- we want all rn= "1" rows
    Google    B          1          2
    Google    C          1          3
    Microsoft B          3          1<- we want all rn= "1" rows
    

    but now if theres duplicates (as google has)..we count(*) the rows that have RN=1.

    select company, code1, recs,
           row_number() over (partition by company order by recs desc) rn,
           count(*) over (partition by company, recs ) c
      from (select company, code1, count(*) recs
              from t1
             group by company, code1)
    

    giving

    COMPANY   C       RECS         RN          C
    --------- - ---------- ---------- ----------
    Apple     C          2          1          1
    Apple     A          1          2          1
    Google    A          1          1          3
    Google    B          1          2          3
    Google    C          1          3          3
    Microsoft B          3          1          1
    

    so we need to say where RN=1 and also c = 1 (ie only ONE row had that number of recs. so we end up with:

    select company, case when c > 1 then null else Code1 end Code1
      from (select company, code1, recs, count(*) over (partition by company, recs ) c, 
               row_number() over (partition by company order by recs desc) rn
      from (select company, code1, count(*) recs
              from t1
             group by company, code1))
     where rn = 1
    

    ie rn = 1 and the c > 1 check is in the case at the top (as we don’t want to filter rows out, just mark them as ambiguous.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,

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.