I have fitness data exported from my smartwatch. It has columns Date, Step_count, and others (which are not required now).
I am trying to organize my activity based on steps as very low, low, moderate, high and very high.
My syntaxes are:
for very low activity:
select Date, Step_count as very_low_physical_activity from my-second-project-370721.my_activity_2022.my_activity where Step_count<1000
for low activity:
select Date, Step_count as low_physical_activity from my-second-project-370721.my_activity_2022.my_activity where Step_count BETWEEN 1000 and 2500
and so on for moderate (2500-5000), high (5000-10000) and very high (>10000)
How do I join these queries so that I get all the activity reports in one go?
I tried:
(select Date, Step_count as very_low_physical_activity from my-second-project-370721.my_activity_2022.my_activity where Step_count<1000)
UNION ALL
(select Date, Step_count as low_physical_activity from my-second-project-370721.my_activity_2022.my_activity where Step_count BETWEEN 1000 and 2500)
order_by Date
but am getting results with column as very_low_physical_activity where steps between 0 - 2500 are populated.
Using CASE
is a better approach here instead of the UNION ALL
select
Date,
Step_count,
case
when Step_count < 1000 THEN 'very_low_physical_activity'
when Step_count >= 1000 and Step_count < 2500 THEN 'low_physical_activity'
when Step_count >= 2500 and Step_count < 5000 THEN 'moderate_physical_activity'
when Step_count >= 5000 and Step_count < 10000 THEN 'high_physical_activity'
else 'very_high_physical_activity'
end as Physical_activity
from my-second-project-370721.my_activity_2022.my_activity
Showing in multiple columns:
select
Date,
Step_count,
case when step_count < 1000 then step_count end as very_low_physical_activity,
case when step_count >= 1000 and step_count < 2500 then step_count end as low_physical_activity,
case when Step_count >= 2500 and Step_count < 5000 then step_count end as moderate_physical_activity,
case when Step_count >= 5000 and Step_count < 10000 then step_count end as high_physical_activity,
case when step_count >= 1000 then step_count end as very_high_physical_activity
from `my-second-project-370721.my_activity_2022.my_activity`