Mysql - Group by month with Y-m-d format - mysql

If I have column 'date' in format 'Y-m-d', can I use GROUP BY based on date column but group by months?

Use MySQL's DATE_FORMAT() function to group only on the desired parts:
GROUP BY DATE_FORMAT(my_column, '%Y-%m')

For Example: created_at is the column you need
SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d')
FROM `table`
GROUP BY DATE_FORMAT(`created_at`, '%Y-%m-%d')
Note that you will need to select created_at.

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.

Order by date when a format 'dd-MM-YYYY' is given in the select statement is not working as expected

I store the date in a DATETIME field in my database. When I create a select statement I need to show it with format '%d-%m-%Y' but I have problems sorting the data by date.
this is my selec statement:
SELECT
DATE_FORMAT(Date,'%d-%m-%Y') AS Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30' ORDER BY Date asc;
But the data does not get sorted by date. How can I solve this?
If you use the same name for the column date and formatted date then the order by work for the alias column .. so is order by day, month, year as in your format ('%d-%m-%Y')
then try changing the alias name
SELECT DATE_FORMAT(Date,'%d-%m-%Y') AS My_Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30'
ORDER BY Date asc;
or use a proper order by format
SELECT DATE_FORMAT(Date,'%d-%m-%Y')Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30'
ORDER BY DATE_FORMAT(Date,'%Y-%m-%d') asc;
You can solve this by telling SQL to order by the Purchase.Date field instead of the formatted string date value. When you chose to order by "Date" with no table or table alias specified, SQL assumes you want the formatted result aliased as Date, not the "Date" field from your Purchase table.
SELECT DATE_FORMAT(Date,'%d-%m-%Y') AS My_Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30'
ORDER BY Purchase.Date asc;

MySQL group by date DATE_FORMAT() function

I have the following query:
SELECT DATE_FORMAT(order_completed, "%c/%Y") AS Month, COUNT(*) AS Total_Transactions, SUM(order_total_grand) as Total_Spend FROM `shop_orders` GROUP BY DATE_FORMAT(order_completed, "%c/%Y") DESC
Which outputs the following table:
How can I sort the table by date correctly?
Don't order by the string version of the date. Order by the original date:
SELECT DATE_FORMAT(order_completed, "%c/%Y") AS Month, COUNT(*) AS Total_Transactions,
SUM(order_total_grand) as Total_Spend
FROM shop_orders
GROUP BY DATE_FORMAT(order_completed, "%c/%Y")
ORDER BY MIN(order_completed) DESC;
Strings are ordered as strings, not dates.

MySQL DATETIME to MONTH only

i made a sql statement and looks fine i first wanted to show only weekly dates like this one
WHERE WEEK(date_add) = WEEK(UTC_TIMESTAMP())
this works fine outcome = '2017-02-27 12:08:24'
For MONTHS i change WEEK to MONTH or YEAR works great. But then someone asked me to show only the date like outcome = 'april' or 'juli' and then i was like how do i do that? so i searched on google and found out that i could use %M Month name (January..December)
I know i need
'%M'
but how can i have it work in my case.
I also did try
DATE_FORMAT(NOW(),'%M')
But the outome was 1
I did search other post before posting this one they did not help me.
SELECT
sum(totalExcl) AS total, saleType, date_add
FROM
ex.ps_ox_quo
WHERE
WEEK(date_add) = WEEK(UTC_TIMESTAMP())
AND saleType IN ('IEW')
GROUP BY date_add
ORDER BY date_add DESC
I think you just want date_format():
SELECT sum(totalExcl) AS total, saleType, date_add,
date_format(date_add, '%M')
FROM ex.ps_ox_quo
WHERE WEEK(date_add) = WEEK(UTC_TIMESTAMP()) AND
saleType IN ('IEW')
GROUP BY date_add, saleType
ORDER BY date_add DESC;
Note: You should probably include the year in the date comparison.
You have a datetime type for column date_add you can use MONTHNAME
SELECT
sum(totalExcl) AS total, saleType, MONTHNAME(date_add)
FROM
ex.ps_ox_quo
WHERE
WEEK(date_add) = WEEK(UTC_TIMESTAMP())
AND saleType IN ('IEW')
GROUP BY MONTHNAME(date_add)
ORDER BY MONTHNAME(date_add) DESC
try this
SELECT
sum(totalExcl) AS total, saleType, monthname(date_add)
FROM
ex.ps_ox_quo
WHERE
month(date_add) = month(UTC_TIMESTAMP())
AND saleType IN ('IEW')
GROUP BY month(date_add)
ORDER BY month(date_add) DESC
select YEAR_MONTH(now()) AS current_month

MySQL select only 1 per match

I have a MySQL table with 5 rows:
email
message_id
date
time
And I would like to count the number of emails there is per days.
So far I have this
SELECT Date,COUNT(*) AS Num FROM mail2_mailing_log WHERE Id_message=#Id GROUP BY Date ORDER BY Date DESC
But we found out that there was a problem with a script and many data got multiplied (Which have since been fixed), but I would like to be able to use the data I have.
So basically I would want to "merge" all the rows that match per email, date and time and then group by date and count the number of items.
SELECT q.date, COUNT(*)
FROM (SELECT DISTINCT email, date, time
FROM mail2_mailing_log
WHERE Id_Message = #Id) q
GROUP BY q.date
ORDER BY q.date DESC
Use the distinct keyword:
SELECT DISTINCT Date,COUNT(*) AS Num FROM mail2_mailing_log WHERE Id_message=#Id GROUP BY Date ORDER BY Date DESC
If the Date column contains the time too, then you will need to format the Date column using the DATE_FORMAT() function.
SELECT DATE_FORMAT(Date, '%W %M %Y'),COUNT(*) AS Num FROM mail2_mailing_log
WHERE Id_message=#Id GROUP BY DATE_FORMAT(Date, '%W %M %Y')
ORDER BY DATE_FORMAT(Date, '%W %M %Y') DESC