>Hi,
>
>I having trouble working out how to get a result set similar to the 
>following where I select from a table with Date & Sales column.
>
>My specific question is can I have a column that accumulates values, 
>if so could I have some guidance on how to express this in a select 
>statement please.
>
>
>+----------+-----------+-----------+
>| Month    | Sales     | Cum Sales |
>+----------+-----------+-----------+
>| Jan      | 1000      | 1000      |
>| Feb      | 1500      | 2500      |
>| Mar      | 1200      | 3700      |
>| April    | 1400      | 5100      |
>+----------+-----------+-----------+
>
>Many thanks in advance.
>
>Richard

Sir, at times like this it's handy to have a copy of 'SQL for 
Smarties'. The following is taken from one of the examples, with only 
the table and column names changed.

    SELECT s1.mnd, s1.sales, Sum(s2.sales) AS Cum_sales
    FROM sales AS s1, sales AS s2
    WHERE s2.mnd <= s1.mnd
    GROUP BY s1.mnd;

Note that I used mnd instead of Month. Month is a function name, so 
you probably don't want to use it for a column name.

I used a date instead of a month name to make the WHERE clause work, 
i.e. the mnd column contained 2001-1-1, 2001-2-1, 2001-3-1, 2001-4-1, 
instead of Jan, Feb, etc.

I ran the statement above on the MS Titanic (aka my Wintel box) and 
it worked fine.

Bob Hall

Know thyself? Absurd direction!
Bubbles bear no introspection.     -Khushhal Khan Khatak

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to