I am tried several approaches to this and searched for an answer but haven't found anything so here goes.
I have been tasked with getting the result from an activity tracking table for the first 30 days of activity. Problem is each user
has a different activity start date. The results are calculated daily so there are many rows per user. What I have done is to try to use a min function on the date and a dateadd function to get 1 month from the min and only take those rows and sum them. I get the dreaded
"An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."
Below is my current query:
SELECT distinct tbl1_cst_key as CSTKEY,
tbl1_activity_date,
sum(tbl1_summary_steps) AS TotalSteps,
sum(tbl1_summary_floors)AS TotalFloors,
sum(tbl1_summary_distance_total)AS TotalDistance,
sum(tbl1_summary_activeScore)AS TotalActiveScore,
sum(tbl1_summary_sedentaryMinutes)AS TotalSedentaryMinutes,
sum(tbl1_summary_fairlyActiveMinutes)As TotalFairlyActiveMinutes,
sum(tbl1_summary_veryActiveMinutes)AS TotalVeryActiveMinutes,
sum(tbl1_summary_distance_lightlyActive)AS TotalLighlyActiveDistance,
sum(tbl1_summary_distance_moderatelyActive)AS TotalModeratelyActiveDistance,
sum(tbl1_summary_distance_sedentaryActive)AS TotalSedentaryActiveDistance,
sum(tbl1_summary_distance_veryActive) AS TotalVeryActiveDistance
from
tbl1_activity_log
GROUP BY tbl1_ind_cst_key,tbl1_activity_date
HAVING tbl1_activity_date between convert(varchar(10),MIN(tbl1_activity_date),101)
and convert(varchar(10),DATEADD(month,1,MIN(tbl1_activity_date)),101)
I have been tasked with getting the result from an activity tracking table for the first 30 days of activity. Problem is each user
has a different activity start date. The results are calculated daily so there are many rows per user. What I have done is to try to use a min function on the date and a dateadd function to get 1 month from the min and only take those rows and sum them. I get the dreaded
"An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."
Below is my current query:
SELECT distinct tbl1_cst_key as CSTKEY,
tbl1_activity_date,
sum(tbl1_summary_steps) AS TotalSteps,
sum(tbl1_summary_floors)AS TotalFloors,
sum(tbl1_summary_distance_total)AS TotalDistance,
sum(tbl1_summary_activeScore)AS TotalActiveScore,
sum(tbl1_summary_sedentaryMinutes)AS TotalSedentaryMinutes,
sum(tbl1_summary_fairlyActiveMinutes)As TotalFairlyActiveMinutes,
sum(tbl1_summary_veryActiveMinutes)AS TotalVeryActiveMinutes,
sum(tbl1_summary_distance_lightlyActive)AS TotalLighlyActiveDistance,
sum(tbl1_summary_distance_moderatelyActive)AS TotalModeratelyActiveDistance,
sum(tbl1_summary_distance_sedentaryActive)AS TotalSedentaryActiveDistance,
sum(tbl1_summary_distance_veryActive) AS TotalVeryActiveDistance
from
tbl1_activity_log
GROUP BY tbl1_ind_cst_key,tbl1_activity_date
HAVING tbl1_activity_date between convert(varchar(10),MIN(tbl1_activity_date),101)
and convert(varchar(10),DATEADD(month,1,MIN(tbl1_activity_date)),101)