How to count number of date between two days MySQL - mysql

On my mysql table I keep date as datetime. (eg: 2013-11-09 11:15:58), I want to write mysql Query to get number of dates to given date. can someone help me to write this..
eg:
Given date: 2014-01-19 10:15:15
On table 'date_added' has: 2013-11-09 11:15:58
is it possible to get number of dates using mysql query, like COUNT(), SUM()

I think your using TIMESTAMP to use this method
SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01') from tablename;
if you want to count following this code:
SELECT COUNT(*) FROM a1 WHERE '2014-03-01';

To find number of records on a specific date of given timestamp, use date_format or date functions on the field to filter by date.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date_format( date_added, '%Y-%m-%d' );
Alternatively, you can also use date function to extract date part from a datetime column.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date( date_added );
And if you want to count based on date part of an input datetime value then, use
select
date( '2014-01-19 10:15:15' ) dt,
count( date_added ) dt_cnt
from my_table
where
date( date_added ) = date( '2014-01-19 10:15:15' );
Refer to:
DATE(expr)
Extract the date part of a date or datetime expression
DATE_FORMAT(date,format)
Format date as specified

Related

Format a string to a date in MySQL query

I have a table as shown below, the date column is a string(MMMM-yyyy).
I want to select the latest row for an ID. For ID # 2, the latest date would be August-2020 with the price of 45.40
ID Date Price
2 August-2020 45.40
2 July-2020 42.30
I've tried the format(date, 'MMMM-yyyy) as formatted date and STR_TO_DATE(date, '%MMMM-%yyyy') but I can't get it to convert to a date so I can order the column. I want to keep the date in this format, I just need to order it for my query.
Use str_to_date() to convert the string to a date; a little trick is needed to make the original value a complete date string before conversion, so:
str_to_date(concat(date, '-01'), '%M-%Y-%d')
To filter the table on the latest date per id, you would do:
select t.*
from mytable t
where date = (
select date
from mytable t1
where t1.id = t.id
order by str_to_date(concat(date, '-01'), '%M-%Y-%d') desc
limit 1
)
You need to proper format specifiers for MySQL. I would expect this to work:
select STR_TO_DATE('August-2020', '%M-%Y')
However, it appears that you need:
select str_to_date(concat('01-', 'August-2020'), '%d-%M-%Y')
Here is a db<>fiddle.

Mysql. Change date format output on GROUP BY DAY query

I execute this query
SELECT * FROM graph WHERE ean IN ('00000000166330') group by DAY(created_at);
Getting those results:
# id, ean, avg_price, created_at
'58', '00000000166330', '2799.0000', '2020-06-11 16:43:27'
I want to change the date format returned of the created_at field.
I would like to get only the date, not the hour, and with the format: Day, month, Year.
My guess is that DATE_FORMAT should be used, but how to use it, grouping also by day?
Example here
You are not doing any aggregation, so remove group by
You can replace the operator IN with = because you are comparing against 1 value only
Use the function DATE() to get only the date part from created_at
You need a correlated subquery in the WHERE clause to get the row with the minimum id (since it does not matter whic row will be returned) of each day:
SELECT g.id, g.ean, g.avg_price, DATE_FORMAT(g.created_at, '%d-%m-%Y') created_at
FROM graph g
WHERE g.ean = '00000000166330'
AND g.id = (SELECT MIN(id) FROM graph WHERE ean = g.ean AND DATE(created_at) = DATE(g.created_at))
See the demo.
If you want distinct values without aggregation function you should use DISTINCT and for date you can use the date_format() function
SELECT DISTINCT DAY(created_at)
, date_format(date(created_at),'%d, %m, %Y') , id, avg_price
FROM graph WHERE ean = '00000000166330';
and when you have only a value you should use = and not IN operator.

Get SUM of values in a column after query is ran

This is my SQL statement which pulls back all the fills I've had in a certain time frame. Is there a way to get the list to come up and also pull the SUM of all of them?
SELECT customerName, date, gallons
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
ORDER BY CONVERT(DATE, date) ASC
First, I would write your query as:
SELECT customerName, date, gallons
FROM addFill
WHERE date >= '2015-03-03'
ORDER BY date ASC;
I see no value in ordering by the date and not the date/time component. Also, you might as well just use a recognizable date format for the comparison.
If you want the sum as well, then that is tricky. One method uses rollup:
SELECT customerName, date, SUM(gallons) as gallons
FROM addFill
WHERE date >= '2015-03-03'
GROUP BY customerName, date with rollup
sums of fills per customer since '3-3-2015'
SELECT customerName, date, sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
GROUP BY customerName
sum of fills for all customers since '3-3-2015'
SELECT sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )

Count on curdate() in mysql

I have a user_entry table which contains a date field. data type is datetime.
data base is mysql.
I want a count of current date and current month and all data of current date.
How can I get this?
I tried below query but it's not working.
select * from table
where DATE(date) = CURDATE()
SELECT date FROM `test` WHERE date = CURDATE()
or
SELECT date FROM `test` WHERE date = DATE('2016-04-04')
it's work.
if you want the number of matches:
SELECT COUNT(date) from test WHERE date = CURDATE()
What is the data type of field 'date'?
To obtain the DAY/MONTH you can use the corresponding functions
SELECT MONTH(date), DAY(date) from test
Moreover, you can use groups to create a complete report
SELECT COUNT(date), date from test GROUP BY DAY(date), MONTH(date)
i used below query and it works for me.
SELECT *
FROM `user_entry`
WHERE DATE( DATE ) = DATE( NOW( ) )

How to resolve issue in order by date

SELECT COUNT(patient_id) AS idpateint,
patient_id
FROM patient
WHERE STR_TO_DATE(date_enter,'%d/%m/%Y' )
BETWEEN STR_TO_DATE( '$repeat','%d/%m/%Y' ) AND STR_TO_DATE('$to','%d/%m/%Y')
AND patient_type='opd'
AND patient_id ='$idpatient1'
ORDER BY STR_TO_DATE(date_enter,'%d/%m/%Y' )
Actually I saved date in dd/mm/yyyy format in db. Order by is not working. No SQL error but date is not coming in order.
Here is your query:
select count(patient_id) as numpatients, patient_id
from patient
where STR_TO_DATE(date_enter, '%d/%m/%Y' ) between STR_TO_DATE('$repeat', '%d/%m/%Y' ) and
STR_TO_DATE('$to', '%d/%m/%Y') and
patient_type = 'opd' and
patient_id = '$idpatient1'
order by STR_TO_DATE(date_enter, '%d/%m/%Y' )
You have a count() in the select. This turns the query into an aggregation query, so it only returns one row. In addition, you are selectxing only a single patient id. I could imagine that you want the counts by date, because you are so focused on date.
The following will give counts by date, regardless of the patient:
select STR_TO_DATE(date_enter, '%d/%m/%Y' ), count(patient_id) as numpatients
from patient
where STR_TO_DATE(date_enter, '%d/%m/%Y' ) between STR_TO_DATE('$repeat', '%d/%m/%Y' ) and
STR_TO_DATE('$to', '%d/%m/%Y') and
patient_type = 'opd' and
group by STR_TO_DATE(date_enter, '%d/%m/%Y' )
order by STR_TO_DATE(date_enter, '%d/%m/%Y' );
By the way. Don't you think the query looks ugly with all those calls to STR_TO_DATE(). They are ugly in addition to making the query less efficient. Store dates in the database using the database data types. That is what they are there for.