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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:26:01+00:00 2026-06-11T13:26:01+00:00

I need to select 10 or less rows with the SUM(FileSize) < 1000000. Results

  • 0

I need to select 10 or less rows with the SUM(FileSize) < 1000000. Results need to be ordered by AttachmentId. Let’s say that if a single FileSize exceeds the limit it’s okay just for that row (and no other) to be selected.

Ideally I’d like it to be just a select query with no more statements.

The table is:

CREATE TABLE [Attachment](
    [AttachmentId] [int] NOT NULL,
    [FileSize] [int] NOT NULL
)

Please help.

Updated. Sorry to hear that the requirements are unclear for most of the readers. There is no requirement to do any grouping. All I need to get is just plain first 10 rows or less. It will be less than 10 if their total on FileSize exceeds 1000000. It will be only 1 row if its FileSize equals 1000000 or more. The server is SQL 2008.

Updated. Many thanks to Nikola. We are getting there, but I’m still not sure how to implement the case when the first row exceeds FileSize of 1000000.

SELECT TOP 10 a.AttachmentId, rt.runningTotal 
FROM Attachment a
CROSS APPLY (SELECT SUM(aa.FileSize) AS runningTotal
   FROM Attachment aa
   WHERE aa.AttachmentId <= a.AttachmentId
) AS rt
GROUP BY a.AttachmentId, rt.runningTotal
HAVING rt.runningTotal < 1000000
ORDER BY a.AttachmentId  

Solution. This is the code (slightly modified) from Stuart which I accept as answer. Many thanks to Stuart!:

WITH CTE
  AS ( SELECT TOP 10 AttachmentId, FileSize
, RunningID = ROW_NUMBER() OVER (ORDER BY AttachmentId)
   FROM Attachment
  )
SELECT AttachmentId, FileSize
FROM CTE AS a
WHERE (SELECT SUM(FileSize)
       FROM CTE
       WHERE RunningID <= a.RunningID
       ) <= 10000000
       OR a.RunningID = 1
  • 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-11T13:26:02+00:00Added an answer on June 11, 2026 at 1:26 pm

    Maybe this will get you started:

    BEGIN TRAN
    
    CREATE TABLE [Attachment]
        (
          [AttachmentId] [int] NOT NULL
        , [FileSize] [int] NOT NULL
        ) 
    
    
    INSERT  INTO Attachment
            SELECT  1
                  , 10
            UNION
            SELECT  2
                  , 20
            UNION
            SELECT  3
                  , 30
    
    --values to exceed
    DECLARE @p INT = 50
    
    --row count to restrict to
    DECLARE @r INT = 10
    
    ;
    WITH    CTE
              AS ( SELECT   AttachmentID
                          , FileSize
                          , RunningID = ROW_NUMBER() OVER ( ORDER BY FileSize DESC )
                   FROM     Attachment
                 )
        SELECT TOP ( @r )
                AttachmentID
              , FileSize
        FROM    CTE AS A
        WHERE   ( SELECT    SUM(FileSize)
                  FROM      CTE
                  WHERE     RunningID <= A.RunningID
                ) <= @p 
    OR A.RunningID = 1
    
    
    
    ROLLBACK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a query (issued via php) to select n records (let's say 4)
I need to select past 30 days data and find the no: rows....i am
I need to select a row from table that has more than 5 millions
If I need to select just less than datetime, I do something like this:
I need ideas to optimize this query: SELECT U.Id , U.Name , SUM(P.Credit) AS
I need to fetch all the rows where the 'zip' field is less than
I need help writing select statement that will do an average of the most
I'm creating an advertisement platform for my website, where we need select 3 rows
how can i express in xpath a) need select elements which contains specified keyword.
I need to select elements without child node (including text since in <p> text

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.