how to get count of all records on monthly wise.

  • Hi ,

    how to get count of all records on monthly basis.

    Eg: i maintain 5 years old data in db..using the below function gives all records for the past 6 months.

    DATEADD(MONTH, -6, GETDATE()))

    But all i wanted is to get count of all records for each month individually for the latest 6 months.any idea on how to get it??

    Expected output

    ---------------

    Name-----code-------Jan--------Feb----Mar-------Apr----May-----Jun

    AAA-------111-------1000-------2348---1045------2579----3678---400

  • You can use PIVOT to get that kind of results.

    Can you provide a table script and some sample data? That would make helping you a lot easier.

    -- Gianluca Sartori

  • riys (6/10/2011)


    ...get count of all records for each month individually [font="Arial Black"]for the latest 6 months[/font].any idea on how to get it??

    Yep... Dynamic Cross-Tab. Please see the following article.

    http://www.sqlservercentral.com/articles/Crosstab/65048/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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