Let's say I have a table in MySQL DB with following columns
employee, status, work, start_date
Consider that start_date column is date and time.
If I do
SELECT employee, status, work, start_date from table_name WHERE DATE(date) >= CURDATE()-10
this will give me records from Current date - 10 days. In this case I might get 1 record to 100 records based on the data.
I need only 10 records based on date/time (e.g. if there are 10 employees that started to work today then I should get only today's records and not 10 days record)
How can I do that?
You mean you want the ten most recent entries? You can add an ORDER BY to set the order in which the results come back, and a LIMIT to reduce the total number of results.
SELECT employee, status, work, start_date from table_name
WHERE DATE(date) >= CURDATE()-10
ORDER BY date DESC
LIMIT 10
you need to use order by on start_date and limit
SELECT employee, status, work, start_date
from table_name
order by start_date desc
limit 10
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
Hi i am trying to get the count of records per day which i can do, but i also want the date to be show, for example,
Result
Date | Count
26/01/2015 20
25/01/2015 | 413
Here is an example of my data.
I would think this would work. Replace 'yourTable' with your table name
SELECT Date, COUNT(*) FROM yourTable GROUP BY Date;
Get the total count and group them by date.
SELECT `date`, COUNT(*) as Total
FROM `table`
GROUP BY `date`
ORDER BY `date`;
I have a query I am trying to formulate, but it keeps giving me unexpected results.
What I need to do is:
select all distinct values from my table where the unix timestamp is >= the last 24 hours, then order these results by which one has the highest amount of entries.
I have managed the time part:
SELECT DISTINCT(column_name) as myValue from table_name WHERE time_column >= unix_timestamp(CURRENT_TIMESTAMP, INTERVAL 1 DAY)
This works as expected. Then I was just going to use PHP to sort through the results etc etc, however I wish to use the power of SQL on this one.
Any ideas how I can extend the above query to encapsulate counting the amount of distinct column_name values? Also to then sort this in order dependent on how many values are in each one?
So essentially I want to get my results like so :
a unique ID | highest amount
a unique ID | second highest amount
a unique ID | lowest amount
I think you want to use group by for this query:
SELECT column_name as myValue, count(*) as cnt
from table_name
WHERE time_column >= unix_timestamp(CURRENT_TIMESTAMP, INTERVAL 1 DAY)
GROUP BY column_name
ORDER BY cnt;
I managed to figure it out, and in my case it works perfectly:
SELECT column_name, COUNT(*) as myValue
FROM table_name
WHERE time_column >= unix_timestamp(CURRENT_TIMESTAMP - INTERVAL 1 DAY)
GROUP BY column_name
ORDER BY myValue DESC
This gave me the 3 values which I expected. Which were the 3 values in the last 24 hours, and they were ordered by the amount of occurrences of this were in the database table - I tested this by manually creating another occurrence of these and checking at each stage. Worked a treat
I have a MySQL database with the following columns:
id (INT)
duration (FLOAT)
start_date (DATETIME)
I would like to get modal statistics (frequency counts) for the duration and start_date columns.
Sorting the duration of an event by frequency was straightforward:
SELECT duration, COUNT(duration)
AS frequency
FROM trips
GROUP BY duration
ORDER BY frequency
DESC;
If I run this same query on the start_date column, I get results of the form:
(datetime.datetime(2012, 8, 20, 15, 22), 6L)
(I'm using python to access the database). However, receiving my queries binned by the minute isn't necessarily helpful -- month, day of the week and hour are much more useful queries. I've tried changing the query to:
SELECT MONTH(start_date), COUNT(MONTH(start_date))
AS frequency
FROM trips
GROUP BY start_date
ORDER BY frequency
DESC;
However, if I do this then I start getting errors about my SQL syntax (Error 1064). I haven't been able to figure out what the error is, unfortunately.
I've considered putting the year, month, day of the week, hour, minute, etc. in different columns, but I feel that it should be possible to select the subfield of the datetime field within a query.
thanks!
Count frequency of start_date months:
SELECT YEAR(start_date), MONTH(start_date), COUNT(*) AS frequency
FROM trips
GROUP BY YEAR(start_date), MONTH(start_date)
ORDER BY frequency DESC;
Count frequency of durations:
SELECT duration, COUNT(*) AS frequency
FROM trips
GROUP BY duration
ORDER BY frequency DESC;
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