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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:36:29+00:00 2026-05-25T20:36:29+00:00

A while ago I found a handy query for mysql to get the top

  • 0

A while ago I found a handy query for mysql to get the top X per group by.
This is what I mean:

if this is the table:

rid id1 id2 id3 value
1   1   1   1   10
2   1   1   2   11
3   1   1   3   9
4   1   2   1   20
5   1   2   2   18
6   1   2   3   23
7   1   3   1   30
8   1   3   2   34
9   1   3   3   31
10  1   3   4   27
11  1   3   5   32
12  1   4   1   41
13  1   4   2   40
14  1   4   3   43
15  1   5   1   53
16  1   5   2   51
17  1   5   3   50
18  2   1   1   11
19  2   1   2   9
20  2   1   3   12

I want to get this result:

rid id1 id2 id3 value
2   1   1   2   11
6   1   2   3   23
8   1   3   2   34
14  1   4   3   43
15  1   5   1   53

I can get this by running the following mysql query:

SELECT * FROM
  (SELECT * FROM idsandvalue 
   WHERE id1=1 AND 
     (SELECT COUNT(*) FROM idsandvalue AS helper 
      WHERE helper.id1 = idsandvalue.id1 
      AND helper.id2= idsandvalue.id2 
      AND helper.value > idsandvalue.value
     ) < 1
  )a;

if I change < 1 to lets say 2, 3 or x I can get the top x per id2 where id1=1 (so, two of the same id2’s with different id3’s) like this:

rid id1 id2 id3 value
1   1   1   1   10
2   1   1   2   11
4   1   2   1   20
6   1   2   3   23
8   1   3   2   34
11  1   3   5   32
12  1   4   1   41
14  1   4   3   43
15  1   5   1   53
16  1   5   2   51

two questions.
A) the query is not really fast in MySQL. Takes a while (runs a table with 3207394 rows). Can I get the same result with the use of a different query (I was not able to get it).
B) How can I translate this to linq? Due to the strange where statement, I have no clue how to translate this into linq.

(later I added this extra question as well)
in MySQL I use this query:

SELECT *,COUNT(*) AS Counter FROM idsandvalue GROUP BY id1,id2;

to get this result:

rid id1 id2 id3 value   Counter
1   1   1   1   10       3
4   1   2   1   20       3
7   1   3   1   30       5
12  1   4   1   41       3
15  1   5   1   53       3
18  2   1   1   11       3

I’m also having difficulties translating this to Linq.

(extra info was too big for comment)

Hi John (thanks for the quick respond).
with this mysql query

SELECT * FROM 
  (SELECT * FROM idsandvalue 
   WHERE id1=1 AND 
     (SELECT COUNT(*) FROM idsandvalue AS helper 
      WHERE helper.id1 = idsandvalue.id1 
      AND helper.id2= idsandvalue.id2 
      AND helper.value > idsandvalue.value
     ) < 1
  )a 

I try to get the rows for each grouped id1 and id2 with it’s biggest value. That’s why in this case I get for instance row with id 2. 11 is the biggest of 10,11 and 9 where id1=1 and id2=1. and that’s why I get the row with id 8, because where id1=1 and id2=3 the biggest value for column value is 34. If I change the query to < 2, I get the top two. for id2=1 and id2=3 this would give the rows with id 8 and 11. Is this better explained?

  • 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-25T20:36:30+00:00Added an answer on May 25, 2026 at 8:36 pm

    Recreated your table in SQL Server and ran your query against it, than converted the query via linqer:

    from a in ((from idsandvalue in db.idsandvalue whereidsandvalue.id1 == 1 &&
    
        (from helper in db.idsandvalue
        where
          helper.id1 == idsandvalue.id1 &&
          helper.id2 == idsandvalue.id2 &&
          helper.value > idsandvalue.value
        select new {
          helper
        }).Count() < 1
    select new {
      idsandvalue
    }))
    

    select new {
    a.idsandvalue.rid,
    a.idsandvalue.id1,
    a.idsandvalue.id2,
    a.idsandvalue.id3,
    a.idsandvalue.value
    }

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

Sidebar

Related Questions

I found an answer to this a while ago, and made a mental note
A while ago I had a query that I ran quite a lot for
A while ago I was trying to figure out a way of doing this
I found a lua aes solution on the web a while ago. And have
A while ago I found out that FOP doesn't allow you to use floats,
This took me a while to figure out and I found the answer on
A while ago I found a packaging tool for visual studio that allowed quite
A while ago, I found in a website some code examples of utility functions
A few days ago, this was my question, and I found the answer. Maybe
A while ago, I started on a project where I designed a html-esque XML

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.