Select records according to month's last day - mysql

I have table having 26 columns in which first 3 Columns are day,month,year. And rest of columns having some information that i have to show. Now i have to fetch records according to month's last day.
I have tried writing code.
select * from subscription_stats where year * 10000 + month * 100 + day = LAST_DAY(CONCAT(year,'-',month,'-',day))
But this will fetch records from last day of every month. When i dont have actual last day in records then this code will not work. So instead of LAST_DAY i want some functionality like MAX date in that month. How can i implement this functionality.

You want the last date in each month in your data. For this:
select s.*
from subscription_stats s
where s.day = (select max(s2.day)
from subscription_stats s2
where s2.year = s.year and s2.month = s.month
);
Although it would not make this query much simpler, you should be storing dates as dates in your table. That is, one date, not three separate columns for year/month/day.

Related

Quicksight: Calculation between two dates

I work with hotel reservations with the following main fields:
reservation id
check-in date
check-out date
Nights: dateDiff({check-in_date},{check-out_date},"DD")
The thing is I have the total nights per reservation but I would like to have a table with the total nights by each date. Data example:
"booking_id","check_in date","check_out date","Nights"
"1010354582","2022-01-01","2022-01-02",1
"1010364988","2022-01-01","2022-01-03",2
"1010366636","2022-01-01","2022-01-03",2
"1010366752","2022-01-01","2022-01-02",1
"1010367996","2022-01-01","2022-01-04",3
And the result I want:
"stay_date","Nights"
"2022-01-01",5
"2022-01-02",3
"2022-01-03",1
How can I replace the check_in of the original dataset with a new "stay_date" which sum all the reservations that go through the same day of stay?
It can be solved directly with Quicksight or I have to do a different query on the database (Mysql)?
You need a different query on Mysql. You need to create a table called dates with one field date. You need to put a date there for all days you might need to report (example pre populate from 2020 to 2030).
Then you would need to use a custom sql query to have a join
that is like this:
SELECT
dates.date AS stay_date,
COUNT(DISTINCT booking_id) as Nights
FROM
dates LEFT JOIN bookings
ON
dates.date BETWEEN bookings.check_in_date AND bookings.check_out_date - INTERVAL 1 DAY
AND bookings.check_in_date <= bookings.check_out_date - INTERVAL 1 DAY
GROUP BY
dates.date

Select records according to years's last day

I have table having 26 columns in which first 3 Columns are day,month,year. And rest of columns having some information that i have to show. Now i have to fetch records according to years's last day.
I have tried writing code.
select * from subscription_stats where year * 10000 + month * 100 + day = LAST_DAY(CONCAT(year,'-',month,'-',day))
But this will fetch records from last day of every month and i want last day of years.And also, When i dont have actual last day in records then this code will not work. So instead of LAST_DAY i want some functionality like MAX date in that month. How can i implement this functionality.
Is this what you want?
select *
from subscription_stats
where month = 12 and day = 31;
That returns the rows for December 31st.
If you don't have records for all days and you want the last day in the data:
select ss.*
from subscription_stats ss
where (ss.month, ss.day) = (select ss2.month, ss2.day
subscription_stats ss2
where ss2.year = ss.year
order by ss2.month desc, ss2.day desc
);

How to get the record of employees who were joined in first quarter or first month

I want to retrieve the records of employees who were joined in first quarter or in the first month. I have tried this but am not getting the right answer...
SELECT * FROM table
WHERE DOJ(date_created) = DOJ(CURRENT_DATE - INTERVAL 1 MONTH)
Please help me with this!
Answering the question as clarified in a comment...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND QUARTER(table.doj) = 1
If instead you want "first quarter of prior year"...
SELECT * FROM table
WHERE YEAR(table.doj) = YEAR(CURRENT_DATE) - 1 AND QUARTER(table.doj) = 1
In either case, note that there's no code to include the first month, because that's part of the first quarter. However, if you wanted to make that explicit (at a slight performance hit), you could code it as follows...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND (QUARTER(table.doj) = 1
OR MONTH(table.doj) = 1)
If you run into performance problems because you have a lot of records but only an index on table.doj, you could also write the query over an explicit date range...
SELECT * FROM table
WHERE table.doj >= '2015-01-01' AND table.doj <= '2015-03-31'

MySQL: Returning records from the current month and previous 3 months

I'm using PHP/MySQL booking system and i'm using a Google Line Chart to try and display the gross sales from the current month, and also the previous 3 months.
Each booking has a date in the standard phpmyadmin "date" format, so yyyy:mm:dd.
So, im looking to get 4 results from 4 queries, each query filtering out a certain month and grabbing the sum of each booking from that month.
My question is, how can i distinguish between the months in the query? How would i structure the query?
Based on the title:
select * from bookings where MONTH(CURDATE())=MONTH(booking_date);
select * from bookings where MONTH(booking_date) > MONTH(CURDATE()-INTERVAL 3 MONTH) and < MONTH(CURDATE() + INTERVAL 1 MONTH);
For simple per-month searches you can use the following:
Select * from bookings where MONTHNAME(booking_date)='July' and YEAR(booking_date)=2013;
Or:
Select * from bookings where MONTH(booking_date)=7 and YEAR(booking_date)=2013;
Also since you've already got the months, you could do this (this method requires that you maintain a table of ending dates for each month an compensate for leap year though):
select * from bookings where booking_date>'2013-06-30' AND booking_date<'2013-08-01';
In first place, excuse my english....
I know this is old thread and cant comment but, #AbsoluteƵERØ, that answer apply to the current month, in example, if i got records of July in 2013-2014-2015, the query will return the records on the month for those years.... To avoid that and using your posted code:
SELECT * FROM bookings WHERE MONTH(CURDATE()) = MONTH(booking_date) AND YEAR(CURDATE()) = YEAR(booking_date);
Note: if use the "name form" and specify the year there's no problem, like this:
SELECT * FROM bookings WHERE MONTH(CURDATE()) = MONTH(booking_date) AND YEAR(booking_date) = 2013;

How to filter records from date in MySQL?

In my database, there is a field called birthday(date) and I want to retrieve records that their birth month and birth day is equal to the current month and the day. Is there a way to write a query for this? Or I just have to do it by retrieving all of the records and find the matching records after by using another program? Thanks!
This might also work: (NOTE that I am not sure if you mean day within a Month or day within a week)?
SELECT * FROM TABLE
WHERE MONTH(birthday) = MONTH(CURRENT_DATE)
AND DAY(birthday) = DAY(CURRENT_DATE) --assuming day within a month
select * from table
where date_format(birthday, '%m%d')=date_format(current_date, '%m%d');
The above query would not make use of mysql index