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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:53:33+00:00 2026-06-08T09:53:33+00:00

I have a table called Sales: id_sale id_ticket total state ——- ——— —– —–

  • 0

I have a table called Sales:

id_sale  id_ticket  total  state
-------  ---------  -----  -----
100      100        30     inactive    
101      101        30     active    
102      101        60     active    
103      102        30     active    
104      102        30     active    
105      103        10     active    
106      103        5      active    

What I need is to get the total sum of the total column, sum(total) when the state is active and multiply it for x percentage (I can do that), having that I have to select all the tickets that their sum (of all the tickets summed) is <= sum(total) * x percentage and copy it to another table (same values)

So if I have the sum of ticket 101 = 90 and the sum of ticket 102 = 60 and the sum of ticket 103 = 15, and the sum(total)*x percentage = 160, the query will only return all the info from the tickets 101 and 102.

Reading all morning I found a code that somehow does something like what I need but using id_Sales and not id_ticket:

    select t1.id_sale, t1.total, SUM(t2.total) as sum
    from sales t1
    inner join sales t2 on t1.id_sale >= t2.id_sale
    where t2.state='active'
    group by t1.id_sale, t1.total
    having sum(t2.total)<=(
                           select sum(total)*.65
                           from sales
                           where state='active')
    order by t1.id_sale

What i espect to recieve is:

id_sale  id_ticket  total  state
-------  ---------  -----  ----- 
101      101        30     active    
102      101        60     active    
103      102        30     active    
104      102        30     active

In wich the sum of ticket 101 and ticket 102 is <= sum(total)*x

so the sum of 101 and 102 = 150 and sum(total)*x = 160 (for example)

How can I do what I need or how can I edit this code so it does what I’m looking for.

  • 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-08T09:53:34+00:00Added an answer on June 8, 2026 at 9:53 am

    You could start with something like this:

    WITH grouped_and_ranked AS (
      SELECT
        id_ticket,
        ticket_total = SUM(total),
        grand_total = SUM(SUM(total)) OVER (),
        ticket_rnk = ROW_NUMBER() OVER (ORDER BY id_ticket)
      FROM sales
      WHERE state = 'active'
      GROUP BY id_ticket
    ),
    cumulative AS (
      SELECT
        id_ticket,
        ticket_total,
        grand_total,
        ticket_rnk,
        total_so_far = ticket_total
      FROM grouped_and_ranked
      WHERE ticket_rnk = 1
      UNION ALL
      SELECT
        r.id_ticket,
        r.ticket_total,
        r.grand_total,
        r.ticket_rnk,
        total_so_far = c.total_so_far + r.ticket_total 
      FROM grouped_and_ranked r
        INNER JOIN cumulative c ON r.ticket_rnk = c.ticket_rnk + 1
      WHERE r.ticket_total columns
    )
    SELECT
      s.id_sale,
      s.id_ticket,
      s.total,
      s.state
    FROM sales s
    INNER JOIN cumulative c ON s.id_ticket = c.id_ticket
    ;
    

    The first CTE, grouped_and_ranked, retrieves active sales and calculates totals by ticket as well as the grand total (using windowed SUM()). It also assigns ranking numbers to the tickets to be used later when calculating the running total.

    The next CTE, cumulative, is a recursive CTE. It retrieves rows from the previous result set as long as the running total is no greater than the specified @percent‘age of the grand total.

    The main SELECT uses the final ticket list to get the detail rows, by joining the list back to the sales table.

    This query can be tested at SQL Fiddle.

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

Sidebar

Related Questions

I have a table called sales with the following few columns: salesno (PK, char(25))
In the sales table I have a field called end_date (date type), I want
I have a Rails Table, called Sales, that I would like to render in
I have the following table called [Store_Sales] - Store Date Sales 1 23/04 10000
I have table called stats . In am inserting yes or no in the
i have table called as Support, which have a field named Name and contains
I have table called Buttons. Buttons table i have column button_number . Table contain
I have table called posts in my DB. Each post has field called social_network
I have a table called 'MyTable' that looks like: ID | Item | Type
I have a table called States and i am displaying values of states in

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.