On 18/09/2006, "Ahmad Al-Twaijiry" wrote:

> I want to run SQL query that will return to me the first records that
> the SUM of Total field = 100

USE test;
DROP TABLE IF EXISTS foo;
CREATE TABLE foo ( 
id INT UNSIGNED NOT NULL PRIMARY KEY, total INT NOT NULL);

INSERT INTO foo VALUES
(1, 20), (2, 30), (3, 40), (4, 10), (5, 20), (6, 20);

SELECT
        f1.id,
        f1.total
FROM foo f1
JOIN foo f2 ON f2.id <= f1.id
GROUP BY f1.id, f1.total
HAVING SUM(f2.total) <= 100;

-- 
felix

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to