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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:43:11+00:00 2026-05-26T19:43:11+00:00

I have a table that looks like this one : +——+——+——————+ | item |

  • 0

I have a table that looks like this one :

+------+------+------------------+
| item | val  |    timestamp     |
+------+------+------------------+
|  1   | 3.66 | 16-05-2011 09:17 | 
|  1   | 2.56 | 16-05-2011 09:47 | 
|  2   | 4.23 | 16-05-2011 09:37 | 
|  3   | 6.89 | 16-05-2011 11:26 | 
|  3   | 1.12 | 16-05-2011 12:11 |
|  3   | 4.56 | 16-05-2011 13:23 |
|  4   | 1.10 | 16-05-2011 14:11 |
|  4   | 9.79 | 16-05-2011 14:23 |
|  5   | 1.58 | 16-05-2011 15:27 |
|  5   | 0.80 | 16-05-2011 15:29 |
|  6   | 3.80 | 16-05-2011 15:29 |
+------+------+------------------+

so, the grand total of all item for the day : 16 May 2011 is : 40.09

Now i want to retrieve which items of this list form an amount of 80% of the grand total.
Let me make an example :

  • Grand Total : 40.09
  • 80% of the Grand Total : 32.07

starting from the item with more percentage weight on the total amount i want to retrieve the grouped list of the item that form the 80% of the grand total :

+------+------+
| item | val  |
+------+------+
|  3   | 12.57|
|  4   | 10.89|
|  1   |  6.22|
+------+------+

As you can see the elements in the result set are the elements grouped by item code and ordered from the element with greater percentage weight on the grand total descending until reaching the 80% threshold.

From the item 2 onward the items are discarded from the result set because they exceed the threshold of 80%, because :

12.57 + 10.89 + 6.22 + 4.23 > 32.07 (80 % of the grand total )

This is not an homework, this is a real context where i am stumbled and i need to achieve the result with a single query …

The query should run unmodified or with few changes on MySQL, SQL Server, PostgreSQL .

  • 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-26T19:43:12+00:00Added an answer on May 26, 2026 at 7:43 pm

    You can do this with a single query:

    WITH Total_Sum(overallTotal) as (SELECT SUM(val) 
                                     FROM dataTable), 
    
         Summed_Items(id, total) as (SELECT id, SUM(val)
                                     FROM dataTable
                                     GROUP BY id),
    
         Ordered_Sums(id, total, ord) as (SELECT id, total, 
                                             ROW_NUMBER() OVER(ORDER BY total DESC)
                                          FROM Summed_Items),
    
         Percent_List(id, itemTotal, ord, overallTotal) as (
                      SELECT id, total, ord, total
                      FROM Ordered_Sums
                      WHERE ord = 1
                      UNION ALL
                      SELECT b.id, b.total, b.ord, b.total + a.overallTotal
                      FROM Percent_List as a
                      JOIN Ordered_Sums as b
                      ON b.ord = a.ord + 1
                      JOIN Total_Sum as c
                      ON (c.overallTotal * .8) > (a.overallTotal + b.total))
    
    SELECT id, itemTotal
    FROM Percent_List
    

    Which will yield the following:

    id      itemTotal
    3       12.57  
    4       10.89  
    1       6.22  
    

    Please note that this will not work in mySQL (no CTEs), and will require a more recent version of postgreSQL to work (otherwise OLAP functions are not supported). SQLServer should be able to run the statement as-is (I think – this was written and tested on DB2). Otherwise, you may attempt to translate this into correlated table joins, etc, but it will not be pretty, if it’s even possible (a stored procedure or re-assembly in a higher level language may then be your only option).

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

Sidebar

Related Questions

I have a table that looks like something like this: timestamp value person ===============================================
I have a Table that looks like this MAC-ADDRESS | ACCESSPOINT | TIMESTAMP Data
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a layout that looks like this: It's a table with one cell
I have a table that looks like this: Month Site Val 2009-12 Microsoft 10
I have one sql table that looks like this called posts: id | user
I have one table that looks something like this: id parent_id name 1 0
I have a table that looks like this: nid vid tid 1 2 3
I have a table that looks like this: <table> <thead> <!-- random table headings
I have a table that looks like this: |StreetName NR| NR | |Teststreet 34|

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.