Results 1 to 4 of 4

Thread: Sql Tricks

  1. #1
    Join Date
    Sep 2003
    Posts
    4

    Question Unanswered: Sql Tricks

    Hi All,

    I need a sql which can display a computed column as shown below

    Table

    Period sales
    1 1000
    2 1500
    3 2000

    Expected Result

    Period Sales YTD
    1 1000 1000
    2 1500 2500
    3 2000 4500

    Please help!!!
    Last edited by sbabu; 09-15-03 at 22:29.

  2. #2
    Join Date
    Oct 2001
    Location
    Naples, FL
    Posts
    273
    Try this

    SELECT a.period,a.sales,sum(b.sales) as running_total
    FROM table a, table b
    WHERE a.period <= b.period
    GROUP by a.period,a.sales
    ---------------
    Ray Higdon MCSE, MCDBA, CCNA

  3. #3
    Join Date
    Sep 2003
    Posts
    4

    Smile Sql Trick

    Thank you so much!!!!!
    Last edited by sbabu; 09-16-03 at 10:42.

  4. #4
    Join Date
    Sep 2003
    Posts
    8

    Re: Sql Trick

    Try this:

    SELECT distinct
    a.period,
    a.sales,
    isnull( (select sum(b.sales) from table b where b.period <= a.period) ,0) as running_total
    FROM table a

    I hope this helps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •