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: Tuesday, October 7, 2014 10:19 AM
Points: 4, Visits: 57
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: Yesterday @ 11:17 AM
Points: 1,116, Visits: 2,389
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 @ 4:17 PM
Points: 35,372, Visits: 31,920
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: Yesterday @ 11:17 AM
Points: 1,116, Visits: 2,389
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
Posted Tuesday, September 30, 2014 2:41 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 2:26 AM
Points: 257, Visits: 104
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)...
Post #1621125
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse