1

I've got a database that stores when users subscribed and unsubscribed from a service. What I want to do is see who unsubscribed each month, and then see how many of those people had unsubscribed within 30 days of subscribing. I have two fields, DateJoined_ and DateUnsub_ that both return a smalldatetime. How would I be able to find these people using DateJoined and DateUnsub? I know I have to do some sort of calculation, and I could do this easily if I wasn't using SQL - any suggestions?

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
Hani Honey
  • 2,101
  • 11
  • 48
  • 76
  • What have you tried? What *can* you do in SQL? What particular difficulties do you have implementing this in SQL? – Andriy M Jun 23 '11 at 16:45
  • To be honest my biggest problem is that I don't have much knowledge of the library and didn't even know what to look for >.> – Hani Honey Jun 23 '11 at 16:56
  • What you will need apart from DATEDIFF is: [how to truncate a timestamp by a specific date/time part](http://stackoverflow.com/questions/2639051/what-is-the-best-way-to-truncate-a-date-in-sql-server) (because you'll need to group data by month); [how to group and how to aggregate data](http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server); [how to aggregate conditionally](http://stackoverflow.com/questions/1288058/conditional-count-on-a-field) (for it seems like you want to know the total number of users unsubscribed as well as the number of those unsubscribed within 30 days). – Andriy M Jun 23 '11 at 17:26

3 Answers3

2
SELECT *
FROM UserTable
WHERE DATEDIFF(day, DateJoined, DateUnSub) <= 30

http://msdn.microsoft.com/en-us/library/ms189794.aspx

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
1

What DBMS are you using? For MySQL:

select * from table where DATEDIFF(DateUnsub_, DateJoined_) <= 30
Christopher Armstrong
  • 7,907
  • 2
  • 26
  • 28
1

As for getting the number of users who unsubscribed each month, you could GROUP BY DATEPART(year, DateUnsub_), DATEPART(month, DateUnsub_) or instead limit on those dateparts to get the list of users.

http://msdn.microsoft.com/en-us/library/ms174420.aspx

Yuriy Zubovski
  • 128
  • 1
  • 6