List:General Discussion« Previous MessageNext Message »
From:Felix Geerinckx Date:September 21 2006 1:20pm
Subject:Re: SUM in WHERE
View as plain text  
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
Thread
SUM in WHEREAhmad Al-Twaijiry18 Sep
  • AW: SUM in WHEREAndré Hänsel18 Sep
    • Re: SUM in WHEREAhmad Al-Twaijiry18 Sep
  • Re: SUM in WHEREEdward Macnaghten18 Sep
    • Re: SUM in WHEREAhmad Al-Twaijiry19 Sep
      • Re: SUM in WHEREAhmad Al-Twaijiry19 Sep
        • RE: SUM in WHERERandall Price19 Sep
          • Re: SUM in WHEREAhmad Al-Twaijiry19 Sep
  • Re: SUM in WHEREFelix Geerinckx21 Sep
    • Re: SUM in WHEREAhmad Al-Twaijiry24 Sep
RE: SUM in WHEREQuentin Bennett19 Sep
  • Re: SUM in WHEREDouglas Sims20 Sep
    • Re: SUM in WHEREAhmad Al-Twaijiry24 Sep
      • Re: SUM in WHEREDouglas Sims24 Sep
        • Re: SUM in WHEREAhmad Al-Twaijiry24 Sep
          • Re: SUM in WHEREDouglas Sims25 Sep
            • Re: SUM in WHEREAhmad Al-Twaijiry25 Sep
              • Re: SUM in WHEREPeter Brawley25 Sep
  • adding columns to a large tableHelen M Hudson25 Sep
    • Re: adding columns to a large tableEhrwin Mina25 Sep
      • RE: adding columns to a large tableMugunthan SIFY25 Sep
        • RE: adding columns to a large tableOw Mun Heng25 Sep
    • Re: adding columns to a large tablepraj25 Sep
  • Re: adding columns to a large tableHelen M Hudson25 Sep