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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:36:34+00:00 2026-05-13T18:36:34+00:00

I’m trying to convert a Linq query to SQL. My Linq query looks like

  • 0

I’m trying to convert a Linq query to SQL. My Linq query looks like this:

from s in Somethings
where s.CreatedTime >= new DateTime(2010, 01, 01)
where s.CreatedTime <  new DateTime(2010, 02, 01)
group s by s.Data into grouping
select grouping.OrderByDescending(s => s.CreatedTime)
               .ThenByDescending( s => s.UpdatedTime)
               .First();

In words, that is supposed to get all things from a certain month. Then group them by a specific key. For each key, I want the most recently created element. If two elements with the same key were created at the same time, I want to break ties by most recently updated.

So far I have this for SQL

SELECT s1.*
FROM Somethings s1
JOIN (
  SELECT s.Date AS Data, MAX(CreatedTime) AS CreatedTime
  FROM Somethings s
  WHERE s.CreatedTime >= '20100101' 
    AND s.CreatedTime <  '20100201' 
  GROUP BY s.Data
) s2 ON s1.Data = s2.Data
    AND s1.CreatedTime = s2.CreatedTime

That works, but I can’t control how ties are broken.

What I really want is a way to arbitrarily sort each grouping like I can in Linq. I want to define my own aggregation function that takes a set of rows, and returns one row. Is this possible in SQL, or is Linq more expressive? SQL’s aggregation functions MAX, MIN, COUNT etc don’t seem to be first class functions like their equivalent in Linq is. Of course it could just be my lack of knowledge of SQL.

Here’s a made up example to further illustrate what I want to do in SQL:

SELECT (SELECT * 
        FROM grouping 
        ORDER BY CreatedTime DESC, UpdatedTime DESC
        LIMIT 1)
FROM Somethings s
WHERE s.CreatedTime >= '20100101' 
  AND s.CreatedTime <  '20100201' 
GROUP BY s.Data AS grouping

In this example, my illegal inner query is serving the same role as an aggregation function.

  • 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-13T18:36:34+00:00Added an answer on May 13, 2026 at 6:36 pm

    This isn’t really an aggregation, it’s just a groupwise maximum. ROW_NUMBER is the easiest way to write these queries:

    ;WITH CTE AS
    (
        SELECT
            Query, CreatedTime, UpdatedTime, <other_columns>,
            ROW_NUMBER() OVER
            (
                PARTITION BY Query
                ORDER BY CreatedTime DESC, UpdatedTime DESC
            ) AS RowNum
        FROM Somethings
        WHERE CreatedTime >= '20100101'
        AND CreatedTime < '20100201'
    )
    SELECT *
    FROM CTE
    WHERE RowNum = 1
    

    It’s not necessarily the most efficient, but it’s reasonably good in most cases. And the nice thing is that you can modify this to do top 2 per group, top 3, etc., and you have complete control over ties.

    (P.S. I hope you don’t actually name the column “Query”)

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put

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.