Database/MSSQL

LAG 편차 쿼리

sonny.kim 2019. 3. 25. 11:16

[테스트]
1. 테스트 환경 셋팅

-- 데이터베이스 생성

create database TESTDB 

use testdb
go

-- 임시 테이블 생성
-- drop table tab_01
create table Tab_01 (
    seqNo int identity(1,1)
,   compareValue int default 0);
go

create clustered index idx_Tab_01_seqNo on Tab_01 (seqNo)
go

-- 20개의 compareValue 랜덤값을 가지는 데이터 입력
insert into tab_01 (compareValue) values (rand()*1000)
go 20

-- 쿼리 성능 보기 위한 옵션 설정
set statistics io on
set statistics time on

2-1. LAG 함수 사용 (Case 1)
select
    seqNo
,   compareValue
,   lag(compareValue, 1, 0) over (order by seqNo) as LagValue
from tab_01 with (readuncommitted)
order by seqNo



출처: https://nexondbteam.tistory.com/121?category=13105 [데이터베이스]

[테스트]
1. 테스트 환경 셋팅

-- 데이터베이스 생성

create database TESTDB 

use testdb
go

-- 임시 테이블 생성
-- drop table tab_01
create table Tab_01 (
    seqNo int identity(1,1)
,   compareValue int default 0);
go

create clustered index idx_Tab_01_seqNo on Tab_01 (seqNo)
go

-- 20개의 compareValue 랜덤값을 가지는 데이터 입력
insert into tab_01 (compareValue) values (rand()*1000)
go 20

-- 쿼리 성능 보기 위한 옵션 설정
set statistics io on
set statistics time on

2-1. LAG 함수 사용 (Case 1)
select
    seqNo
,   compareValue
,   lag(compareValue, 1, 0) over (order by seqNo) as LagValue
from tab_01 with (readuncommitted)
order by seqNo



출처: https://nexondbteam.tistory.com/121?category=13105 [데이터베이스]

[테스트]
1. 테스트 환경 셋팅

-- 데이터베이스 생성

create database TESTDB 

use testdb
go

-- 임시 테이블 생성
-- drop table tab_01
create table Tab_01 (
    seqNo int identity(1,1)
,   compareValue int default 0);
go

create clustered index idx_Tab_01_seqNo on Tab_01 (seqNo)
go

-- 20개의 compareValue 랜덤값을 가지는 데이터 입력
insert into tab_01 (compareValue) values (rand()*1000)
go 20

-- 쿼리 성능 보기 위한 옵션 설정
set statistics io on
set statistics time on

2-1. LAG 함수 사용 (Case 1)
select
    seqNo
,   compareValue
,   lag(compareValue, 1, 0) over (order by seqNo) as LagValue
from tab_01 with (readuncommitted)
order by seqNo



출처: https://nexondbteam.tistory.com/121?category=13105 [데이터베이스]

[테스트]
1. 테스트 환경 셋팅

-- 데이터베이스 생성

create database TESTDB 

use testdb
go

-- 임시 테이블 생성
-- drop table tab_01
create table Tab_01 (
    seqNo int identity(1,1)
,   compareValue int default 0);
go

create clustered index idx_Tab_01_seqNo on Tab_01 (seqNo)
go

-- 20개의 compareValue 랜덤값을 가지는 데이터 입력
insert into tab_01 (compareValue) values (rand()*1000)
go 20

-- 쿼리 성능 보기 위한 옵션 설정
set statistics io on
set statistics time on

2-1. LAG 함수 사용 (Case 1)
select
    seqNo
,   compareValue
,   lag(compareValue, 1, 0) over (order by seqNo) as LagValue
from tab_01 with (readuncommitted)
order by seqNo



출처: https://nexondbteam.tistory.com/121?category=13105 [데이터베이스]

use test;
go

create table tab_01(
    seqNo    int    identity(1,1)
    , compareValue    int default 0);

go

create clustered index ids_tab_01_seqno    on tabl_01 (seqNo)
go

insert into tab_01(compareValue) values (rand()* 1000)
go 20

set statistics io on
set statistics time on

--편차쿼리
select
    seqNo
,   compareValue
,   lag(compareValue, 1, 0) over (order by seqNo) as LagValue
from tab_01 with (readuncommitted)
order by seqNo


-- 다른 편차 쿼리(SUM 이용)

SELECT
    billing_date,
    SUM(price) as price,
    lag(sum(price), 1,0) over (order by billing_date) as lagValue
FROM [dbo].[tbl_billing]
GROUP BY billing_date
ORDER BY billing_date