Quantcast
Channel: Pentaho Community Forums
Viewing all articles
Browse latest Browse all 16689

How to GROUP BY depending on a value of a field?

$
0
0
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

Viewing all articles
Browse latest Browse all 16689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>