I am new to Apache Pig and am trying to learn. Is there an SQL equivalent COUNT(DISTINCT CASE WHEN ...)in Apache Pig?
For example, I'm trying to do something like this:
CREATE TABLE email_profile AS
SELECT user_id
, COUNT(DISTINCT CASE WHEN email_code = 'C' THEN message_id ELSE NULL END) AS clickthroughs
, COUNT(DISTINCT CASE WHEN email_code = 'O' THEN message_id ELSE NULL END) AS opened_messages
, COUNT(DISTINCT message_id) AS total_messages_received
FROM email_campaigns
GROUP BY user_id;
I canโt use FILTER email_campaigns BY email_code = 'C'because it shortens other cases. Is there a way to do this all in one nested block FOREACH?
Thanks!
EDIT:
As requested, sample data. Fields used_id, email_codeand message_id.
user1@example.com O 111
user1@example.com C 111
user2@example.com O 111
user1@example.com O 222
user2@example.com O 333
Expected Result:
user1@example.com 2 1 2
user2@example.com 2 0 2
duber source
share