i have some records on mysql db, i tried to group by month, data is correct but time of March month is showing as "2022-03-22", i required as same as other months like 2022-03-01.
time month_name inc_number
2022-01-04 19:58:09 January 39393
2022-02-08 17:36:33 February 90203
2022-03-22 13:40:48 March 82923
2022-04-01 00:14:33 April 23333
2022-05-01 00:31:58 May 33322
2022-06-06 17:21:29 June 33244
2022-07-01 04:19:20 July 90283
2022-08-01 00:07:04 August 8428
2022-09-01 09:40:15 September 10097
2022-10-01 00:30:19 October 6421
2021-12-01 07:12:30 December 8521
the query im using is below
SELECT
created_on as 'time',
MONTHNAME(created_on) AS 'month_name',
count(distinct id_number) AS "inc_number"
FROM test_reports
WHERE
MONTH(created_on)
GROUP BY MONTH(created_on)
ORDER BY MONTH(created_on)
Please suggest the way to get all time should be first date of each month.
If you use GROUP BY and do not specify a column, MySQL will just use one of the created_on values from the 'array'.
Instead you should define the expected output of created_on and add it to your GROUP BY
You could use something like DATE_FORMAT(created_on, '%Y-%m-01') to always display the first day of that month
working, thanks #verhie
SELECT
created_on as 'time',
MONTHNAME(created_on) AS 'month_name',
count(distinct id_number) AS "inc_number"
FROM test_reports
WHERE
MONTH(created_on)
GROUP BY DATE_FORMAT(created_on, '%Y-%m-01')
ORDER BY MONTH(created_on)
Related
I have a table wherein I want to get the total prices of a column in specific dates. The dates are strings (eg: January 2018). What I have tried is to convert the string to date.
SELECT
SUM(price_amount) as 'total_paid',
STR_TO_DATE(CONCAT('01 ',price_month), "%M %d %Y") as dates
FROM user_prices
WHERE price_type = 'cash' AND price_month >= DATE('January 01 2018')
GROUP BY price_month
ORDER BY price_month DESC;
The result of this is null. I have to get only the sum of each existing month in the table that dates are equal or greater January 01 2018. And also group them by month.
email price_amount price_month price_type date_added
1#gmail.com 200 April 2017 cash ---
19#gmail.com 400 December 2017 cash ---
12#gmail.com 100 January 2018 cash ---
123#gmail.com 230 January 2018 cash ---
1234#gmail.com 250 January 2018 credit ---
321#gmail.com 200 April 2018 cash ---
32#gmail.com 120 March 2018 cash ---
So the example above should show the expected result below:
price_month total_paid
March 2018 120
April 2018 200
January 2018 330
If the price_month column is actually literal text as you have shown us, then you'll need to use STR_TO_DATE to make this query work. Note that STR_TO_DATE requires at least year, month, and day information in order to generate a date. So, in the query below, I arbitrarily build each month year data as occurring on the first of the month.
SELECT
price_month,
SUM(price_amount) AS total_paid
FROM user_prices
WHERE
STR_TO_DATE(CONCAT('01 ', price_month), '%d %M %Y') >= '2018-01-01' AND
price_type = 'cash'
GROUP BY
price_month,
STR_TO_DATE(CONCAT('01 ', price_month), '%d %M %Y')
ORDER BY
STR_TO_DATE(CONCAT('01 ', price_month), '%d %M %Y');
Demo
Moving forward, please don't store your dates as text like this. In general, you may assume that a puppy gets run over every time you have to use STR_TO_DATE in a MySQL query.
How to display the last one month result in where clause statement in DB2.
This is my query:-
SELECT distinct
payment_date dated_daily
FROM payment BB
WHERE YEAR(payment_date)= '2018'
AND MONTH(payment_date) = '04'
Say I have the following Column
dated_daily
2018-04-01
until
2018-04-30
After subtract
dated_daily
2018-03-01
until
2018-03-31
Anyone can help me?. Thank you
You can use NEXT_MONTH and FIRST_DAY to find out when a specific month starts. Moreover, Db2 supports date and time arithmetics. Just add or subtract days, weeks, months, years...
values(next_month('2018-04-16') - 1 day)
=> 2018-04-30
I want to make a query to group date by month depend on the last date.
in my table:
july
2015-07-02
2015-07-03
august
2015-08-04
this is my query:
SELECT DISTINCT Visit_Date FROM tr_visit
JOIN tm_child ON tm_child.Child_ID = tr_visit.Child_ID
WHERE tr_visit.Child_ID='CH001'
GROUP BY YEAR(tr_visit.Visit_Date), MONTH(tr_visit.Visit_Date) DESC
My result is:
2015-07-02
and 2015-08-04
How can I select the last visit in july so the result become:
2015-07-03 (last visit in july)
and 2015-08-04 (last visit in august)
like this?
select max(visit_date)
from tr_visit
group by year(visit_date), month(visit_date);
I have a table with a column named timestamp
i want to be able to order by month(timestamp) and year(timestamp) and group the months and years together
so for example, if i had the following timestamps:
2014-01-01
2014-02-01
2014-05-01
2015-01-01
i want to show in this order
MONTH YEAR
1 2015
5 2014
2 2014
1 2014
You can pick month and year from timestamp with MONTH and YEAR:
GROUP BY MONTH(field_with_ts) , YEAR(field_with_ts)
and the same thing with the ORDER BY clause.
Try this:
substring(date, 8 , 2) as Month, substring(date, 1, 3) as Year
See, if that works.
I have a table like this:
id | created_on
1 2013-09-03 20:05:09
2 2013-09-05 17:03:13
...
How do I write a query to return a result of record counts that was created from Date X to Date Y in 7-day intervals?
So the result would look like this:
count | created_on
4 2013-09-17 00:00:00
2 2013-09-24 00:00:00
1 2013-09-31 00:00:00
10 2013-10-07 00:00:00
...
You can go to the beginning of the week by subtracting the day of the week. Here is one way to do that:
select date(created_on - interval dayofweek(created_on) day), count(*)
from t
group by date(created_on - interval dayofweek(created_on) day);
If this is not the day you want the week to start, then you can add an offset day.
Group by the date field, floored to the week:
SELECT
count(*),
YEARWEEK(created_on) as week
FROM
yourtable
GROUP BY week
This assumes that created_on is a type that can be interpreted as a date:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_yearweek
This will get you weekly groupings, but you may want to then convert that field (which will look like YYYYWW) back to something more readable.
You can try this
SELECT created_on, count( id ) AS count
FROM `test_table`
WHERE created_on
BETWEEN '2013-09-01'
AND '2013-10-10'
GROUP BY WEEK( created_on )