开发者

SQL get users within range

开发者 https://www.devze.com 2023-03-15 04:11 出处:网络
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 wit

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

0

精彩评论

暂无评论...
验证码 换一张
取 消