I need to do a sumproduct in SQL Server
ALL_HOSPITALS table
days percentage days avoidable
1 0.244
2 0.127
30 0.890
The calculation would be
0.244*1+0.127*2+0.890*30
The total of the above has to be divided by the sum of days.
I am planning on having a temp table with the hosp name field and the multiplication field only as follows:
INSERT INTO temp_mul_days(hosp_name_key, mul_days)
select ah.hosp_name_key,(ah.per_total_days_avoidable*ah.days)
from all_hospitals ah
left join rft_hosp_name rhn on rhn.hosp_name_key = ah.hosp_name_key
LEFT JOIN RFT_MED_GROUP MG ON MG.MED_GROUP_KEY=AH.MED_GROUP_KEY
where mg.med_group_type = 1
group by ah.hosp_name_key
I am getting the error
Server: Msg 8120, Level 16, State 1, Line 1
Column 'ah.PER_TOTAL_DAYS_AVOIDABLE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'ah.DAYS' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can I avoid this?
After this query I was planning on having a second temp table with the sum of the results of the above and then another temp table with the sum of the denominator.
The fourth temp table would be the division between the second and third table.
Anybody has a better idea on how to do this?
Thank you!
Tammy
ALL_HOSPITALS table
days percentage days avoidable
1 0.244
2 0.127
30 0.890
The calculation would be
0.244*1+0.127*2+0.890*30
The total of the above has to be divided by the sum of days.
I am planning on having a temp table with the hosp name field and the multiplication field only as follows:
INSERT INTO temp_mul_days(hosp_name_key, mul_days)
select ah.hosp_name_key,(ah.per_total_days_avoidable*ah.days)
from all_hospitals ah
left join rft_hosp_name rhn on rhn.hosp_name_key = ah.hosp_name_key
LEFT JOIN RFT_MED_GROUP MG ON MG.MED_GROUP_KEY=AH.MED_GROUP_KEY
where mg.med_group_type = 1
group by ah.hosp_name_key
I am getting the error
Server: Msg 8120, Level 16, State 1, Line 1
Column 'ah.PER_TOTAL_DAYS_AVOIDABLE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'ah.DAYS' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can I avoid this?
After this query I was planning on having a second temp table with the sum of the results of the above and then another temp table with the sum of the denominator.
The fourth temp table would be the division between the second and third table.
Anybody has a better idea on how to do this?
Thank you!
Tammy