Running Total cursor

  • I have two tables. ID table that has these fields

    Code_id,YearPeriod, ReachedDate fields. And table

    Amount that has Code_id,Amount,Date fields. What I

    need to do is write a cursor  that when the

    amount.Amount adds up to $100.00 it Updates the

    ReachedDate in the ID table to the amount.date fields

    value of that record. Any help would be greatly appreciated.

  • You can use a update cursor on the Amount table to do this.

    [EDIT]i mean update trigger [/EDIT]

  • You don't need cursor at all.

    DECLARE @TBL TABLE (

     PCT int,

     CDate datetime,

     GroupCol varchar(50))

    INSERT INTO @TBL

    SELECT 20, '2006-04-01', 'A'

    UNION

    SELECT 15, '2006-04-02', 'A'

    UNION

    SELECT 12, '2006-04-03', 'A'

    UNION

    SELECT 17, '2006-04-04', 'B'

    UNION

    SELECT 14, '2006-04-05', 'B'

    UNION

    SELECT 30, '2006-04-06', 'B'

    UNION

    SELECT 22, '2006-04-07', 'A'

    UNION

    SELECT 18, '2006-04-08', 'A'

    UNION

    SELECT 19, '2006-04-09', 'A'

    UNION

    SELECT 12, '2006-04-10', 'A'

    UNION

    SELECT 23, '2006-04-08', 'B'

    UNION

    SELECT 16, '2006-04-10', 'B'

    SELECT MIN(DT.CDate) as ReachedDate, DT.GroupCol

    FROM  (

     select T0.CDate , T0.GroupCol

     from @TBL T0

      inner join @TBL T1 on T0.CDate >= T1.CDate and T0.GroupCol = T1.GroupCol

     GROUP BY T0.CDate, T0.GroupCol

     HAVING SUM(T1.PCT) >=100

      ) DT

    GROUP BY DT.GroupCol

    _____________
    Code for TallyGenerator

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply