comparing with today and yesterday record in sql query

Solutions on MaxInterview for comparing with today and yesterday record in sql query by the best coders in the world

showing results for - "comparing with today and yesterday record in sql query"
Berenice
03 Jul 2018
1declare @bgn_dt date = '2017-12-15' --set by OP
2    , @end_dt date = '2017-12-22' --set by OP
3    , @lag_dt date;
4
5set @lag_dt = (select max(MyDate) from #myTable where MyDate < @bgn_dt) --get the "yesterday" that the @bgn_dt will need
6
7select a.MyDate
8, a.SalesTotal
9, format(((1.0 * a.SalesTotal) / a.SalesTotalPrevDay) - 1, '0%') as SalesTotalChange
10from (
11    select t.MyDate
12    , t.SalesTotal
13    , lag(t.SalesTotal, 1, NULL) over (/*partition by (if needed)*/ order by t.MyDate asc) as SalesTotalPrevDay
14    from #myTable as t
15    where 1=1
16    and t.MyDate between @lag_dt and @end_dt
17    ) as a
18where 1=1
19and a.MyDate >= @bgn_dt
20