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

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
pinned-register now
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