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?
SELECT *
FROM UserTable
WHERE DATEDIFF(day, DateJoined, DateUnSub) <= 30
http://msdn.microsoft.com/en-us/library/ms189794.aspx
What DBMS are you using? For MySQL:
select * from table where DATEDIFF(DateUnsub_, DateJoined_) <= 30
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
精彩评论