I have a table with columns user_id, time_stamp and activity which I use for recoding user actions for an audit trail.
Now, I would just like to get the most recent timestamp for each unique user_id. How do I do that?
SELECT MAX(time_stamp), user_id FROM table GROUP BY user_id;
The following query should be what you want...
select user_id,max(time_stamp) from yourtable group by user_id order by user_id, time_stamp desc
Related
I have table with users actions. One of them occurs every time when user opens certain page.
Table structure:
id, user_id, action_type, created_at...
I need to select from this table all actions per day/week... but without repeating of similar in one day. For example: user has visited 10 pages but 5 of them was the same. The result of selection should contain only unique pages per day.
Is it possible to do with only MySQL logic? Or better I should update repeated action if it occurs the same day?
One approach uses select distinct:
select distinct user_id, action_type, date(created_at) created_date
from mytable
If needed, you can also count how many times each action_type was met on a user_id and day basis with aggregation:
select user_id, action_type, date(created_at) created_date, count(*) cnt
from mytable
group by user_id, action_type, date(created_at)
I suggest the following SQL code :
SELECT DISTINCT URL
FROM table_name
GROUP BY date;
I assume that your table name is table_name, you have the URLs (pages) in the column named URL and you you track the date in the column named date;
My table has following data:
As you can see above table has user_id and created_at columns. I want to query data as shown below.
I used below query but it only data for 1 user at any date.
e user_id is not null
GROUP BY DATE(created_at);
[![enter image description here][3]][3]
As you can see, it shows data only for one user a time.
Please help.
Include user_id in group by clause to get data for each date and each user
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',
user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id;
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at), user_id;
Try to group with concat
SELECT `date`,`user_id`,COUNT(*) FROM ibiza_production.line_write_revenues GROUP BY CONCAT(`date`,'-',`user_id`) ORDER BY `date`,`user_id` LIMIT 50
Hi Can you try this and let me know if it helped you.
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id order by DATE(created_at);
I've a table cart which structure is following: id, item_id, session_id, date, num, so I need rows to be grouped by session_id column and sorted by date column in the same time, is it possible?
Yes it is possible.
... order by session_id, date
This will order firstly by session_id, and then by date. I believe that is what you want?
Try this:
SELECT * FROM YourTable
GROUP BY session_id ORDER BY date DESC;
Hope this helps!
You cant group by session_id and in the same time order by date.
You will get:
ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause
what you can do is:
SELECT [session_id],[date]
From TableName
group by [session_id], [date]
order by [date] desc
So I have a schema that goes something like this
Historical -- Table
--CID
--ID
--LOCATION
--STATUS
--TIME
CID doesn't matter for this, but I do want to get the 1 oldest TIME (TimeStamp) for each ID
So what I'm trying to do is something like
SELECT DISTINCT(id) from Historical order by TIME asc limit 1 But where I get each ID, and the oldest time for it.
Thanks
Does this do what you want?
SELECT id, min(time) FROM Historical GROUP BY id;
SELECT id, MIN(TIME) FROM Historical GROUP BY id;
This will select distinct IDs (by grouping) and the MIN TIME for each grouped ID.
All I want to count entries based on date.(i.e entries with same date.)
My table is
You can see 5th and 6th entry have same date.
Now, the real problem as i think is the same date entry have different time so i am not getting what I want.
I am using this sql
SELECT COUNT( created_at ) AS entries, created_at
FROM wp_frm_items
WHERE user_id =1
GROUP BY created_at
LIMIT 0 , 30
What I am getting is this.
I want entries as 2 for date 2012-02-22
The reason you get what you get is because you also compare the time, down to a second apart. So any entries created the same second will be grouped together.
To achieve what you actually want, you need to apply a date function to the created_at column:
SELECT COUNT(1) AS entries, DATE(created_at) as date
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE(created_at)
LIMIT 0 , 30
This would remove the time part from the column field, and so group together any entries created on the same day. You could take this further by removing the day part to group entries created on the same month of the same year etc.
To restrict the query to entries created in the current month, you add a WHERE-clause to the query to only select entries that satisfy that condition. Here's an example:
SELECT COUNT(1) AS entries, DATE(created_at) as date
FROM wp_frm_items
WHERE user_id = 1
AND created_at >= DATE_FORMAT(CURDATE(),'%Y-%m-01')
GROUP BY DATE(created_at)
Note: The COUNT(1)-part of the query simply means Count each row, and you could just as well have written COUNT(*), COUNT(id) or any other field. Historically, the most efficient approach was to count the primary key, since that is always available in whatever index the query engine could utilize. COUNT(*) used to have to leave the index and retrieve the corresponding row in the table, which was sometimes inefficient. In more modern query planners this is probably no longer the case. COUNT(1) is another variant of this that didn't force the query planner to retrieve the rows from the table.
Edit: The query to group by month can be created in a number of different ways. Here is an example:
SELECT COUNT(1) AS entries, DATE_FORMAT(created_at,'%Y-%c') as month
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE_FORMAT(created_at,'%Y-%c')
You must eliminate the time with GROUP BY
SELECT COUNT(*) AS entries, created_at
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE(created_at)
LIMIT 0 , 30
Oops, misread it.
Use GROUP BY DATE(created_at)
Try:
SELECT COUNT( created_at ) AS entries, created_at
FROM wp_frm_items
WHERE user_id =1
GROUP BY DATE(created_at)
LIMIT 0 , 30