SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


3 month data count comparison


3 month data count comparison

Author
Message
khott
khott
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 71
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
Michael_Garrison
Michael_Garrison
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1326 Visits: 2754
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())
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85684 Visits: 41087
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Michael_Garrison
Michael_Garrison
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1326 Visits: 2754
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)
Mohit Dhiman
Mohit Dhiman
Old Hand
Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)

Group: General Forum Members
Points: 367 Visits: 260
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search