Change the month to its previous month (SQL Dates) - mysql

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.

Related

How to show data with zero value every month [duplicate]

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;

Find date scope from MySQL efficiently

Here is my DiscountPeriod table's structure:
id
room_id
date_from
date_last
discount
Imagine that we have discount starting 01 December 2017 and ending in in 10 December 2017.
I'm searching for date-range to see if it has discount.
So date range might be totally or partly inside some of discount periods. 3 example date-ranges for search:
From 02 December to 10 December (fully inside one of discount periods)
From 20 November to 4 December (partly inside)
From 5 December to 15 December (partly inside)
Expected for all of 3 examples above is to get discount that starts in 01 December 2017 and ends in 10 December 2017.
Currently my query takes only those results which is completely inside exact period from database.
It looks like this:
SELECT * FROM `DiscountPeriod` WHERE (`room_id`=1517) AND (`date_last` >= '2017-12-12') AND (`date_from` <= '2017-12-20');
Question is, how to fit all of 3 possible search cases into 1 query for efficient searching in MySQL database tables?
Expected result is
All of following scopes: From 02 Dec to 10 Dec, From 20 Nov to 4 Dec, From 5 Dec to 15 Dec should return back 1-10 december discount.
This looks like an overlapping range problem. If you want to return all discounts which overlap with 1-10 December 2017, then try the following query:
SELECT *
FROM DiscountPeriod
WHERE
room_id = 1517 AND
'2017-12-01' <= date_last AND '2017-12-10' >= date_from;
Here is a demo which uses your test data. All three discount ranges you suggested show up in the result set. But a range lying completely outside 1-10 December 2017 is absent, as we would expect.
Demo

MySQL Custom WEEK() Mode

I am trying to create a weekly report (including grouping by WEEK(date)) where weeks start on Monday. However, Week 1 is always the first week any day of a new year occurs. For example if Jan 1st is a Friday, that is week one.
This doesn't seem to be consistent with MySQL mode options for WEEK():
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
Based on the available options it only works if the week has 4 or more days (options 1 and 3) or the Monday is in the new year (options 5 and 7). Neither of these account for example week 1 of 2016:
Wk Mon Tue Wed Thu Fri Sat Sun
No.
1 28 29 30 31 1 2 3
Is there any way to order/number the week numbers in MySQL based on this custom week mode requirement?

MySQL get next 3 months of holidays by month, day (regardless of year)

I have a table of holidays, stored by date, so they have a year:
Christmas 2014-12-25
Birthday 2014-05-12
Holiday 3 2015-11-27
Let's say today is November 1st, in the year 2020. How do I retrieve the holidays for the next three months, in order of month, day occurrence? So the record set should look like:
Holiday 3 2015-11-27
Christmas 2014-12-25
EDIT: If I modified the table to include the months and dates in separate columns, a suggested by PressingOnAlways below, how could I query for the date range?
Holiday Year Month Date
Christmas 2014 12 25
Birthday 2014 05 12
Holiday 3 2015 11 27
You can select holidays using DAY() and MONTH() methods of SQL
In your where clause use DAY(holiday) AND MONTH(holiday)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_day
http://www.sqlfiddle.com/#!2/92c97/15

Get name of day from month, year, and day

Does anyone know a way to convert a month, year, and day into the day's name for any year? Example:
function convert(day, year, month)
...
return "Monday"
end
Thanks in advance!
You can use the following method:
This method uses codes for different
months and years to speed up the
calculation of the day of the week.
You might even be able to memorize the
codes. We'll use December 16, 2482 as
an example.
Take the last 2 digits of the year. In
our example, this is 82.
Divide by 4, and drop any remainder.
82 / 4 = 20, remainder 2, so we think
"20."
Add the day of the month. In our
example, 20 + 16 = 36.
Add the month's key value, from the
following table. Jan Feb Mar Apr May
June July Aug Sept Oct Nov Dec 1 4 4
0 2 5 0 3 6 1 4 6
The month for our example is December,
with a key value of 6. 36 + 6 = 42.
If your date is in January or February
of a leap year, subtract 1. We're
using December, so we don't have to
worry about this step.
Add the century code from the
following table. (These codes are for
the Gregorian calendar. The rule's
slightly simpler for Julian dates.)
1700s 1800s 1900s 2000s 4 2 0 6
Our example year is 2482, and the
2400s aren't in the table. Luckily,
the Gregorian calendar repeats every
four hundred years. All we have to do
is add or subtract 400 until we have a
date that is in the table. 2482 - 400
= 2082, so we look at the table for the 2000s, and get the code 6. Now we
add this to our running total: 42 + 6
= 48.
Add the last two digits of the year.
48 + 82 = 130.
Divide by 7 and take the remainder.
This time, 1 means Sunday, 2 means
Monday, and so on. A remainder of 0
means Saturday.
How to calculate the day of the week
A quickl google for "day of week from date algorithm" showed up this Wikipedia article
But depending on the dates you need to work with, beware the strange history of Gregorian calendar adoption