Cumulative totals

  • Hi, I have a table following table

    DateVal
    6/1/2006A
    6/2/2006A
    6/3/2006A
    6/1/2006B
    6/2/2006B
    6/3/2006B
    6/1/2006A
    6/2/2006A
    6/3/2006A
    6/1/2006B
    6/2/2006B
    6/3/2006B

    I need following result set

    Val6/1/2006Total
    A26
    B26

    Is that possible?

    thanks in advance.

     

  • You could do a count(*), min(date), val and group by val.

    ******************
    Dinakar Nethi
    Life is short. Enjoy it.
    ******************

  • Not exactly what you want, but close:

    CREATE TABLE #test ([ID] INT NOT NULL IDENTITY(1,1) PRIMARY KEY,

     [Date] DATETIME,

     Val CHAR(1))

    INSERT INTO #test ([Date], Val)

    SELECT '2006-06-01', 'A'

    UNION ALL SELECT '2006-06-02', 'A'

    UNION ALL SELECT '2006-06-03', 'A'

    UNION ALL SELECT '2006-06-01', 'B'

    UNION ALL SELECT '2006-06-02', 'B'

    UNION ALL SELECT '2006-06-03', 'B'

    UNION ALL SELECT '2006-06-01', 'A'

    UNION ALL SELECT '2006-06-02', 'A'

    UNION ALL SELECT '2006-06-03', 'A'

    UNION ALL SELECT '2006-06-01', 'B'

    UNION ALL SELECT '2006-06-02', 'B'

    UNION ALL SELECT '2006-06-03', 'B'

    SELECT  t1.Val, COUNT(*) AS TotalByDate,  t1.[Date], t2.Total

    FROM #test t1

    INNER JOIN

    (SELECT Val, COUNT(*) AS Total

     FROM #test

     GROUP BY Val) t2

    ON t2.Val = t1.Val

    WHERE t1.[Date] = '2006-06-01'

    GROUP BY t1.Val, t2.Total, t1.[Date]

    DROP TABLE #test

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

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