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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:42:55+00:00 2026-05-23T08:42:55+00:00

There is the table below: (columns: ID-CAUSE-WORK) ID | CAUSE | WORK A |

  • 0

There is the table below: (columns: ID-CAUSE-WORK)

ID  | CAUSE | WORK
A   |   C1  |  W1
B   |   C1  |  W1
C   |   C1  |  W1
D   |   C1  |  W1
E   |   C1  |  W2
F   |   C1  |  W2
G   |   C1  |  W2
H   |   C1  |  W3
I   |   C1  |  W3
FF  |   C2  |  W4
FG  |   C2  |  W4
FG  |   C2  |  W1
FG  |   C2  |  W1
FG  |   C2  |  W6

I want the two max values of work’s count per cause. That is, with a simple count(work) group by cause, the result would be:

cause | work| count(work)
c1    |  w1 | 4
c1    |  w2 | 3
c1    |  w3 | 2
c2    |  w4 | 2
c2    |  w1 | 2
c2    |  w6 | 1

I want to take only the 2 max counts works per cause:

c1    |  w1 | 4
c1    |  w2 | 3
c2    |  w4 | 2
c2    |  w1 | 2
  • 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-05-23T08:42:55+00:00Added an answer on May 23, 2026 at 8:42 am

    This should work:

    select cause,
           work,
           cnt as "COUNT"
    from (
      select cause, 
             work, 
             count(work) as cnt,
             row_number() over (partition by cause order by count(work) desc, work desc) as rown
      from your_table group by cause, work
    ) where rown <= 2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple employee table that I want to display in a particular
In a MySQL (5.1) database table there is data that represents: how long a
Ok I have two tables. Table IDAssoc has the columns bill_id , year ,
Is there a standard way to convert between TVarRec and Variant values? I want
I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
When joining across tables (as in the examples below), is there an efficiency difference
in an sql table there's an id, first name and last name field. i'd
There is a table: create table table1 ( id integer primary key, user_id varchar(36),
There is a table in a databse, let's call this table Document. This table
There is a table: doc_id(integer)-value(integer) Approximate 100.000 doc_id and 27.000.000 rows. Majority query on

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.