Hello,
I am trying to count the number of distinct trip_ids for each seating_class GROUP BY month_id and level.
Each month_id, level and seating_class combination should end up having a value even if a value is 0 (or NULL).
So, I can't just count trip_id GROUP BY month_id, level and seating_class, because this will not have any result for certain month, level, seating_class combinations.
How do I do this in Pantheon?
From what I see I cannot add a condition to GROUP BY. Does it mean that I need to split rows by seating_class and then calculate the COUNT DISTINCT?
Below is the query:
SELECT
A.month_id,
B.level,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'First' THEN A.trip_number END)) AS first_class_trips,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'Business' THEN A.trip_number END)) AS business_trips,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'Premium Economy' THEN A.trip_number END)) AS prem_econ_trips,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'Economy' THEN A.trip_number END)) AS economy_trips,
COUNT(DISTINCT A.trip_number) total_trips
FROM A join B on A.cost_center = B.cost_center
GROUP BY A.month_id, B.level
Thank You,
Natalia
I am trying to count the number of distinct trip_ids for each seating_class GROUP BY month_id and level.
Each month_id, level and seating_class combination should end up having a value even if a value is 0 (or NULL).
So, I can't just count trip_id GROUP BY month_id, level and seating_class, because this will not have any result for certain month, level, seating_class combinations.
How do I do this in Pantheon?
From what I see I cannot add a condition to GROUP BY. Does it mean that I need to split rows by seating_class and then calculate the COUNT DISTINCT?
Below is the query:
SELECT
A.month_id,
B.level,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'First' THEN A.trip_number END)) AS first_class_trips,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'Business' THEN A.trip_number END)) AS business_trips,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'Premium Economy' THEN A.trip_number END)) AS prem_econ_trips,
COUNT(DISTINCT (CASE WHEN A.seating_class = 'Economy' THEN A.trip_number END)) AS economy_trips,
COUNT(DISTINCT A.trip_number) total_trips
FROM A join B on A.cost_center = B.cost_center
GROUP BY A.month_id, B.level
Thank You,
Natalia