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.
Related
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
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
I would like to determine two things from a single query:
Most prevalent column in a table
The amount of times such column was located upon querying the table
Example Table:
user_id some_field
1 data
2 data
1 data
The above would return user_id # 1 as being the most prevalent in the table, and it would return (2) for the total amount of times that it was located in the table.
I have done my research and I came across two types of queries.
GROUP BY user_id ORDER BY COUNT(*) DESC
SUM
The problem is that I can't figure out how to use these two queries in conjunction with one another. For example, consider the following query which successfully returns the most prevalent column.
$top_user = "SELECT user_id FROM table_name GROUP BY user_id ORDER BY COUNT(*) DESC";
The above query returns "1" based on the example table shown above. Now, I would like to be able to return "2" for the total amount of times the user_id (1) was found in the table.
Is this by any chance possible?
Thanks,
Evan
You can include count(*) in the SELECT list:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC;
If you want only the first one:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC LIMIT 1;
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
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