sql - over partition by without an aggregate function, avoiding group by -
is there way in use window function over (partition column)
without using aggregate function?
i have lots of columns , i don't want use group by since i'll have specify both in select , in group by.
i giving syntax example, needs somehow corrected (by guys cause it's not working when adapt on real query(real query long , time consuming explain go example)).
let's assume works:
select *, ( select sum (column1) on (partition column2) sumcol1 mytable column20 = column21 ) mytable
ok, want same thing 2 changes:
1: no aggregate function
2: column1
time date
(i wouldn't able use aggregate function date
far know, trying eliminate aggregate, shouldn't matter.)
what want should somehow (query not correct, since i'm trying achieve)
select *, ( select column1 on (partition column2) col1new mytable column20 = column21 ) mytable
sql server 2012
thanks
edit:
sample data:
rn rd rnc d e name abc1m 2010-03-31 abc 5.7 2 blue abc3m 2010-04-15 abc 5.7 3 blue abc1y 2010-02-14 abc 5.7 4 blue xfx1m 2010-02-31 xfx 1.7 2 blue xfx3m 2010-03-24 xfx 1.7 1 blue xfx1y 2012-03-30 xfx 1.7 1.7 red <= d=e use date "red" rows tnt1m 2010-03-28 tnt 9.6 2 red tnt3m 2010-01-12 tnt 9.6 9.6 blue <= d=e use date "blue" rows tnt1y 2010-08-20 tnt 9.6 2 red
expected table, please @ expectedcol
rn rd rnc d e name expectedcol abc1m 2010-03-31 abc 5.7 2 blue 2010-01-12 abc3m 2010-04-15 abc 5.7 3 blue 2010-01-12 abc1y 2010-02-14 abc 5.7 4 blue 2010-01-12 xfx1m 2010-02-31 xfx 1.7 2 blue 2010-01-12 xfx3m 2010-03-24 xfx 1.7 1 blue 2010-01-12 xfx1y 2012-03-30 xfx 1.7 1.7 red 2012-03-30 tnt1m 2010-03-28 tnt 9.6 2 red 2012-03-30 tnt3m 2010-01-12 tnt 9.6 9.6 blue 2010-01-12 tnt1y 2010-08-20 tnt 9.6 2 red 2012-03-30
the logic this: when d = e @ rd , take date , put expectedcol1 group name
so, want write soemthing this:
select *, (select rd on (partition name) expectedcol1 mytable d = e) mytable
just calculate rd
each name
d = e
with mydate ( select name, rd yourtable d = e ) select t.*, m.rd expectedcol yourtable t join mydate m on t.name = m.name
Comments
Post a Comment