i have table 5 columns :
pk_column, fk_meterid, date, index, value
and have query working fine :
select fk_meterid, sum(value) 'total', @p_month, @p_year valueshistory month(date) = @p_month , year(date) = @p_year group fk_meterid
however instead of summing values on month, do
a difference of indexes between last index of month , first index of
month. (it should return same result)
how can make query using difference ?
thanks
sounds if looking aggregate functions.
with cte ( select rn = row_number() on ( partition fk_meterid order date, pk_column), total = sum(value) on (partition fk_meterid), minval = min(value) on (partition fk_meterid), maxval = max(value) on (partition fk_meterid), pk_column, fk_meterid, [date], [index], value valueshistory month(date) = @p_month , year(date) = @p_year ) select difference = maxval - minval, cte.* cte
Comments
Post a Comment