SQL Server 2008 - error using OVER with GROUP BY -


this query works flawless @ sql server 2014 (http://sqlfiddle.com/#!6/19de4/1), i'm having problems 2008 version, complains "incorrect syntax near 'order'. missing?

create table t (id bigint, cliente varchar(100), vendas float);  insert t values (1, 'vitor', 234.3); insert t values (2, 'emerson', 456.2); insert t values (3, 'thiago', 6789.3); insert t values (4, 'john', 5423.0); insert t values (5, 'fulano', 3467.8);  select *, case when vendas_agrupadas <= 0.7 'a' else  (case when vendas_agrupadas <= 0.9 'b' else 'c' end) end  "grupo abc" (   select *, sum(vendas) on (order vendas desc)   /(sum(vendas) over()) vendas_agrupadas t) asdf; 

sql server 2008 doesn't support cumulative sums directly. following version subquery should want:

select *,        (case when vendas_agrupadas <= 0.7 'a'               when vendas_agrupadas <= 0.9 'b'              else 'c'         end) "grupo abc" (select *,              (select sum(vendas)               t t2               t2.vendas >= t.vendas               ) / (sum(vendas) over()) vendas_agrupadas       t      ) asdf; 

some notes:

  • this may not produce same results if there duplicated values of vendas.
  • case can accept multiple when, don't need nested case statements.

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -