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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:17:54+00:00 2026-06-08T17:17:54+00:00

Given this table: # select * from messages; id verbosity 1 20 2 20

  • 0

Given this table:

# select * from messages;
id verbosity
1 20
2 20
3 20
4 30
5 100

(5 rows) I would like to select N messages for which sum of verbosity is lower than N. So if N = 70, the desired result will be messages with id 1, 2 and 3. It’s important the solution is database-independent. It should work at least on PostgreSQL and SQLite.

Something like:

SELECT * FROM messages GROUP BY id HAVING SUM(verbosity) < 70;

doesn’t sum all values from the verbosity column.

  • 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-08T17:17:55+00:00Added an answer on June 8, 2026 at 5:17 pm
    SELECT m.id, sum(m1.verbosity) AS total
    FROM   messages m
    JOIN   messages m1 ON m1.id <= m.id
    WHERE  m.verbosity < 70    -- optional, to avoid pointless evaluation
    GROUP  BY m.id
    HAVING SUM(m1.verbosity) < 70
    ORDER  BY total DESC
    LIMIT  1;
    

    This assumes a unique, ascending id like you have in your example.


    In modern Postgres – or generally with modern standard SQL (but not in SQLite):

    Simple CTE

    WITH cte AS (
       SELECT *, sum(verbosity) OVER (ORDER BY id) AS total
       FROM   messages
       )
    SELECT *
    FROM   cte
    WHERE  total < 70
    ORDER  BY id;
    

    Recursive CTE

    Should be faster for big tables where you only retrieve a small set.

    WITH RECURSIVE cte AS (
       (  -- parentheses required
       SELECT id, verbosity, verbosity AS total
       FROM   messages
       ORDER  BY id
       LIMIT  1
       )
    
       UNION ALL 
       SELECT c1.id, c1.verbosity, c.total + c1.verbosity 
       FROM   cte c
       JOIN   LATERAL (
          SELECT *
          FROM   messages
          WHERE  id > c.id
          ORDER  BY id
          LIMIT  1
          ) c1 ON  c1.verbosity < 70 - c.total
       WHERE c.total < 70
       )
    SELECT *
    FROM   cte
    ORDER  BY id;
    

    All standard SQL, except for LIMIT.

    Strictly speaking, there is no such thing as "database-independent". There are various SQL-standards, but no RDBMS complies completely. LIMIT works for PostgreSQL and SQLite (and some others). Use TOP 1 for SQL Server, rownum for Oracle. Here’s a comprehensive list on Wikipedia.

    The SQL:2008 standard would be:

    ...
    FETCH  FIRST 1 ROWS ONLY
    

    … which PostgreSQL supports – but hardly any other RDBMS.

    The pure alternative that works with more systems would be to wrap it in a subquery and

    SELECT max(total) FROM <subquery>
    

    But that is slow and unwieldy.

    db<>fiddle here
    Old sqlfiddle

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

Sidebar

Related Questions

I have this query (which I removed some keys from for brevity's sake): SELECT
I have a table, Messages, which is set out as follows: This table stores
I have a query similar to this: SELECT itemID FROM itemTable WHERE itemID LIKE
Given this JavaScript code (which is just a comment referring to a url): //
I am trying to select data from a MySQL table, but I get one
In this table, there is a column called 'position'. This column counts up from
I have some tables like this: USERS TABLE: | id | created | active
In Oracle, given a list of table names, I want to perform 'select column1
Why doesn't this work (when parameter is set to 1) : SELECT * FROM
I have a script: $friendnotes = mysql_query(SELECT nid,user,subject,message FROM friendnote WHERE tousers LIKE '%$userinfo[username]%'

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.