6
I need to select the data stored in the database and separate them by month, but when the month does not have data yet, the month is not returned.
I need the month to be returned with the value reset but bring the month anyway.
My Query this way:
SELECT
(year(article.content_path)) as anoAtual,
MONTHNAME(STR_TO_DATE(month(article.content_path), '%m')) AS MES,
TIME_FORMAT(SEC_TO_TIME(Sum(time_accounting.time_unit)*60),"%H:%i") as tempo_total,
concat(users.first_name, ' ',customer_user.last_name) as usuario,
customer_user.email
FROM
article,
time_accounting,
users,
customer_user
where
article.id = time_accounting.article_id and
article.create_by = users.id and
EXTRACT(YEAR FROM article.content_path) IN (2018) and
users.first_name = customer_user.first_name and
users.last_name like concat(customer_user.last_name,'%') and
customer_user.customer_id = empresa and
users.valid_id = 1 and
customer_user.email in (emails)
group by
article.create_by,
year(article.content_path),
MES
order by
article.create_by,
year(article.content_path),
month(article.content_path)
I get the following return:
In this case, I need the month of December to come, even though I still have no records this month. I tried something similar with what I need with a Query simpler:
SELECT
month(article.change_time) as mes,
SEC_TO_TIME(SUM(IFNULL(TIME_TO_SEC(STR_TO_DATE(TIMEDIFF(
(SELECT value_date FROM otrs.dynamic_field_value where object_id in (t2.object_id) and field_id = 79),
(SELECT value_date FROM otrs.dynamic_field_value where object_id in (t2.object_id) and field_id = 78)),'%H:%i:%s')),0))) as tempoDeslocamento
FROM otrs.dynamic_field_value t2, otrs.article
where otrs.article.id = t2.object_id and year(article.change_time) = 2018 and article.create_by = 9
and field_id in (78)
group by month(article.change_time);
But even so it only returns the records in the months that have records.
Missed the month 9 and the month 12 however the IFNULL should not treat this?
Could post your
create table
and some sample data here https://paiza.io/en/projects/new?language=mysql, save and add link to the question please?– edson alves
I updated the @edsonalves question but I can’t ask this question of putting example data and create table commands because I don’t have access to it, I can only create
SELECT
– R.Santos
Just to reinforce, you would need to find a way to at least put the create table here or in some fiddle to facilitate the answers, as it is totally possible and easy to do what you are wanting.
– wryel