This question already has answers here:
How to group by month and return zero if no value for certain month?
(3 answers)
Closed 3 years ago.
I have "MySQL Query" with a table of data items in 1 year. I want to display based on month grouping.
SELECT MONTHNAME(create_date) AS month, COUNT(*) AS cplan FROM nota_jual GROUP BY month
I want the data displayed to include items with a value of 0 every month.
month cplan
January 3
February 2
March 0
April 0
May 0
June 0
July 8
August 8
September 6
October 3
November 5
December 3
can some one help /answer this? (Thankyou)
Check this
SELECT date_format(tn.create_date,'%m') as Month, count(*) as cpplan
FROM nota_jual tn
GROUP BY Month
ORDER by Month;
Related
I currently have a monthly list of generated revenue but it is incorrectly accounted for and I have to change the dates to the previous month. For example, December revenue actually corresponds to November, May to April, February to January... and so on historically over the years.
The table I have made from groupings by month and year is the following:
Month
2021
2022
1
7.582
7.242
2
2.456
2.992
3
34.513
4.566
4
57.433
9.991
5
35.788
8.689
6
52.466
7.600
7
35.657
26.246
8
44.673
54.345
9
92.376
57.885
10
3.444
86.685
11
34.788
67.246
12
0
57.378
Which approach should I use in this case? I am using Metabase.
I have a table in my database which stores the meters energy value of 1st of every month. In case meter is offline it will store the value of the next day.
Below is my case
I have a record of a meter of past 2 months February and March. The February data is of 2019-02-01 00:00:00 but there are 4 rows for the month March. See the below image
In the above image the 1st,2nd and 3rd of March have a null value of FA but the 4th March contains some value.
What I have done?
I am able to select the rows having values of FA.
What I want to do?
I want to get only the current month data i.e. Current month is March so it should get only march record and then next month it should get only April record and so on.
The query should not exceed the days limit more than 4 i.e. It should only check record for 1st four days of every month.
Here is my DB-Fiddle
Any help would be highly appreciated.
one way to solve this is
FOR
I want to get only the current month data i.e. Current month is March so it should get only march record and then next month it should get only April record and so on.
means month(TV)= month(now())
and
The query should not exceed the days limit more than 4 i.e. It should only check record for 1st four days of every month. means day(TV)<= 4
and finally your query
select * from `biz_pub_data_f_energy_m` a
where a.`DATA_ID` = '1b9716122dd5408691a063227316ac0a'
and a.`FA` is NOT NULL and month(TV)= month(now())
and day(TV)<= 4
You can try below -
DEMO
select * from `biz_pub_data_f_energy_m` a
where a.`DATA_ID` = '1b9716122dd5408691a063227316ac0a'
and a.`FA` is NOT NULL and tv>=date(DATE_SUB(now(),INTERVAL DAYOFMONTH(now())-1 DAY))
and tv<=DATE_ADD(date(DATE_SUB(now(),INTERVAL DAYOFMONTH(now())-1 DAY)), INTERVAL 4 DAY)
I would to seek some help from the SQL Experts here in Stackoverflow.
I currently have this kind of table:
And I have been successfully getting the sum amount per year with this query:
select case when month(savings_date) >=11
then year(savings_date) +1
else year(savings_date)
end as fiscal, sum(amount)
from net_savings
group by fiscal
And having this output:
Now I would like to display all the sum amounts per month with a given input of fiscal year. How would I do this?
My fiscal year starts from november and ends at october. So if I have october 2015 in my records, it should not show up when I enter 2016 as fiscal year.
You can do this:
select month(savings_date), sum(amount)
from net_savings
where dateadd(savings_date, interval -2 month)
group by month(savings_date);
I need stats on orders, week by week, so I have done this:
SELECT YEAR(orders.date), WEEKOFYEAR(orders.date), COUNT(*)
FROM orders
GROUP BY YEAR(orders.date), WEEKOFYEAR(orders.date)
It worked for one year, but just now (new year) it does not count the last days of 53rd week (jan 1st, 2nd, 3rd). How can I update my Query in order to have the full last week (from Monday 2015-12-28 to Sunday 2016-01-03)?
You need to switch to YEARWEEK(orders.date,3) to get the ISO weeks as a single column. Using WEEK(orders.date,3) (which is exactly the same as WEEKOFYEAR) will return the correct week number, but YEAR(orders.date) will return either 2015 or 2016, splitting the week into four days in 2015 and and three days in 2016.
As Strawberry mentioned in the comments you're looking for the WEEK function. I just checked the documentation at the MySQL website.
Week(date [,mode])
This function returns the week number for date. The two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used
Here's an example
SELECT WEEK('2008-12-31',1);
=> 53
It should also be noted that this is not the same as the WEEKOFYEAR function.
Returns the calendar week of the date as a number in the range from 1 to 53. WEEKOFYEAR() is a compatibility function that is equivalent to WEEK(date,3).
We can see that the value of the mode parameter here is 3. Here is the table that shows the values for the modes
Mode First day of week Range Week 1 is the first week
0 Sunday 0-53 With a Sunday in this year
1 Monday 0-53 With 4 or more days this year
2 Sunday 1-53 With a Sunday in this year
3 Monday 1-53 With 4 or more days this year
4 Sunday 0-53 With a Sunday in this year
5 Monday 0-53 With 4 or more days this year
6 Sunday 1-53 With a Sunday in this year
7 Monday 1-53 With 4 or more days this year
Source
https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_week
As I understand you, you want calculate total orders for one week only once not twice for various years.
I've written SQL query for SQL Server, but I think you can easy rewrite it on mysql.
DECLARE #test_table TABLE(created_date DATETIME, VALUE INT)
INSERT INTO #test_table
SELECT N'20151231', 10
UNION ALL
SELECT N'20151228', 20
UNION ALL
SELECT N'20160101', 40
UNION ALL
SELECT N'20160104', 5
UNION ALL
SELECT N'20160107', 2
SELECT first_day_week_number,
SUM(value) AS total
FROM
(SELECT *,
DATEPART(ww,
DATEADD(dd, -DATEPART(dw, created_date)+2, created_date)) AS first_day_week_number
FROM #test_table ) AS T
GROUP BY first_day_week_number
To avoid problems with years you could calculate first week day of order date and then get week number from it.
I need a query to get results from a table that has 2 columns
Column startdt (datetime), Column enddt (datetime)
there are some records with startdt 2013-07-19 and enddt 2013-07-29
I need to get the records with weekday = 1 (Tuesday)
the record with date 2013-07-19 is weekday 4 and ends 2013-07-29 which is 0
Actually i want to get the results that has for weekday Monday or another weekday.
You can check the above link for an example
http://sqlfiddle.com/#!2/a80ce/1
If you don't understand what i want to do let me explain. I have an event that starts July 15 and ends July 25. (Starts Monday and ends Thursday) The user selects one of the week days (Monday, Tuesday etc). If he select Tuesday then i want the query that will get all events that are active in Tuesday.
I already found the answer so if anyone want to check it
SELECT articleid,startdt,enddt,dayofweek(startdt), DATEDIFF(enddt,startdt) datedf
FROM events
WHERE (dayofweek(events.startdt) <= 3 AND dayofweek(events.enddt) >= 3)
OR DATEDIFF(enddt,startdt) >=6
(3 is the number of the weekday "Tuesday")
How about using the comments that other people gave you and use a query that combines both dayofweek and a simple greater/smaller/equal syntax as follows:
SELECT * FROM events where dayofweek(events.startdt) <= 6 AND dayofweek(events.enddt) >= 6
This gives the following results if the user specified a friday (= 6):
ARTICLEID STARTDT ENDDT
4 July, 12 2013 00:00:00+0000 July, 26 2013 00:00:00+0000
6 July, 16 2013 00:00:00+0000 July, 20 2013 00:00:00+0000
I do think that you are better of using dayofmonth however as this (maybe just to me) makes it clearer, possibly combining the use of both to ensure that it's active on a friday.
The OP indicates that events which are in the history should also be retrieved and as such the following query does what he wants:
SELECT * FROM events where dayofweek(events.startdt) <= 6 AND dayofweek(events.enddt) >= 6 OR DATEDIFF(enddt,startdt) >=6
How about this solution:
first you convert the day of week in format that 6 is Saturday and 7 is sunday(it's easier for me)
if(dayofweek(o.start_date) = 1, 7, dayofweek(o.start_date) -1)
after that you calc the days from the start_date needed to reach some of the weekdays
(7 - if(dayofweek(o.start_date) = 1, 7, dayofweek(o.start_date) -1)
finally you make sure that the difference in days between the two dates is no less that the above calculation
(7 - if(dayofweek(o.start_date) = 1, 7, dayofweek(o.start_date) -1) <= datediff(o.end_date, o.start_date)