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
Related
I have a database with a table containing the history of the login times of all accounts. The table includes columns USERID and LOGIN_DATE. I want to find those users who have not logged in for over 60 days, so an SQL query that says
Find users who have a login date which was greater than 60 days ago, but have no entry for any date more recently than 60 days ago
Can anyone suggest how I would do this ?
Following can be a solution
select USERID, min(login_date) mind, max(login_date) maxd
from Logins
group by UserId
having max(login_date) < dateadd(d,-60,getdate())
You could use aggregation, and filter on users whose maximum login date is older than 60 days:
select userid
from mytable
group by userid
having max(login_date) < current_date - interval '60' day
You did not tell which database you are using, so this uses standard date arithmetics. You might need to adapt that to your actual database (all major databases have alternatives for this).
Now the question was tagged Oracle. The above would work; you might want to truncate the time portion of the date to check on entire days. And if you want to display the last login, just add the aggregate function to the select clause:
select userid, max(login_date) last_login_date
from mytable
group by userid
having max(login_date) < trunc(current_date) - interval '60' day
You can use aggregation:
select userid
from t
group by userid
having max(logindate) < trunc(sysdate) - interval '60' day;
Date/time functions are notoriously database-specific, so the exact syntax for the having clause might depend on your database.
I want to get the users from a postgresql database where users activity is not seen for a specific period of time etc. (Basically I am trying to get which users who are not using the application at all)
For example the following SQL query is for users not using for the last 30 days:
SELECT distinct on (username) username, started_at
FROM projects_user JOIN projects_synclog
ON projects_user.id = projects_synclog.user_id
WHERE started_at BETWEEN '2019-08-15' AND '2019-09-15'
ORDER BY username, started_at DESC
In this query it is showing all the users which means for example a user may have logged in a month ago for once and again the same user has logged in 2 days ago. In this case, the user is still active, which I don't want to be listed out.
I have been trying this for countless hours. I searched for solutions a lot in here and other forums listed in google.
I would highly appreciate any help.
Thanks a lot.
I think you want aggregation and having. This answers the question in the title:
SELECT pu.username, max(sl.started_at)
FROM projects_user pu LEFT JOIN
projects_synclog sl
ON pu.id = sl.user_id
GROUP BY pu.username
HAVING MAX(sl.started_at) IS NULL OR
MAX(sl.started_at) < CURRENT_DATE - INTERVAL '7 DAY'
I have one list of events. One event name is creating an account and another is creating an account with Facebook. I am trying to see what percentage of accounts created use Facebook.
The code below will give me an accurate count of the number of facebook accounts and total accounts, but when I try to divide the two numbers it just gives me the number 1.
I am very new to SQL, and have spent hours trying to figure out why it is doing that to no avail.
with
fb_act as (
select *
from raw_event
where name = 'onboard_fb_success'
and event_ts::date >= current_date - 30
),
total_act as (
select *
from raw_event
where name ='create_account'
and event_ts::date >= current_date - 30
)
select count(fb_act)/count(total_act), total_act.event_ts::date as day
from total_act, fb_act
group by day
order by day
I expect the output to be about ~.3, but the actual output is always exactly 1.
Conditional aggregation is a much simpler way to write the query. You appear to be using Postgres, so something like this:
select re.event_ts::date as day,
(sum( (name = 'onboard_fb_success' and event_ts::date >= current_date - 30):: int) /
sum( name = 'create_account' and event_ts::date >= current_date - 30)::int)
) as ratio
from raw_event re
group by re.event_ts::date
order by day;
Im new to stack so please go easy on me. Ive looked all over the web and cant find anything that really helps me.
So I need to provide details of all regular academics working in the Computing Department who were
over 60 years old as of 31/12/2014.
my trouble comes with how would I approach showing data of someone 60+ could you minus one date from another date? or is there is possible sql command that I am missing.
my attempt:
SELECT *
FROM staff, department
WHERE DOB <= '31/12/1964'
AND staff.department_ID = department.department _ID
There are functions to calculate the difference between dates, but the most efficient is to first calculate the date that a person would be born to be 60 at 2014-12-31. That way you make a direct comparison to a value, so the database can make use of an index if there is one.
Example for Oracle:
select
PersonId, FirstName, LastName
from
Person
where
Born <= add_months(date '2014-12-31', -60 * 12)
Example for MySQL (eventhough you removed the MySQL tag):
select
PersonId, FirstName, LastName
from
Person
where
Born <= date_sub('2014-12-31' 60 year)
I think In SQL SERVER
Select Datediff(DAYS,'05-19-2015','05-21-2015')
In My SQL
SELECT TIMESTAMPDIFF(HOUR, start_time, end_time)
as difference FROM timeattendance WHERE timeattendance_id = '1484'
The oracle add_months function will help you.
where yourfield < add_months(date '1964-12-31', 60*12 )
I need help with an SQL query that will get me a count of hourly deposits to an account for a specific date. The database consists of deposits and time of deposit to various accounts. I need a query that will return the number deposits made to a certain account on an hourly basis. Ideally the result would look something like this:
MM/DD/YYYY Account XYZ :
12:00PM : 3
1:00PM : 5
2:00PM: 7
3:00PM; 11
*Formatting doesnt really matter, just as long as I can get this info.
Try:
DECLARE #CheckDate DATE
SET #CheckDate='2014/5/13'
SELECT DATEPART(hh, DespositDate), COUNT(1)
FROM table t
WHERE account = 'XYZ' AND CAST(DepositDate AS DATE)=#CheckDate
GROUP BY DATEPART(hh, DespositDate)
You can use the date/time functions for aggregation. In your case, this would look like:
select cast(DepositDate as date), datepart(hour, DespositDate), count(*)
from table t
where account = 'XYZ'
group by cast(DepositDate as date), datepart(hour, DespositDate);