I have a very basic table, consisting of an integer column and a timestamp column.
What's the query to count how many entries there are for each day?
When I use SELECT COUNT(*) FROM taps GROUP BY(DATE(time_stamp)) , I get the total number of rows int he table, rather than the number of rows for each DISTINCT date.
How do I need to modify the query?
Pretty straightforward.
SELECT
DATE(time_stamp),
COUNT(1)
FROM taps
GROUP BY DATE(time_stamp)
Related
I have a table with the following rows. id, address, timestamp. Timestamp is a unix timestamp. How can I get the day that has the most rows, and the number of rows in that day using 1 query. I am using MySQL 5.6.34.
Group the rows by date, use COUNT(*) to get the count of rows in each group, and then use ORDER BY and LIMIT to get the highest count.
SELECT DATE(timestampColumn) AS date, COUNT(*) AS count
FROM yourTable
GROUP BY date
ORDER BY count DESC
LIMIT 1
I have table_schedule(id,instructorNumber,time,date,cpNumber,user_id);
Is it possible to output the values of instructorNumber,time, etc. with the same highest occurence with same values?
sorry I am new in sql, so I am hoping that someone can help me to the query
Just group by all the fields you need for "same value comparison", order by count desc (so the result with most occurences will be first), and take first.
select
instructorNumber, time, date, cpNumber
from table_schedule
group by instructorNumber, time, date, cpNumber
order by count(*) desc
LIMIT 1
you may use this as a join on a main query if you want more than one result.
First group by the values you want to compare by and count. Get the maximum count (in MySQL you can use LIMIT for that). Then do the same group-by query again and take only the results HAVING the maximum count. Use GROUP_CONCAT to get a list of IDs in a string.
select instructorNumber, time, date, cpNumber, user_id, group_concat(id) as ids
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
having count(*) =
(
select count(*)
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
order by count(*) desc limit 1
);
When selecting data, I'm able to show how many rows were grouped from using GROUP BY by running:
SELECT id, created, COUNT(id) `count` FROM table
GROUP BY id
LIMIT 0,30
The count field easily outputs how many rows were affected by the GROUP BY. In MySQL is it possible to automatically not include any row which only has a count value of 1?
You should consider using a HAVING clause which lets you add conditions after the grouping has been done.
SELECT id, created, COUNT(id) cnt FROM table
GROUP BY id
HAVING cnt > 1
LIMIT 0,30
Another thing to mention is that the grouping might not be correct. If you group by id and you select id, created then the value for created is undetermined. MySQL will choose any of them for a given id if they differ. You might be interested in grouping by that field too or applying an aggregate function (eg: max(created)).
A second thing to mention is that COUNT(id) won't count the amount of rows but rather the amount of rows that have id not null. If id is never null then it would result in the same value as doing count(*).
Yes. You can use HAVING to limit the results of an aggregate function like COUNT (MAX,MIN,SUM, etc).
SELECT id, created, COUNT(id) `count`
FROM table
GROUP BY id
HAVING COUNT(id) > 1
LIMIT 0,30
Show the manual and find HAVING
... HAVING COUNT(id) > 1
http://dev.mysql.com/doc/refman/5.1/de/select.htmlhavinh
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
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;