[MSSQL] 쿼리 문의드립니다. 0 2 883

by 라슈 [SQLServer] [2017.04.13 13:41:27]


시간 카운터
05 2
06 4
07 6
08 12
09 36
10 49
11 73
12 117
13 139
14 174
15 204
16 219
17 235
18 245
19 253
20 256
21 257
22 260
23 266

위와같은 테이블이 있을때 아래와 같이 바로 뒤 시간에어 앞시간을 빼서 자료를 만들고 싶습니다,

원 데이터가 누적 데이터이므로  05시 가 2건 이고 06시가 4건이므로, 06시는 2건 이런식으로 만들고 싶습니다, 

고수님들 부탁드립니다.

시간 카운터
05 2
06 2
07 2
08 6
09 24
10 13
11 24
12 44
13 22
14 35
15 30
16 15
17 16
18 10
19 8
20 3
21 1
22 3
23 6
by jkson [2017.04.13 14:11:33]
with t as
(
select '05' hh, 2   cnt  union all
select '06' hh, 4   cnt  union all
select '07' hh, 6   cnt  union all
select '08' hh, 12  cnt  union all
select '09' hh, 36  cnt  union all
select '10' hh, 49  cnt  union all
select '11' hh, 73  cnt  union all
select '12' hh, 117 cnt  union all
select '13' hh, 139 cnt  union all
select '14' hh, 174 cnt  union all
select '15' hh, 204 cnt  union all
select '16' hh, 219 cnt  union all
select '17' hh, 235 cnt  union all
select '18' hh, 245 cnt  union all
select '19' hh, 253 cnt  union all
select '20' hh, 256 cnt  union all
select '21' hh, 257 cnt  union all
select '22' hh, 260 cnt  union all
select '23' hh, 266 cnt 
)
select hh, cnt - isnull(lag(cnt) over(order by hh),0) cnt 
  from t
 order by hh

 


by 라슈 [2017.04.13 14:25:52]

감사합니다.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입