running totals

  • Can anyone tell me how to do running totals in Transact SQL in an efficient way.

    TIA

    Murali

  • Have you looked at the ROLLUP command. Here is a sample query that display sub_totals and grand_total:

    use pubs

    select

    case when stor_id is not null and title is null then ''

    when stor_id is null then ''

    else cast(stor_id as char(11))

    end as stor_id,

    case when stor_id is null and title is null then 'Grand Total'

    when title is null then 'Sub Total'

    else title

    end as title,

    count(*) as 'Total Sales'

    from sales s join titles t

    on s.title_id = t.title_id

    group by stor_id, t.title

    with rollup

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

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

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