Files
alpinebits_python/sql_analysis.md
2025-12-04 15:32:29 +01:00

140 lines
3.9 KiB
Markdown

```
select sum(room.total_revenue::float)
from alpinebits.conversions as con
join alpinebits.conversion_rooms as room on room.conversion_id = con.id
join alpinebits.reservations as res on res.id = con.reservation_id
where con.reservation_id is not null and room.total_revenue is not null
;
```
```
select res.created_at,directly_attributable ,con.reservation_date, res.start_date, room.arrival_date,res.end_date,
room.departure_date, reservation_type, booking_channel, advertising_medium,
guest_first_name,guest_last_name, total_revenue,is_regular,
room.room_status
from alpinebits.conversions as con
join alpinebits.conversion_rooms as room on room.conversion_id = con.id
join alpinebits.reservations as res on res.id = con.reservation_id
join alpinebits.conversion_guests as guest on guest.guest_id = con.guest_id
where con.reservation_id is not null and room.total_revenue is not null
order by reservation_date;
```
Um zu schaugn wie viele schon bearbeitet wurden und als Anfragen in ASA drins sein
```
select res.id, res.created_at, con.created_at as "Con Created at", con.updated_at as "Con Updated at", given_name, surname, guest_first_name, guest_last_name,
meta_account_id, google_account_id, con.id
from alpinebits.reservations as res
join alpinebits.customers as cus on res.customer_id = cus.id
left join alpinebits.conversions as con on con.reservation_id = res.id
left join alpinebits.conversion_guests as g on g.guest_id = con.guest_id
where hotel_id = '39054_001'
order by res.created_at desc limit 400
```
```
select hotel_id
from alpinebits.conversions as con
join alpinebits.conversion_rooms as room on room.conversion_id = con.id
join alpinebits.reservations as res on res.id = con.reservation_id
where con.reservation_id is not null and room.total_revenue is not null
and res.start_date <= room.arrival_date + INTERVAL '7 days'
order by reservation_date;
```
```
select round(sum(room.total_revenue::numeric)::numeric, 3), con.advertising_medium
from alpinebits.conversions as con
join alpinebits.conversion_rooms as room on room.conversion_id = con.id
where room.total_revenue is not null
and con.reservation_date > '2025-01-01'
group by con.advertising_medium
;
```
```
select sum(room.total_revenue::float), is_regular
from alpinebits.conversions as con
join alpinebits.conversion_rooms as room on room.conversion_id = con.id
join alpinebits.reservations as res on res.id = con.reservation_id
join alpinebits.conversion_guests as g on g.guest_id = con.guest_id
where room.total_revenue is not null
and directly_attributable = true
group by is_regular
;
```
```
SELECT res.created_at AS "AnfrageDatum",
directly_attributable,
con.reservation_date,
res.start_date,
room.arrival_date,
res.end_date,
room.departure_date,
advertising_medium,
guest_first_name,
cus.given_name,
guest_last_name,
cus.surname,
total_revenue,
room.room_status,
room_number,
is_regular,
is_awareness_guest,
guest_matched,
con.hotel_id,
guest.guest_id
FROM alpinebits.conversions AS con
JOIN alpinebits.conversion_rooms AS room ON room.conversion_id = con.id
JOIN alpinebits.conversion_guests AS guest ON guest.guest_id = con.guest_id
LEFT JOIN alpinebits.reservations AS res ON res.id = con.reservation_id
LEFT JOIN alpinebits.customers AS cus ON cus.id = con.customer_id
WHERE reservation_date > '2025-01-01'
AND guest.guest_id IN (
SELECT DISTINCT g.guest_id
FROM alpinebits.conversions AS c
JOIN alpinebits.conversion_rooms AS r ON r.conversion_id = c.id
JOIN alpinebits.conversion_guests AS g ON g.guest_id = c.guest_id
WHERE c.reservation_date > '2025-01-01'
AND r.total_revenue > 0
)
ORDER BY guest_first_name, guest_last_name, room_status;
```