3 month data count comparison

  • Hello all,

    So I am trying to figure out a way to present the number of records for a 3 month window and for the same date. I know not all months have 31 days so it would need to be smart enough so it does not error out. This is kinda the manual way to help you understand what I am looking for:

    select count(learner_id) TheCount, month(creation_date) TheMonth from learner_info

    where creation_date between '8/1/2014' and '8/18/2014'

    or creation_date between '7/1/2014' and '7/18/2014'

    or creation_date between '6/1/2014' and '6/18/2014'

    group by month(creation_Date)

    Keep in mind, tomorrow all three dates will need to be the 19th and the next day the 20th and so on.

    Thank you in advance.

    Kam

  • Give this a try: https://www.katieandemil.com/t-sql-first-day-of-previous-month?tab=article

    put these in variables:

    SELECT DATEADD(m,-1, Dateadd(d,1-DATEPART(d,getdate()),GETDATE()))

    SELECT DATEADD(m,-2, Dateadd(d,1-DATEPART(d,getdate()),GETDATE()))

    SELECT DATEADD(m,-3, Dateadd(d,1-DATEPART(d,getdate()),GETDATE()))

    SELECT DATEADD(m,-1, GETDATE())

    SELECT DATEADD(m,-2, GETDATE())

    SELECT DATEADD(m,-3, GETDATE())

  • khott (8/18/2014)


    Hello all,

    So I am trying to figure out a way to present the number of records for a 3 month window and for the same date. I know not all months have 31 days so it would need to be smart enough so it does not error out. This is kinda the manual way to help you understand what I am looking for:

    select count(learner_id) TheCount, month(creation_date) TheMonth from learner_info

    where creation_date between '8/1/2014' and '8/18/2014'

    or creation_date between '7/1/2014' and '7/18/2014'

    or creation_date between '6/1/2014' and '6/18/2014'

    group by month(creation_Date)

    Keep in mind, tomorrow all three dates will need to be the 19th and the next day the 20th and so on.

    Thank you in advance.

    Kam

    I don't know if your creation_date column has times in it but remember that BETWEEN is INCLUSIVE of both end points.

    --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)

  • Also, if you are looking to dates to start at 12:00:00 AM, for the times you will need to use the convert and cast date/time styles. Ex. select CONVERT(varchar(10),DATEADD(MM,-1,GETDATE()-DATEPART(dd,GETDATE())+1),101)

  • Assuming time part is not to be considered and that you need this for only 3 date spans (if you need for more one could Recursive CTE for this) :

    -- The two dates for first condition

    select DATEADD(m,DATEDIFF (m,'19000101',GETDATE()),'19000101')

    select GETDATE()

    -- The two dates for second condition

    select DATEADD(m,-1,DATEADD(m,DATEDIFF (m,'19000101',GETDATE()),'19000101'))

    select DATEADD(m,-1,GETDATE())

    -- The two dates for third condition

    select DATEADD(m,-2,DATEADD(m,DATEDIFF (m,'19000101',GETDATE()),'19000101'))

    select DATEADD(m,-2,GETDATE())

    The only variable in these 6 dates is getdate() (which of course can be replaced with any other date u want)...

    "The price of anything is the amount of life you exchange for it" - Henry David Thoreau

Viewing 5 posts - 1 through 4 (of 4 total)

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