-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlag_function.sql
More file actions
55 lines (43 loc) · 1.13 KB
/
Copy pathlag_function.sql
File metadata and controls
55 lines (43 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
select top 100
dt
,month(Dt)
,[GC=F_Close]
,lag([GC=F_Close]) over (order by month(dt)) as [prev_dt]
,lag([GC=F_Close]) over(order by year(dt)) as [prev_yr)
from dbo.Stocks
group by month(dt)
order by dt desc
;with lst_mnth_gld
as
(
select top 100
s.dt
,DATEPART(MONTH, s.dt) mnth
,sum(s.[GC=F_Close]) as [gld_close]
from dbo.Stocks s
group by datepart(month, s.dt), s.dt
order by s.dt,DATEPART(MONTH, s.dt) desc
)select
dt
,mnth
,lag(gld_close, 1, 0) over (order by mnth) [lst_mnt]
,lag(gld_close, 1, 0) over (order by year(dt)) [lst_yr]
,lag(dt) over (order by year(dt)) lst_Yr
,gld_close
from lst_mnth_gld
select top 100 * from dbo.Stocks
select
FullDateAlternateKey ,
lag(FullDateAlternateKey, 1) over (order by FullDateAlternateKey )
from AdventureWorksDW2019.dbo.DimDate
alter table dbo.Stocks add dt date
update dbo.stocks
update dbo.Stocks set dt = format(Date_ , 'yyyy-MM-dd')
select DATEPART(i, getdate())
select format(getdate(), 'yyyy-MM-dd')
select
rank() over (order by SI_Close desc)
,dt
from dbo.Commodity
update dbo.Commodity set dt = convert(date , dt, 101)
select convert(date, getdate(), 101)