Help with SQL Query !

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • russzee

    Help with SQL Query !

    I have a table with the following columns

    ---------------------------------------------
    Payment Payment_Date
    --------------------------------------------
    900 5/15/05
    900 615/05
    900 7/15/05
    900 8/15/05
    900 9/15/05
    -------------------------------------------

    I also have a predetermined value @total = 3600

    I need to write a SQL query where it returns the Payment_Date as soon
    as SUM(Payment) > 3600.

    So , in this case the returned result would be 9/15/05

    I know I can do this via cursors but i was wondering if there is a sql
    query which can accomplish this.


    TIA
    - Mr. Z

  • MC

    #2
    Re: Help with SQL Query !

    Is this good enough?

    select top 1 Payment_date,
    (select sum(Payment) from testsum where Payment_date <= t1.Payment_date )
    from testsum t1
    where (select sum(Payment) from testsum where Payment_date <=
    t1.Payment_date )>3600
    order by Payment_date

    -----

    select top 1 t1.Payment_date , sum(t2.Payment) as suma
    from
    testsum t1
    left join testsum t2 on t1.Payment_date >= t2.Payment_date
    group by t1.Payment_date
    having sum(t1.Payment) >3600
    order by Payment_date




    MC


    "russzee" <trusst_3@hotma il.com> wrote in message
    news:1133923717 .618716.252890@ g44g2000cwa.goo glegroups.com.. .[color=blue]
    >I have a table with the following columns
    >
    > ---------------------------------------------
    > Payment Payment_Date
    > --------------------------------------------
    > 900 5/15/05
    > 900 615/05
    > 900 7/15/05
    > 900 8/15/05
    > 900 9/15/05
    > -------------------------------------------
    >
    > I also have a predetermined value @total = 3600
    >
    > I need to write a SQL query where it returns the Payment_Date as soon
    > as SUM(Payment) > 3600.
    >
    > So , in this case the returned result would be 9/15/05
    >
    > I know I can do this via cursors but i was wondering if there is a sql
    > query which can accomplish this.
    >
    >
    > TIA
    > - Mr. Z
    >[/color]


    Comment

    Working...