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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:14:54+00:00 2026-06-13T10:14:54+00:00

I have a table rel with fields id refid cellid cat and some working

  • 0

I have a table rel with fields id refid cellid cat and some working PHP code to count the number of rows for each cellid and return this as an array for later use.

<?php
// connect to database
require($DOCUMENT_ROOT . "connect.php");
// count the number of relationships for each cell id
  $cell_array = array();
  if ($result = mysqli_query($link, "SELECT cellid, COUNT(*) totalcount, cat FROM rel GROUP BY cellid")) {
    if (mysqli_num_rows($result)) {
      while ($row = mysqli_fetch_assoc($result)) {
        $cell_array[$row["cellid"]] = $row['totalcount'];
      }
    }
  }
?>

I now want to add an additional function, calculate the Mode (most common value) of cat for each cellid and include that value in the array. If there is more than one mode or no mode then return 9 (cat can only have values of 1 to 8).

I’ve been reading around how to find the Mode and the common solution looks like this:

SELECT COUNT(*) frequency, cat, cellid
FROM rel
GROUP BY cat
ORDER BY COUNT(*) DESC
LIMIT 1

But this does not return the most common cat for each cellid just the most common cat in general.

I have been working using a sub-query

SELECT cellid, cat, COUNT( * ) 
FROM  `rel` 
GROUP BY cellid, cat

Which produces a row for each cellid cat combination, but I’m unsure how to then use this to find the total number of entries for each cellid and the the most common cat for each cellid

EDIT

I have made some further progress, I now have a working query to find the mode cat for each cellid

SELECT cellid, cat
FROM rel t
GROUP BY cellid, cat
HAVING cat = ( 
SELECT cat
FROM rel
WHERE cellid = t.cellid
GROUP BY cat
ORDER BY COUNT( * ) DESC , cat
LIMIT 1 )

But I am still unsure how to combine the two queries are the output from one cannot be used in the other.

  • 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-13T10:14:55+00:00Added an answer on June 13, 2026 at 10:14 am

    You can combine them by joining them together:

    select cell.*, mode.cat as modecat
    from (SELECT cellid, COUNT(*) totalcount
          FROM rel
          GROUP BY cellid
         ) cell join
         (SELECT cellid, cat
          FROM rel t
          GROUP BY cellid, cat
          HAVING cat = (SELECT cat
                        FROM rel
                        WHERE cellid = t.cellid
                        GROUP BY cat
                        ORDER BY COUNT( * ) DESC , cat
                        LIMIT 1
                      )
         ) mode
         on cell.cellid = mode.cnt
    

    This doesn’t handle the case of multiple values. If you only wanted an arbitrary value, I would go with:

    select cell.*,
           (select cat
            from rel
            where cell.cellid = rel.cellid
            group by cellid, cat
            order by COUNT(*) desc
            limit 1
           ) as mode
    from (SELECT cellid, COUNT(*) totalcount
          FROM rel
          GROUP BY cellid
         ) cell
    

    Identifying muliples is a bit trickier. It requires an additional aggregation in the subquery:

    select cell.*,
           (select (case when min(cat) = max(cat) then min(cat) else 'MULTIPLE' end) as mode
            from (select cat, COUNT(*) as cnt
                  from rel
                  where cell.cellid = rel.cellid
                  group by cellid, cat
                 ) t
            group by cnt
            order by cnt desc
            limit 1
           ) mode
    from (SELECT cellid, COUNT(*) totalcount
          FROM rel
          GROUP BY cellid
         ) cell
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table rel --------------------- |id|refid|cellid|cat| |1 |1 |1 |1 | |2 |2
I have a MANY_MANY relation: 'rel'=>array(self::MANY_MANY, 'MyClass','table(id_1,id_2)', 'condition'=>'some condiotions AND field_name=:param') I get the
I have some code to produce a table from an SQL query. I would
I have table with around 70 000 rows. There is 6000 rows that i
I have table in which I am inserting rows for employee but next time
I have table and this table contain result column with some entries. I just
I have table A with fields user1 and user2 and table B with user3
In oracle: I have a table rel with these columns (object_id1, object_id2) that relates
I have the following code: $(.perf-row).click(function(e) { if (e.target.tagName.toLowerCase() == a) { pageTracker._trackPageview($(this).attr(rel)); return
I have some tables that has the following structure: cat( cat_id(pk), cat_name) ven( ven_id(pk),

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.