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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:14:39+00:00 2026-06-02T20:14:39+00:00

I have a table like this; +—-+———+————-+ | id | user_id | screenWidth |

  • 0

I have a table like this;

+----+---------+-------------+
| id | user_id | screenWidth |
+----+---------+-------------+
|  1 |       1 |        1366 |
|  2 |       1 |        1366 |
|  3 |       1 |        1366 |
|  4 |       1 |        1366 |
|  5 |       2 |        1920 |
|  6 |       2 |        1920 |
|  7 |       3 |        1920 |
|  8 |       4 |        1280 |
|  9 |       5 |        1280 |
| 10 |       6 |        1280 |
| 11 |       7 |        1890 |
| ...|   ...   |     ...     |
| ...|   ...   |     ...     |
| ...|   ...   |     ...     |
| 100|       6 |        1910 |
+----+---------+-------------+

Where there are lots of screenWidths, but 90% of them are equal to one of 5 values.

Using a query like:

SELECT      screenwidth
        ,   COUNT(DISTINCT user_id) AS screenwidthcount
FROM        screenwidth
GROUP BY    screenwidth
ORDER BY    screenwidthcount;

(Thanks from How do I count only the first occurrence of a value?)

I get a nice count for the number of times a screenWidth has occurred, counting only once per user.

Is there a way to count the most popular screenWidths, then collect all the others in a category called “other” – that is to say, instead of the query above returning loads of rows, it returns 6, the first 5 being the first 5 it returns currently, the 6th being called other with the sum of the rest of the values?

  • 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-02T20:14:41+00:00Added an answer on June 2, 2026 at 8:14 pm

    Here is one way to do it. Following script was created based on the answer to this question Rank function in MySQL

    The query assigns a ranking to all the rows for which distinct count has been computer. I have assigned a value of 2 in the CASE expressions. This denotes that the script will display the top 2 screen widths and the remaining will be clubbed into Other. You need to change the value according to your requirement. I have hard coded the value 99999 to group all the other rows.

    There might be a better way to do this but this is one of the ways I could make it to work.

    Click here to view the demo in SQL Fiddle.

    Script:

    CREATE TABLE screenwidth 
    (
        id INT NOT NULL
      , user_id INT NOT NULL
      , screenwidth INT NOT NULL
    );
    
    INSERT INTO screenwidth (id, user_id, screenwidth) VALUES
      (1, 1, 1366),
      (2, 2, 1366),
      (3, 2, 1366),
      (4, 2, 1366),
      (5, 3, 1366),
      (6, 1, 1920),
      (7, 2, 1920),
      (8, 1, 1440),
      (9, 2, 1440),
      (10, 3, 1440),
      (11, 4, 1440),
      (12, 1, 1280),
      (13, 1, 1024),
      (14, 2, 1024),
      (15, 3, 1024),
      (16, 3, 1024),
      (17, 3, 1024),
      (18, 1, 1366);
    
    SELECT screenwidth
        , SUM(screenwidthcount) AS screenwidth_count
    FROM
    (
        SELECT      CASE    
                        WHEN @curRank < 2 THEN screenwidth 
                        ELSE 'Other' 
                    END AS screenwidth
                ,   screenwidthcount
                ,   @curRank := 
                    (   CASE 
                            WHEN @curRank < 2 THEN @curRank + 1 
                            ELSE 99999
                        END
                    ) AS rank
        FROM
        (
            SELECT      screenwidth
                    ,   COUNT(DISTINCT user_id) AS screenwidthcount
            FROM        screenwidth
            GROUP BY    screenwidth
            ORDER BY    screenwidthcount DESC
        ) T1
                    ,   (SELECT @curRank := 0) r
    ) T2
    GROUP BY    screenwidth
    ORDER BY    rank;
    

    Output:

    SCREENWIDTH SCREENWIDTH_COUNT
    ----------- -----------------
    1440               4
    1024               3
    Other              6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table like this ID NAME Amount 1 cal 100 2 cal
I have a table like this: <table id=myTable cellspacing=0 cellpadding=10 border=0 width=100%> <tbody> ....
I have a table like this: user_id hobbie1 hobbie2 hobbie3 1 ski soccer tv
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have a table like this: TransId. LayerNo. AccountId. 100. 1. 2. 100. 2.
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |

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.