Quicksight: Calculation between two dates - mysql

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

Related

How to compare a date with array of dates in SQL query and update a field value in MySQL?

I'm working on a task where I need to find the expected date to resolve a ticket using createdAt and sla_name fields values. After that I need to compare the this expected date with the dates in holidays table.
If the expected date falls in holidays, I need to extend the sla_name field value.
This is the query am using.
SELECT t.sla_meet, t.tid, t.ticket_id, t.ticket_name,t.createdAt,t.updatedAt,t.status, dw.dropdown_name
as ticket_priority,p.project_name, dw3.dropdown_name as ticket_status,t.sla as sla_name,
isn.issue_name as issue_type,inn.incidentName as incident_type,t.ticket_accepted_date,
t.asset_id,t.ticket_closed_date,t.contact_number,
IF(NOW() <= DATE_ADD(t.createdAt,INTERVAL (t.sla)+1 DAY),'YES','NO') AS slaMeetData
from tickets t
JOIN assets ast ON t.asset_id=ast.asset_id
JOIN projects p ON p.project_id=ast.project_id
JOIN admin_dropdowns dw ON t.ticket_priority=dw.id
JOIN admin_dropdowns dw3 ON t.ticket_status=dw3.id
JOIN issues isn ON t.issue_type=isn.issue_id
JOIN incident_names inn ON t.incident_type=inn.incidentId
order by t.tid DESC
This is the resultant data of the above query.
Now I need to compare the holidays in above query. And the sample data is,
If the expected date that am getting in IF condition of above query is falls in this holidays, I need to update the sla_name value with COUNT OF HOLIDAYS(If startdata and enddate are there, need to count the days between them) + sla_name.
If expected date is falls on dates range(start and end dates of holidays), need to calculate the count of days from expected date to end date and update that count in sla_name field
Is it possible to do this functionality in SQL? I've used the above query as VIEWS.
Instead of t.sla AS sla_name, use this expression to determine whether to add the length of the overlapping holiday to the number of days:
(
t.sla +
IF(
DATE_ADD(t.createdAt,INTERVAL (t.sla)+1 DAY) BETWEEN holidays.holiday_date AND holidays.end_date,
DATEDIFF( holidays.end_date, holidays.holiday_date ), /* add holiday length number of days */
0 /* no holiday overlap so don't add any days */
)
) as sla_name
You'll also have to join on the holidays table to find the holiday (if any) which overlaps the date in question:
JOIN holidays ON ( DATE_ADD(t.createdAt,INTERVAL (t.sla)+1 DAY) BETWEEN holidays.holiday_date and holidays.end_date )

Join on Date Ranges

I've looked at other answers to this question but haven't found a solution.
I have two tables with a tracking number, one has status history and several records per tracking with different date times for each status. The other table is a cost table that has one record per tracking with a date time that is in the same general time period of the status table but never exact.
I cannot join just on the tracking number itself due to the duplication of the tracking number in the data from months prior. Ex. a tracking number may appear in March of 2019 and again in January of 2020 even though they are very different parcels being shipped. However if you concatenate the tracking with the orderid on the status table you do get a unique value. That orderid number though is not in the cost table so you cannot join the two tables on that value either. It has to be tracking and a date range of some sort.
So I am looking to join the two tables using the tracking number and a date range of +- 30 days from the date provided on the cost table and the final date for that tracking number on the status table.
So something like this without the "is in a 30 day window" part clearly.
SELECT C.cost
, S.trackingnumber
From UPSCost C
join UPSStatus S
ON C.trackingnumber = S.trackingnumber
WHERE MAX(S.date_time) is in a 30 day window of C.event_date_time
You could expand your join and add the date condition to it. Something like this.
SELECT
C.cost,
S.trackingnumber
From UPSCost C
join UPSStatus S
ON(
-- Same tracking number
C.trackingnumber = S.trackingnumber AND
-- status updated within -+30 days from the date found in cost table
s.date_time between DATE_SUB(C.event_date_time, interval 30 day) AND DATE_ADD(C.event_date_time, interval 30 day)
)
Order by S.date_time desc -- latest status first?

Select records according to month'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 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.

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;

SQL query for calculating a monthly total out of a running total

I have a table titled "Reports" in MySQL that has a column titled "Flow_Total" which has a running total value that goes up every day and never resets, what i need is a query that takes the values that are stored in the "Flow_Total" column and divide them by month and tells me how much the value goes up every month.
This is how i would like to see the data:
https://skydrive.live.com/redir?resid=BC22A6E2F92CE833!11843&authkey=!ACgipFLKDJTBlN8
The value for the month is written on the last day of that month.
A summary of what i want to do is subtract the monthly change from the Flow_Total and display it in a separate column titled Monthly Total.
Maybe not the most pleasing SQL to the eyes, but this should do what you're asking; it'll just self join the table with itself delayed 1 month and calculate the difference from that.
SELECT DATE_FORMAT(MAX(a.`DATE`), '%b-%y') `DATE`,
MAX(a.`FLOW_TOTAL`) `Flow Total`,
(MAX(a.`FLOW_TOTAL`) - MAX(b.`FLOW_TOTAL`)) `Monthly Total`
FROM Reports a
LEFT JOIN Reports b
ON YEAR(a.`DATE`) = YEAR(DATE_ADD(b.`DATE`, INTERVAL 1 MONTH)) AND
MONTH(a.`DATE`) = MONTH(DATE_ADD(b.`DATE`, INTERVAL 1 MONTH))
GROUP BY YEAR(a.`DATE`), MONTH(a.`DATE`)
ORDER BY a.`DATE` DESC;
An SQLfiddle for testing.