Hello all!I have he table with the rates for currency pair for the last 12h. I wish to calculate with SQL query the Simpla=e mving average and Exponencial moving average for some period. Help me please wit this query.

declare @t table (date datetime not null, rate float not null)
insert @t
select getdate()-10, 1 union all
select getdate()-9, 2 union all
select getdate()-8, 4 union all
select getdate()-7, 4 union all
select getdate()-6, 5 union all
select getdate()-5, 6 union all
select getdate()-4, 6 union all
select getdate()-3, 8 union all
select getdate()-2, 9 union all
select getdate()-1, 10 union all
select getdate()-0, 11 union all
select getdate()+1, 9
select * from @t
-- change the upper limit of the between clause to
-- specify how far back the moving average looks. also if your moving average looks
-- back minutes rather than days, you'll have to change the first arg to datediff.
select
t.date,
avg(tt.rate) as simpleMovingAvg
from @t AS t
join @t tt on DATEDIFF(day, tt.date, t.date) between 0 And 2
group by t.date
order by t.date
-- add more when clauses to extend how far back the weighted moving average looks.
-- adjust weights however you like, with the constraint that they all sum to 1.
-- note that you should exclude the first 2 values returned by this since they don't have
-- a full set of history - a minor boundary effect.
select
t.date,
sum(case when DATEDIFF(day, tt.date, t.date)=0 then 0.7*tt.rate
when DATEDIFF(day, tt.date, t.date)=1 then 0.2*tt.rate
when DATEDIFF(day, tt.date, t.date)=2 then 0.1*tt.rate end) as weightedMovingAvg
from @t t
join @t tt on DATEDIFF(day, tt.date, t.date) between 0 And 2
group by t.date
order by t.date

yea, moving averages (like running totals) are a case where the set-based approach is slower than a procedural approach.

that is, a cursor may be faster (gasp!) since you would only have to read each row once, and there would be no join. To do the moving average as I have done above, each row is read 3 times, plus you have the extra overhead of a join. I'm too lazy to give you the cursor-based solution though. that's left as an exercise for you