Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

3 month data count comparison Expand / Collapse
Author
Message
Posted Monday, August 18, 2014 2:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 7:51 AM
Points: 4, Visits: 56
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
Post #1604681
Posted Friday, September 5, 2014 9:56 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:12 AM
Points: 1,082, Visits: 2,310
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())
Post #1610944
Posted Friday, September 5, 2014 3:06 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 35,218, Visits: 31,677
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1611027
Posted Monday, September 8, 2014 11:34 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:12 AM
Points: 1,082, Visits: 2,310
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)
Post #1611523
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse