How do I convert below mentioned postgresql query into HiveSql
SELECT COUNT(user_id) Total_profiles, COUNT(user_id) FITLER (WHERE profile_date BETWEEN '2020-01-01' AND '2020-03-31') as Q1_profile, COUNT(user_id) FITLER (WHERE profile_date BETWEEN '2020-04-01' AND '2020-06-30') as Q2_profile, COUNT(user_id) FITLER (WHERE profile_date BETWEEN '2020-07-01' AND '2020-09-30') as Q3_profile, COUNT(user_id) FITLER (WHERE profile_date BETWEEN '2020-010-01' AND '2020-12-31') as Q4_profile FROM customer_profiles WHERE profile_date BETWEEN '2020-01-01' AND '2020-12-31'
Advertisement
Answer
Use CASE expressions like this:
SELECT COUNT(user_id) Total_profiles, COUNT(case when profile_date BETWEEN '2020-01-01' AND '2020-03-31' then user_id else null end) as Q1_profile, COUNT(case when profile_date BETWEEN '2020-04-01' AND '2020-06-30' then user_id else null end) as Q2_profile, COUNT(case when profile_date BETWEEN '2020-07-01' AND '2020-09-30' then user_id else null end) as Q3_profile, COUNT(case when profile_date BETWEEN '2020-10-01' AND '2020-12-31' then user_id else null end) as Q4_profile FROM customer_profiles WHERE profile_date BETWEEN '2020-01-01' AND '2020-12-31'
Also in Hive version >= 1.3 you can use quarter
function:
SELECT COUNT(user_id) Total_profiles, COUNT(case when quarter(profile_date)=1 then user_id else null end) as Q1_profile, COUNT(case when quarter(profile_date)=2 then user_id else null end) as Q2_profile, COUNT(case when quarter(profile_date)=3 then user_id else null end) as Q3_profile, COUNT(case when quarter(profile_date)=4 then user_id else null end) as Q4_profile FROM customer_profiles WHERE profile_date BETWEEN '2020-01-01' AND '2020-12-31'