I have two tables. One is a Booking_Platform table with all hotel booking data. The second is a Customer_Country_Table which stores information for origins of each customer that booked hotels through the platform.
I have to calculate which country has the highest increase in bookings from 2017 to 2018.
I will give some sample data below for reference:
Booking_Platform_Info
Booking_Date column2 column3.... column N ....... Origin_Country_ID
20-dec-2016 .................................... 103
03-jan-2017 .................................... 101
09-feb-2017 .................................... 103
23-apr-2017 .................................... 102
06-oct-2017 .................................... 102
11-nov-2017 .................................... 103
05-jan-2018 .................................... 102
21-jan-2018 .................................... 102
26-feb-2018 .................................... 101
09-mar-2018 .................................... 101
11-may-2018 .................................... 103
10-sep-2018 .................................... 102
20-nov-2018 .................................... 101
07-dec-2018 .................................... 101
23-dec-2018 .................................... 101
31-dec-2018 .................................... 103
23-jan-2019 .................................... 103
Customer_Country_Info
Country_ID Country_Name
101 Italy
102 Spain
103 Portugal
It is a bit complicated for me, as I understand I have to first join the tables, then do a group by country, then count the total no. of bookings by year (probably another group by); and then compare the results to see which country has the highest positive difference in bookings from 2017-2018. I welcome any help with coding this problem.
In my example, country 101 Italy would be the answer because difference between bookings in 2018 and 2017 is highest (5-1=4)
*********Edit after comments
I am writing two queries to get booking totals by country ID for both 2017 and 2018
SELECT CAST(booking_date AS DATE), COUNT(*) as number_of_bookings, origin_country_id FROM Booking_Platform_Info
WHERE booking_date >= '2017-01-01' AND
booking_date < '2017-01-01'
GROUP BY origin_country_id;
SELECT CAST(booking_date AS DATE), COUNT(*) as number_of_bookings, origin_country_id FROM Booking_Platform_Info
WHERE booking_date >= '2018-01-01' AND
booking_date < '2019-01-01'
GROUP BY origin_country_id;
Sorry for my lack of knowledge, but I am not aware how to join queries so that I could get the country id with the highest difference in bookings.
You need to join those two queries to compare the counts.
You also shouldn't include CAST(booking_date AS DATE)
in the SELECT
list. It's not needed, and it will just be a randomly selected date from the year.
SELECT country_name
FROM (
SELECT a.origin_country_id
FROM (
SELECT origin_country_id, COUNT(*) AS 2017_total
FROM Booking_Platform_Info
WHERE STR_TO_DATE(booking_date, '%d-%b-%Y') BETWEEN '2017-01-01' AND '2017-12-31'
) AS a
JOIN (
SELECT origin_country_id, COUNT(*) AS 2018_total
FROM Booking_Platform_Info
WHERE STR_TO_DATE(booking_date, '%d-%b-%Y') BETWEEN '2018-01-01' AND '2018-12-31'
) AS b
ORDER BY 2018_total - 2017_total
LIMIT 1
) as t1
JOIN Customer_Country_Info AS t2 ON t1.origin_country_id = t2.origin_country_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.