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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:00:41+00:00 2026-05-29T08:00:41+00:00

my sql statement is SELECT c.type,c.title,c.datereg, d.ranknum FROM T_News AS c INNER JOIN (

  • 0

my sql statement is

SELECT c.type,c.title,c.datereg, d.ranknum
FROM T_News AS c
  INNER JOIN (
    SELECT a.id, COUNT(*) AS ranknum
    FROM T_News AS a
          INNER JOIN T_News AS b 
            ON (a.type = b.type) 
            AND (a.datereg >= b.datereg)
    GROUP BY a.id
    HAVING COUNT(*) <= 3
  ) AS d ON (c.id = d.id)
ORDER BY c.type, d.ranknum

that i get http://rickosborne.org/blog/2008/01/sql-getting-top-n-rows-for-a-grouped-query/

for Getting TOP N rows for a grouped query

EFUnitOfWork EF = new EFUnitOfWork();
T_NewsRepository News = new T_NewsRepository();
News.UnitOfWork = EF;
var query = 
    from news1 in News.All()
    join news2 in News.All() 
        on news1.type equals news2.type into resjoin
    group news1 by news1.id into idgroup
    where idgroup.Count() <= 3
    select new { idgroup };

var x = query.ToList();

I did not get any error , but “where idgroup.Count() <= 3” did not work and i get all rows in db as result

  • 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-29T08:00:42+00:00Added an answer on May 29, 2026 at 8:00 am

    Break it down into it’s smallest components and then compose the larger query from that. Let’s start with the innermost query that makes sense:

    SELECT 
        a.id, COUNT(*) AS ranknum
    FROM 
        T_News AS a
            INNER JOIN T_News AS b ON 
                (a.type = b.type) AND
                (a.datereg >= b.datereg)
    GROUP BY 
        a.id
    HAVING 
        COUNT(*) <= 3
    

    I’d convert this to:

    // Items with counts/ranknum
    var ranknum = 
        from a in News.All()
        join b in News.All() on
            a.type equals b.type
        where
            a.datereg > b.datereg
        group by a.id into g
        select new { g.Key as id, g.Count() as ranknum };
    
    // Filter the ranknum.
    ranknum = ranknum.Where(rn => rn.ranknum <= 3);
    

    Then joining that with the outer query:

    SELECT 
        c.type,c.title,c.datereg, d.ranknum
    FROM 
        T_News AS c
            INNER JOIN (<sub-query from above>) as d ON
                c.id = d.id
    ORDER BY
        c.type, d.ranknum
    

    That part becomes simple, as it’s just a join between two existing queries.

    var query =
        from c in News.All()
        join rn in ranknum on c.id = rn.id
        orderby c.type, rn.ranknum
        select new { c.type, c.title, c.datereg, rn.ranknum };
    

    Chances are the SQL that LINQ-to-Entities generates for this is going to look really ugly, and probably be inefficient, in which case, you might want to consider placing this logic in a stored procedure and then calling that through LINQ-to-Entities (which is generally true for more complex queries).

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

Sidebar

Related Questions

The sql statement is like this: select posts.id, posts.title from posts inner join (select
I have a SQL statement: select t3.item1, t3.item2, sum(t1.moneys) from table t1 inner join
This SQL statement SELECT `ip`, `when` FROM `metrics` WHERE `vidID` = '1' GROUP BY
Suppose there is a SQL statement: select * from A order by cola In
I try to use the SQL statement SELECT * FROM table ORDER BY column
If I have an sql statement: select call_id, title, description, due_date from calls I
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have an SQL statement: SELECT * FROM customers WHERE BINARY login='xxx' AND password='yyyy'
There is sql statement select distinct create_date from articles Is it possibele to write
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =

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.