Since Alex also wants to know on which days of the week the top 5 travel agencies make most bookings, we need to use a few more queries.
To find the top booking days, we will first create two new tables:
STOPAGENCY: name, agency number, and count of the bookings made by top 5 agencies
SAGBOOKDAYS: details on the number of bookings made per day for each of the agencies
-
First create the STOPAGENCY table by storing the result of the previous query in a new table. Run this query in your console:
CREATE TABLE STOPAGENCY AS (
SELECT TOP 5
SAGENCYDATA.AGENCYNUM,
STRAVELAG.NAME,
SAGENCYDATA.NUMBOOKINGS
FROM
SAGENCYDATA
INNER JOIN
STRAVELAG
ON SAGENCYDATA.AGENCYNUM = STRAVELAG.AGENCYNUM
);
-
To view all contents of this table, just copy and paste the following query into the SQL console and run it:
SELECT * FROM STOPAGENCY;
-
Next, create the table SAGBOOKDAYS to store the daily bookings for each of the agencies. Use the following query:
CREATE TABLE SAGBOOKDAYS AS (
SELECT
AGENCYNUM,
dayname(ORDER_DATE) AS ORDERDAY,
count(dayname(ORDER_DATE)) AS DAYCOUNT
FROM SBOOK
GROUP BY
AGENCYNUM,
dayname(ORDER_DATE)
);
-
To view all contents of this new table, you can again use the SELECT * FROM query:
SELECT * FROM SAGBOOKDAYS;
-
Now that you have created the 2 tables, join these tables based on the agency number (column AGENCYNUM). You also need to extract only the day with maximum number of bookings for each of the top 5 agencies. For this, use the following nested queries:
SELECT
SAGBOOKDAYS.AGENCYNUM,
STOPAGENCY.NAME,
SAGBOOKDAYS.ORDERDAY,
SAGBOOKDAYS.DAYCOUNT
FROM
SAGBOOKDAYS
INNER JOIN
STOPAGENCY
ON SAGBOOKDAYS.AGENCYNUM = STOPAGENCY.AGENCYNUM
WHERE (SAGBOOKDAYS.DAYCOUNT IN (SELECT max(DAYCOUNT)
FROM SAGBOOKDAYS
GROUP BY AGENCYNUM));
-
Now you can see that the most bookings for the top 5 agencies have been done on Thursdays.
Well done!
You have completed the sixth tutorial of this mission! You learned how to create new tables, view table details, join tables and extract specific data from tables using SQL statements in the SAP HANA database explorer. This way you were able to help Alex get the business inside they were looking for.
Since Alex needs to make these insights available to other departments in Best Run Travel, the next step is to create a calculation view to share these findings with other departments.
Learn in the next tutorial how to get started with SAP Business Application to start creating a calculation view.