Calculate % using OVER() for each row in SQL Server 2012 -
i needed guidance question on how calculate percentage between 2 cells on each row. on sql server 2012. below structure of current dataset.
customer_number price_last_year price_today --------------------------------------------------- 909523 154 190 20175808 154 100 21808187 154 190 22792798 184 284 23256155 154 230
basically, trying include additional column calculates percentage of (price_today)/price_last_year)
each specific row. using windowing over()
function , has not worked properly.
this how want new dataset like:
customer_number price_last_year price_today percentage_as oftoday% 909523 154 190 23.3% 20175808 154 100 -35.1% 21808187 154 190 23.3% 22792798 184 284 54.3% 23256155 154 230 49.3%
this query percentage calculation:
select distinct customer_number, price_last_year, price_today, [percentage of today%]=sum(price_today)100.0/(price_last_year) over() tabl2 group customer_number, price_today, price_last_year
i have tried number of variations percentage over()
calculation. tried [percentage of today%]=sum(price_today)100.0/(price_last_year) over() (partition customer_number)
, , throws error.
how can calculate percentage using over()
each row?
if calculation between 2 columns of same row don't need over()
clause or group by
clause - divide two. subtracting 1 , multiplying 100 should give nice percentage representation:
select customer_number, price_last_year, price_today, (1 - (price_today/price_last_year)) * 100 [percentage of today%] tabl2
Comments
Post a Comment