I want to create a table in mysql the table has 3 columns user_id,activity - online or offline - and timestamp,
Now I want the table to show all user_id's along with most recent status. Currently using
SELECT DISTINCT user_id,activity FROM activity ORDER BY timestamp DESC
statement shows when the user was last online and offline which is not what I want.
Try this:
select act.*
from activity as act
inner join (
select user_id, max(timestamp) as max_ts
from activity
group by user_id) as a on act.user_id=a.user_Id and act.timestamp=a.max_ts
This will return the most recent record for each user.
select user_id, max(timestamp) ts from activity a
inner join
(select user_id, activity, max(timestamp) ts
group by user_id, activity) iam
on iam.user_id =a.user_id and a.ts = iam.ts
Related
I am getting this error but I don't understand why my query doesn't work. Can someone please give me a hand?
The question is this:
SELECT activity
FROM Friends
GROUP BY activity
HAVING COUNT(activity) > MIN(COUNT(activity))
AND COUNT(activity) < MAX(COUNT(activity))
My idea is that as long as the count of the activity is larger than the activity that has the minimum count and less than the activity that has the maximum count, it should be returned. But I am having "Invalid use of group function" error which I don't understand. One possible thing that I could think of is that the parts that I am comparing with the COUNT(activity) have to be a number that is selected from the table instead of a part that has "MIN" or "MAX". But I don't understand why as they both look like the same number to me.
If your version of MySql is less than 8.0, the following will work:
select activity, count(*) as cnt from Friends group by activity
having cnt not in (
select max(cnt) as cnt from (
select activity, count(*) as cnt from Friends group by activity
) sq1
union
select min(cnt) as cnt from (
select activity, count(*) as cnt from Friends group by activity
) sq2
);
activity
cnt
Singing
2
View on DB Fiddle
Since the above SQL references the same select 3 times, namely select activity, count(*) as cnt from Friends group by activity, you might consider creating a quasi-temporary table (but there is the overhead in creating such a table to consider -- for the number of rows you actually presented, this would run more slowly):
create table t as select activity, count(*) as cnt from Friends group by activity;
select activity from t where cnt not in (
select max(cnt) as cnt from t
union
select min(cnt) as cnt from t
);
drop table t;
activity
Singing
View on DB Fiddle
If you just want data from friends table then you can use the analytical function (My sql 8.0 or higher) as follows:
select activity from
(SELECT activity, dense_rank() over (order by count(*)) as rn_asc,
dense_rank() over (order by count(*) desc) as rn_desc
FROM Friends
GROUP BY activity)
where rn_asc <> 1 and rn_dsc <> 1
I have a table with more than 50k entries and a few users:
transactions Table:
ID
USER
VALUE
TIMESTAMP
users Table:
USER
TYPE
REGION
I would like to get the most recent transactions for each user. So far I am using the following GROUP BY statement, but it is slow (takes 5-10sec approx):
select ID , max(TIMESTAMP) as TIMESTAMP from transactions group by USER;
Is there a faster statement to retrieve the most recent entries?
First of all as per my understanding ID columns should keep unique values in transaction table so group by should be on USER field as per your requirement.
Further you can try below query. I am not sure but you can compare its time with your query and use accordingly.
SELECT
USER , TIMESTAMP
FROM
(SELECT USER,TIMESTAMP FROM transactions ORDER BY ID DESC) a
GROUP BY USER;
Assuming there will be an index on USER column as this is common between both tables.
Try this,
select u.*,tr.* from users as u
outer apply
(
select top 1 * from transactions as t
where t.USER= u.USER
order by t.ID desc
)as tr
I have a table with the following columns; id, post_id, status, and datetime. Every time a post(post_id) is updated, a row is inserted with the latest status and datetime timestamp. I am created a pie chart of by status. Therefore I must first SELECT the latest entry of a post (ignoring all past updates), then COUNT how many rows are returned and group by status. What does my query look like?
SELECT status, COUNT(*) AS statusCnt
FROM inspections
WHERE id IN (SELECT MAX(id) FROM inspections GROUP BY post_id)
GROUP BY status
Untested alternative:
SELECT i1.status, COUNT(i1.*) AS statusCnt
FROM inspections i1
JOIN (
SELECT MAX(i2.id) AS maxID FROM inspections i2 GROUP BY i2.post_id
) AS innerTbl ON i1.id = innerTbl.id
GROUP BY i1.status
SELECT count(*) FROM (SELECT *,MAX(time) FROM table GROUP BY post_id) As b
As per my interpretation of your question, query should be like this
I have a query like this, to select the most recent time someone was contacted:
SELECT `user_id`, `last_contact`
FROM `emails_sent`
group by `user_id`
order by `last_contact` desc
The above code gives a table with the last contact time for each user. Now, I have another table with contacts to users, a table with columns user_id and last_contact, among others.
How can I make my select use both tables and select the last contact time for each user from the two tables?
Summarize the union of two summary queries, something like this.
SELECT user_id,
MAX(user_date) user_date
FROM
(
SELECT user_id,
MAX(last_contact) user_date
FROM emails_sent
GROUP BY user_id
UNION ALL
SELECT whatever_user_id_column user_id,
MAX(whatever_date_column) user_date
FROM whatever_table
GROUP BY user_id
)a
GROUP BY user_id
I need help returning a relevant result for this query. I have one table that I am hitting with three columns. trans_date, trans_amount and user_id
what I am trying to determine is this. For a given user_id when was the last trans_date and what was the trans_amount.
I'm having trouble returning the correct transaction_amount. Here is my code so far. It's returning the correct date but the amount is not right
select user_id, trans_date, trans_credit
from table
WHERE trans_credit =
(select max(trans_date) from inclick_account_act as f
where f.user_id = table.user_id);
Thanks in advance
If I understand you correctly you just want to get the most recent transaction for all users.
SELECT user_id, trans_date, trans_credit
FROM `table`
GROUP BY user_id
ORDER BY trans_date DESC;
How about something like
SELECT t.*
FROM table t INNER JOIN
(
SELECT user_id,
MAX(trans_date) max_trans_date
FROM table
GROUP BY user_id
) MaxDates ON t.user_id = MaxDates.max_trans_date