Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Month Count Count:,, (., ALL ., ALL . ,)

The document contains 4 SQL queries: 1) Counts unique guests and total bookings by month by querying 3 tables and unioning the results. 2) Calculates total room nights and average price per room by city by joining 4 tables and grouping by city. 3) Finds the top 3 hotels by revenue in each city for a given time period by joining 2 tables, grouping, and using row_number(). 4) Calculates the repeat customer rate by getting distinct customers from 2 tables and left joining them to find matching customers.

Uploaded by

Prerit Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Month Count Count:,, (., ALL ., ALL . ,)

The document contains 4 SQL queries: 1) Counts unique guests and total bookings by month by querying 3 tables and unioning the results. 2) Calculates total room nights and average price per room by city by joining 4 tables and grouping by city. 3) Finds the top 3 hotels by revenue in each city for a given time period by joining 2 tables, grouping, and using row_number(). 4) Calculates the repeat customer rate by getting distinct customers from 2 tables and left joining them to find matching customers.

Uploaded by

Prerit Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

-------------1---------------------

SELECT MONTH, COUNT(DISTINCT CUSTOMER_ID) AS UNIQUE_GUESTS,COUNT(DISTINCT BOOKING_ID) AS


TOTAL_BOOKINGS
FROM (
SELECT A.*, 'JAN' AS MONTH FROM TABLEA A
UNION ALL
SELECT B.*, 'FEB' AS MONTH FROM TABLEB B
UNION ALL
SELECT C.*, 'MAR' AS MONTH FROM TABLEC C
)X
GROUP BY MONTH;

------------------------2-----------
SELECT CITY, SUM(ROOM_NIGHT) AS TOTAL_ROOM_NIGHTS,
ROUND(SUM(AMOUNT)*1.0/SUM(ROOM_NIGHT),2) AS AVG_PRICE_PER_ROOM
FROM (
SELECT X.*, OYO_ROOMS*DATEDIFF(DD,CHECKIN, CHECKOUT) AS ROOM_NIGHT, Y.CITY
FROM (
SELECT A.*, 'JAN' AS MONTH FROM TABLEA A
UNION ALL
SELECT B.*, 'FEB' AS MONTH FROM TABLEB B
UNION ALL
SELECT C.*, 'MAR' AS MONTH FROM TABLEC C
)X
INNER JOIN TABLED Y ON X.HOTEL_ID=Y.HOTEL_ID
) P
--WHERE STATUS <> 3
GROUP BY CITY
ORDER BY 1;

--------------------------3------------------------------------------
SELECT CITY, HOTEL_ID, REVENUE
FROM (
SELECT A.*, ROW_NUMBER() OVER(PARTITION BY CITY ORDER BY REVENUE DESC) AS RN
FROM(
SELECT CITY, X.HOTEL_ID, SUM(AMOUNT) AS REVENUE
FROM TABLEC X
INNER JOIN TABLED Y ON X.HOTEL_ID=Y.HOTEL_ID
GROUP BY CITY, X.HOTEL_ID
) A
)B
WHERE RN<=3;

-------------------4------------------------------------------,
SELECT ROUND(COUNT(B.CUSTOMER_ID)*1.0/COUNT(A.CUSTOMER_ID),4)*100 AS REPEAT_RATE
FROM (
SELECT DISTINCT CUSTOMER_ID FROM TABLEA
) A
LEFT JOIN (
SELECT DISTINCT CUSTOMER_ID FROM TABLEB
) B
ON A.CUSTOMER_ID=B.CUSTOMER_ID;

You might also like