mysql query: how to select distinct based on time - mysql

I have a table that looks something like this
|ID|TIME-|
|01|01:15|
|01|01:30|
|02|01:35|
|02|01:36|
|02|05:30|
|02|10:30|
|01|12:30|
What I want is to select distinct id only if they fall within the same hour mark, so the result I'm looking for is:
|ID|TIME|
|01|1:15|
|02|1:35|
|02|5:30|
|02|10:30|
|01|12:30|
TIME is in mysql date format.
how do I write this query?
Thanks!

Asuminig that TIME is a timestamp or datetime
SELECT ID, TIME
FROM yourtable
GROUP BY ID, floor(TIME/3600)
this is mysql specific
SELECT ID, TIME
FROM yourtable
GROUP BY ID, TIME DIV 3600

You could do this. This will give you one record for each ID, hour showing the first time value for the given hour.
SELECT ID, TIME
FROM <yourtable>
GROUP BY ID, HOUR(TIME)
ORDER BY ID, TIME

Related

MySQL select only one record from similar per day

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;

MySQL COUNT() ? Date And Frequency

Supposed I have a SQL table that looks like this
Now I am suppose to do this 'logic' so that I know that on 23/6/2017, the word 'accessories' appeared 2 times and 'tools' appeared 1 time.
I think there is some kind of way to do this is mysql, something along the lines of COUNT() and GROUPBY but I cannot get the result I want.
Appreciate any guidance. Thanks!
You can do this by using GROUP BY :
SELECT date, category, count(*) as count FROM table_name GROUP BY date, category
You have to put every selected columns after group by otherwise it will show query error.
SELECT date, category, count(*)
FORM yourtable
GROUP BY date, category
It's as simple as counting the categories corresponding to each date
SELECT date
category
COUNT(*)
FROM table_name
GROUP BY date, category

MySQL Query: Get new customers in the month

I have millions of records in a table with following columns:
id, item_id, customer_id, date
I want to find customer_id for a specific month which are inserted first time in the table in that month.
What is best and simple query for this.
Thanks
select customer_id , min(date) as min_date
from theTable
group by customer_id
having min_date>=<the desired date>
try something like:
SELECT
date_format(date,"%M") month,group_concat(customer_id ORDER BY customer_id) customer_ids
FROM
<table_name>
GROUP BY month;
Something like this may be:
select distinct(id) from customer
where month(date)='Your_month'
order by date

MySql select statement (select distinct) issues

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.

SQL Row grouping

I've a table like (I am omitting unnecessary columns)
id:int | name:string | ts:DateTime
There are multiple entries. now What I want in my resultset is
date:Date | entries:int
e.g. how many entries were made on all dates. actually I gonna make a chart of it.
What SQL Query I need to use for this ? I can create a view of it
This should do the job:
SELECT
DATE(ts) AS date,
COUNT(*) AS entries
FROM table
GROUP BY date
You can try
SELECT ts AS `date`, COUNT(id) AS entries
FROM your_table
GROUP BY ts
If you just need date part, then try
SELECT DATE(ts) AS `date`, COUNT(id) AS entries
FROM your_table
GROUP BY DATE(ts)
To strip off the time part of your timestamps, use DATE(). Sort by the date, ascending to get them in the right order. Note that this will omit dates for which there are no entries. If you need to fill those in, it becomes somewhat more complicated.
SELECT
DATE(ts) AS date,
COUNT(*) AS entries
FROM tbl
GROUP BY DATE(ts)
ORDER BY DATE(ts) ASC