MySQL DATETIME to MONTH only - mysql

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

Related

How to convert mysql query to JPQL query

I have working MySQL query:
SELECT date(timestamp), hour(timestamp), sum(numberDet), count(*)
FROM human_det_counter
GROUP BY hour( timestamp ) , day( timestamp )
ORDER BY date(timestamp)
I want select records, group by hour from each day and sum their number of detections.
I have tried this query:
SELECT date(h.timestamp), hour(h.timestamp), sum(h.numberDet), count(h)
FROM HumanDetCounter h
GROUP BY hour( h.timestamp ) , day(h.timestamp )
ORDER BY date(h.timestamp)
,but it's not working. I readed that jpa don't support Hour() function and that I should use TO_CHART function, but I don't know how.
Okey, I figured it out. There's my query if someone needs that:
createQuery("SELECT cast(Date(h.timestamp) as string), substring(h.timestamp, 12,2) , sum(h.numberDet), count(h) FROM HumanDetCounter h GROUP by substring(h.timestamp, 12,2), substring(h.timestamp, 9,2) ORDER BY h.timestamp")
It's probably not the best solution, but it's work :)

SQL: Order by date, only month and year given - Full Group on

I have an SQL database which is setted to full group on mode. My goal is to get the amount of rows (ID's) for every month. This is why I say Group By Datum. Because of the full group mode I cannot simply say Order By created_at. Because I have selected only %m.%Y. So I can only work with Datum which is cointaing my month and year.
I already tried to connect those values like CONCAT('DATE_FORMAT(created_at, '%Y-%m'), "-01 00:00:00") but also this isn't working... Even if I turn it into a UNIX Timestamp it isn't working: UNIX_TIMESTAMP(CONCAT('DATE_FORMAT(created_at, '%Y-%m'), "-01 00:00:00"))
I even tried this one:
Order By Year(DATE_FORMAT(created_at, '%Y')), Month DATE_FORMAT(created_at, '%m') But it isn't also working...
How can I sort my result by month and year without changing the Select values?
Sofar this is my actual SQL Query. Not working either.. I am nearly trying to find a solution since 1 hour...
SELECT COUNT(ID) AS Anzahl, DATE_FORMAT(created_at, '%m.%Y') AS Datum
FROM leads
WHERE created_at >= '2015-01-01' AND created_at <= '2018-01-01'
AND shopID = 4184
GROUP BY Datum
ORDER BY UNIX_TIMESTAMP(STR_TO_DATE(Datum, '%Y-%m-01 00:00:00'))
I would appreciate any kind of help! And no, I cannot change the Select values or turn off the full_group_mode.
The following will sort by year and then month in ascending order:
order by substring(datum,4,4), substring(datum,1,2)
sqlfiddle:http://sqlfiddle.com/#!9/16fab4/3
ORDER BY DATE_FORMAT(created_at, '%Y'), date_format(created_at, '%M') desc
This will sort the results by year ascending then by month descending.
Take a look at this SQL Fiddle to see the query using MySql 5.6

MySQL group by day with datetime mixing same dates from different months

I'm trying to group posts from same day, the problem is that 2/20 gets grouped with 3/20 (20 = 20)
How can this be fixed?
This is my current code:
select day(Date), count(*) from Posts WHERE shopID != '' group by shopID, day(Date)
You need to group by every piece that might be different. So add MONTH(Date) and even YEAR(Date) depending on the scope of your query.
select DAY(Date), count(*) from Posts WHERE shopID != '' group by shopID, YEAR(Date), MONTH(Date), DAY(Date)
You could also group this way: UNIX_TIMESTAMP(date(date)), instead of grouping by year, month and day separately
select date(date), count(*) from Posts
WHERE shopID != ''
group by shopID, UNIX_TIMESTAMP(date(date))
Note you'll have to also take the other date data in the select statement to be able to recognize which month/year the day belongs to. If you don't you'll get a lot of day numbers and counts, but the day numbers will be repeated for each month/year.
That's why I used date(date), count(*).

How to group by date regardless of time?

I'm trying to group by Date(). I've got 3 records with the created_at column:
2011-12-03 08:00:24, 2011-12-03 08:12:10, 2011-12-04 09:00:00
I'd like to only group by year, month and day, regardless of time. So for the example above. It should only return two rows:
2011-12-03 and 2011-12-04
How should I go about this?
... group by date(date_time_column)
This should allow you to group by year month and day
SELECT group_by_column
, DATE_FORMAT(created_at, '%Y')
, DATE_FORMAT(created_at, '%m')
, DATE_FORMAT(created_at, '%d')
FROM my_table
GROUP BY group_by_column
or if you want to do them all together.
SELECT group_by_column
, DATE_FORMAT(created_at, '%Y%m%d')
FROM my_table
GROUP BY group_by_column
Did you try the following?
SELECT
DATE(created_at) AS created_date
FROM
my_table
GROUP BY
created_date
MySQL permits GROUP BY DATE(created_at). So that would translate in ActiveRecord to .group(DATE(created_at))
In fact, that exact example is available in the Rails Guides on ActiveRecord querying.
You can use the Date() function.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
TRY
GROUP BY DATE(`date_column`)
Reference

get all months last days in mysql

For example I have a table with fields:
id date
1 2001-01-01
2 2001-01-05
.................
N 2011-12-31
How get i get all months last days from this table?
example:
if i have dates like 2001-05-31 and 2001-06-01
i need only 2001-05-31 not both
You can do SELECT LAST_DAY for example the below returns Oct. 31st. 2010
SELECT LAST_DAY('2010-10-10');
select max(date_format(date, '%d')) as last_day_of_the_month
from table
group by date_format(date, '%Y%m')
maybe this would work better?
select DISTINCT(LAST_DAY(date)) from table GROUP BY date_format(date, '%Y%m')
SELECT id, date
FROM `table`
WHERE DATE( date ) = LAST_DAY( date )
The DATE function over field is for filter the date without time, use only if you have a datetime column.
The query get all rows with date = last day of month.
select subdate(adddate(subdate(`date`, day(`date`) - 1), interval 1 month), 1)
Note: This is the "hard way". See #harper89's answer - it's better :)
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
select max(date)
from table
group by year(date), month(date)