limiting the rows to where the sum a column equals a certain value in MySQL

前端 未结 8 2026
闹比i
闹比i 2020-11-29 08:32

I want to write a query which returns all rows until the sum of one of the columns value reaches a certain value.

For example in the table below:

           


        
相关标签:
8条回答
  • 2020-11-29 09:15

    Here's a way to do it without a stored procedure:

    SET @msum := 0;
    SELECT t1.* 
    FROM (
        SELECT m.*,  
              (@msum := @msum + m.meetings) AS cumulative_meetings
        FROM meetings m 
        ORDER BY m.date ASC
    ) t1 
    WHERE t1.cumulative_meetings <= 7;
    
    0 讨论(0)
  • 2020-11-29 09:16

    Hemant you do not state the RDBMS that use. Here is a script in t-sql that you can use in order to solve your problem.

    DECLARE @numberToReach INT;
    SET @numberToReach = 10; --you can change this
    
    DECLARE @date DATETIME;
    DECLARE @etc VARCHAR(20);
    DECLARE @meeting INT;
    DECLARE @temp_sum INT;
    
    CREATE TABLE #tempTable
        (
            Dates DATETIME,
            Etcs VARCHAR(20),
            Meeting INT,
        )
    
    DECLARE tempcursor CURSOR FOR
            SELECT *
            FROM YourTABLENAME
    OPEN tempcursor;
    FETCH NEXT FROM tempcursor INTO @date, @etc, @meeting;
    
    WHILE(@@FETCH_STATUS = 0)
    BEGIN
        SET @temp_sum = @temp_sum + @meeting;
        IF @temp_sum < @numberToReach 
        BEGIN
            INSERT INTO #tempTable
            (
                Dates,
                Etcs,
                Meeting
            )
            VALUES
            (
                @date, 
                @etc, 
                @meeting
            )
    
            FETCH NEXT FROM tempcursor INTO @date, @etc, @meeting;
        END 
    END
    
    SELECT * FROM #tempTable
    
    CLOSE tempcursor
    DEALLOCATE tempcursor
    
    DROP TABLE  #tempTable
    
    0 讨论(0)
  • 2020-11-29 09:18

    If you are using SQL Server, then use CROSS APPLY.

    0 讨论(0)
  • 2020-11-29 09:23

    In Oracle, I think you can use the SUM analytic function. You can refer here: http://www.adp-gmbh.ch/ora/sql/analytical/sum.html

    0 讨论(0)
  • 2020-11-29 09:24

    Here's an ugly way:

    SELECT *
    FROM meetings m1
    WHERE (SELECT SUM(m2.Meeting) 
           FROM meetings m2 
           WHERE m2.DATE < m1.DATE OR (m2.DATE = m1.DATE AND m2.ETC >= m1.ETC)) <= 7
    

    The ordering is based on DATE first, then ETC in descending order, since that seems to be what we have to go on. Note that if that is not unique, you will get the wrong result.

    0 讨论(0)
  • 2020-11-29 09:26

    This one outperforms all.

    SET @runningTotal=0;
    SELECT
      O.Id,
      O.Type,
      O.MyAmountCol,
      @runningTotal + O.MyAmountCol as 'RunningTotal',
      @runningTotal := @runningTotal + O.MyAmountCol
    FROM Table1 O
    HAVING RunningTotal <=7;
    

    SQL Fiddle

    Take a look at execution plans for both queries.

    0 讨论(0)
提交回复
热议问题