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;
Related
I'm trying to understand the right way to divide the count sums from two queries.
I'm teaching myself sql and practising it on line.
Question:
Write a SQL query to find the cancellation rate of requests made between 2017-08-01 and 2017-08-03. The cancellation rate is given by dividing the number of cancelled requests by the total number of rides each day. The result table should have 2 Columns, namely Day that shows each day and Cancellation Rate that provides the cancellation rate of that day.
Table is:
What I tried was:
count cancelled ride rates per date
count all ride requests per date
divide both the counts per date
with
cancelled_rides as
(select count(*) cancel_count, status, Request_id
from TRIPS
where status = 'cncld_driver'
group by state, Request_id)
all_rides as (
select count(*) day_count, status, Request_id
from TRIPS
group by state, Request_id) ,
select cancelled_rides.Request_id as DAY,
(cancelled_rides.cancel_count/all_rides.day_count) as 'Cancellation Rate'
FROM cancelled_rides, all_rides;
Does this look right? Note I purposefully ignored including date ranges as the table has only limited entries.
I do not see that a CTE helps at all for this query. Just use conditional aggregation:
select t.Request_id as day, count(*) as total,
sum( status = 'cncld_driver' ) as num_cancelled,
avg( status = 'cncld_driver' ) as cancellation_rate
from trips t
where request_id >= '2017-08-01' and
request_id < '2017-08-04'
group by request_id;
Calling a date "request_id" is rather confusing. You should have a request id that is unique for each row and a separate column with the date/time.
I am learning Charts in Laravel, i need to draw Line graph for daily Student Attendance for those students come late or on time. I tried to write MYSQL query but it doesn't work
I tried subquery on same table to get data for daily students and i also need 7 dates only not full date, like date is stored in db as 09/08/2019 but i need it as 08 as date.
SELECT Date, COUNT(*) AS TimeStudent
FROM attendance WHERE `Attendance`='OnTime' AND (SELECT COUNT(*) AS
LateStudent FROM attendance
WHERE `Attendance`='Late'
GROUP BY `Date`
ORDER BY LateStudent DESC)
GROUP BY `Date`
ORDER BY TimeStudent DESC
but i got
[Err] 1241 - Operand should contain 1 column(s)
, because i can't use to fetch Date again in subquery while use it after where clause. Any one help me plz.
Here is a way to aggregate based on column value.
This query will give you count of on time and late student for a particular date.
SELECT
`Date`,
DATE_FORMAT(`Date`, '%d') AS Month_Date, -- You can modify it as per your requirement
SUM(IF(`Attendance` = 'OnTime', 1, 0)) AS OnTime_Count,
SUM(IF(`Attendance` = 'Late', 1, 0)) AS Late_Count
FROM attendance
WHERE `Date` >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY `Date`;
There is a task: develop a fragment of the Web site that provides work with one table.
Attributes of the table:
Day of the week,
Time of the beginning of the lesson,
Subject name,
Number of the audience,
Full name of the teacher.
We need to make a query: determine the day of the week with the largest number of entries, if there are more than one maximum (ie, they are the same), then output them all. I did the query as follows:
SELECT COUNT (*) cnt, day
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1;
But if there are several identical maxima, then only one is displayed. How to write a query which returns them all?
You can use your query as a subquery in the HAVING clause, e.g.:
SELECT day, count(*) as cnt
FROM schedule
GROUP BY day
HAVING count(*) = (
SELECT count(*) as cnt
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1
)
ORDER BY day
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
I have a MySQL database populated with power consumption over 20 years.
I want to query the average of the power consumption over every month, from a given month.
For example with this database,
date power_consumption
2014/03/30 30
2014/04/30 40
2014/05/30 50
2014/06/30 20
The result would be, from 2014/04
month average_so_far_from_april_2014
2014/04 40.0
2014/05 45.0
2014/06 36.667
If I cannot achieve this in one query, what query should I go for to retrieve the most useful data for this task? (My naive approach is to query the whole table out and calculate the average in my application.)
Join the table of sub query against the consumption table which gets the unique months against the table of consumption, with a join condition that the year / month is less than or equal to the one from the sub query and use the AVG aggregate function on the power consumption from the table grouped by the year / month
Something like this:-
SELECT consumption_month,
AVG(b.power_consumption)
FROM
(
SELECT DISTINCT DATE_FORMAT(`date`, '%Y%m') AS consumption_month FROM consumption_table a
) a
INNER JOIN consumption_table b
ON consumption_month >= DATE_FORMAT(b.`date`, '%Y%m')
WHERE b.`date` >= '2014/04/01'
GROUP BY consumption_month
SQL fiddle:-
http://www.sqlfiddle.com/#!2/16588/2
If you only had one record per month you could simplify it more by just doing a join of the table against itself without the need for the sub query.
GROUP BY is for this kind of problems. The average is calculated for each distinct value of the expression the the GROUP BY clause.
SELECT DATE_FORMAT(date, '%Y/%m'), AVG(power_consumption)
FROM table_name
WHERE date > ...
GROUP BY DATE_FORMAT(date, '%y/%m')
ORDER BY DATE_FORMAT(date, '%y/%m')
You get the average for each month, DATE_FORMAT(date, '%y/%m') is year and month in format YYYY/MM