Friday, 27 September 2013

SQL: How to update multiple values using a subquery that has group by

SQL: How to update multiple values using a subquery that has group by

I have the below table:
row_date logid type Interval availtime avail_distribution 9/25/2013 122 4
850 640 NULL 9/25/2013 122 5 850 0 NULL 9/25/2013 122 7 850 0 NULL
9/26/2013 122 4 850 500 NULL 9/26/2013 122 5 850 0 NULL 9/26/2013 122 7
850 0 NULL
Here EACH of avail_distribution is to be updated by the average of the
'availtime', group by row_date,logid and Interval.
The required table after update:
row_date logid type Interval availtime avail_distribution 9/25/2013 122 4
850 640 213.3333 9/25/2013 122 5 850 0 213.3333 9/25/2013 122 7 850 0
213.3333 9/26/2013 122 4 850 500 167.66667 9/26/2013 122 5 850 0 167.66667
9/26/2013 122 7 850 0 167.66667



Select sum(availtime)/count(availtime) from table group by
row_date,logid,Interval gives me the values that i am seeking.. but I am
not able to update the avail_distribution column the way i am intending.

No comments:

Post a Comment