I have a table with multiple users that keeps track of application login date and time.
I am looking for a way to pull the last date they log on to the app 1 entry per user?
Column1 = userid
Column2 = date
I have try multiple examples but none are working as bellow:
select *
from
(select *
from ACCTRANS
ORDER BY TIMESTAMP DESC) AS x
GROUP BY
USERID
SELECT UserId, MAX(TIMESTAMP)
FROM ACCTRANS
GROUP BY UserId
Related
I have a table with the following columns
id | user_id | date
What i want to do is count the rows between a date range where the user_id has a row before that date range.
So for example if this table was tracking user log ins i want to count the users that have logged in this month, who have previously logged in before this month, essentially ignoring new users who joined this month.
Ideally a single query, any help appreciated.
Thanks
Try something like this:
SELECT Count(distinct user_id) usr_count
FROM userlogins u
WHERE
-- here we are getting those users for current period...
login_date between '2014-02-01' and '2014-02-28'
-- only where a record exists for that user in an earlier period
and exists (Select * From userlogins
where user_id = u.user_id and login_date < '2014-02-01');
This query works and provides me with the information I need, but it is very slow: it takes 18 seconds to agregate a database of only 4,000 records.
I'm bringing it here to see if anyone has any advice on how to improve it.
SELECT COUNT( status ) AS quantity, status
FROM log_table
WHERE time_stamp
IN (SELECT MAX( time_stamp ) FROM log_table GROUP BY userid )
GROUP BY status
Here's what it does/what it needs to do in plain text:
I have a table full of logs, each log contains a "userid", "status" (integer between 1-12) and "time_stamp" (a time stamp of when the log was created). There may be many entries for a particular userid, but with a different time stamp and status. I'm trying to get the most recent status (based on time_stamp) for each userid, then count the occurrences of each most-recent status among all the users.
My initial idea was to use a sub query with GROUP BY userid, that worked fast - but that always returned the first entry for each userid, not the most recent. If I could do GROUP BY userid using time_stamp DESC to Identify which row should be the representative for the group, that would be great. But of course ORDER BY inside of group does not work.
Any suggestions?
The first thing to try is to make this an explicit join:
SELECT COUNT(status) AS quantity, status
FROM log_table join
(select lg.userid, MAX( time_stamp ) as maxts
from log_table lg
GROUP BY userid
) lgu
on lgu.userid = lg.userid and lgu.maxts = lg.time_stamp
GROUP BY status;
Another approach is to use a different where clause. This will work best if you have an index on log_table(userid, time_stamp). This approach is doing the filtering by saying "there is no timestamp bigger than this one for a given user":
SELECT COUNT(status) AS quantity, status
FROM log_table
WHERE not exists (select 1
from log_table lg2
where lgu.userid = lg.userid and lg2.time_stamp > lg.time_stamp
)
GROUP BY status;
I am trying to count the number of new records for a given date only if the date of the record is the min(date) for the record owner. Here is the query I am trying to run:
SELECT COUNT(*)
FROM user_total_spends
WHERE user_id IN (SELECT user_id
FROM user_total_spends
WHERE MIN(DATE(date_posted)) = '2012-02-07')
AND merchant_location_id = '4f39b201-4a50-40ff-9cdf-cec51506eaf2'
AND date_posted = '2012-02-07';
Basically I am trying to say, if this is the first date this user/merchant is encountered, count it as a new user for this merchant.
When I run this I get a Invalid use of group function error. What am I missing?
you must use HAVING to filter a condition for a group of record or an aggregate function, so Instead of WHERE min(date(date_posted)) = '2012-02-07' try HAVING min(date(date_posted)) = '2012-02-07'
Try below:
SELECT count(*) FROM user_total_spends
WHERE user_id IN (SELECT user_id FROM user_total_spends GROUP BY user_id HAVING min(date(date_posted)) = '2012-02-07')
AND merchant_location_id = '4f39b201-4a50-40ff-9cdf-cec51506eaf2' AND
date_posted = '2012-02-07';
I have a MySQL table ScoreArchive with following fields:
ID (int), primary key
Date (date)
Score (int)
I record in the table the Score of each ID every day.
Now I wish to find the IDs that have the top score increase between, for example, 2011-04-22 and 2011-05-31.
How can I find these using a MySQL query?
Try something like:
select id, max(score) - min(score) as diff ... group by id order by diff desc
Edit (following up on the comment):
Or something like:
select id, final_scores.score - start_scores.score as diff
from (
select id, min(date) as min_date, max(date) as max_date
from scores
where date between ...
group by id
) as ranges
join scores as final_scores
on final_scores.date = ranges.min_date
join scores as start_scores
on start_scores.date = ranges.max_date
where ...
order by diff desc
SELECT score FROM ScoreArchive WHERE date BETWEEN 2011-04-22 AND 2011-05-31 ORDER BY score DESC;
That's how i would do it in pgsql i am guessing that mysql is the same
I have the database table logs as the following:
alt text http://www.freeimagehosting.net/uploads/16e974703a.jpg
I would like to extract the last entry of device, pollDate, status. For eg.
deviceId, pollDate, status
1, 2010-95-06 10:53:28, 1
3, 2010-95-06 10:26:28, 1
I tried to run the following query but the distinct only selects the first records, not the latest
SELECT DISTINCT deviceId, pollDate, status
FROM logs
GROUP By deviceId
ORDER BY pollDate DESC
alt text http://www.freeimagehosting.net/uploads/5d181103f8.jpg
So, could you please help me to extract the latest entries from the table? Thanks.
If (deviceID, poll_date) is unique, you can do the following:
SELECT *
FROM (
SELECT deviceid, MAX(poll_date) AS md
FROM logs
GROUP BY
deviceid
) q
JOIN logs l
ON l.deviceid = q.deviceid
AND l.poll_date = q.md