|
Re: Is it possible to sort data prior to computing columns? [message #1229171 is a reply to message #1227149] |
Wed, 08 January 2014 21:36 |
Vassili Safonov Messages: 7 Registered: May 2013 |
Junior Member |
|
|
Do everything in SQL.
declare @hr_cnt int,
@start_hr int,
@hr_add int
create table #tmp_tbl(
hr_num int identity,
hr int,
vl int null
)
select @hr_cnt = 0, @hr_add = 0
select @start_hr = datepart(hh,getdate())
while @hr_cnt < 24
begin
if @start_hr+@hr_add > 23
select @start_hr = 0,@hr_add = 0
insert #tmp_tbl
select @start_hr+@hr_add, @hr_cnt --mock values
select @hr_cnt = @hr_cnt + 1,@hr_add = @hr_add + 1
end
-- This is your final select. everything before is just a prep for this example
select a.hr_num, a.hr,a.vl,sum(b.vl) s_vl
from #tmp_tbl a,#tmp_tbl b
where b.hr_num <= a.hr_num
group by a.hr_num, a.hr,a.vl
order by 1
drop table #tmp_tbl
|
|
|
Powered by
FUDForum. Page generated in 0.01749 seconds