1시간 마다 누적값 차이를 구하고 싶습니다. 0 3 7,316

by tosswin [SQL Query] MSSQL [2024.10.16 10:00:00]


다음과 같이 테이블 데이터가 있습니다.

run_dt accu
2021-01-01 0:00 12
2021-01-01 0:10 27
2021-01-01 0:20 42
2021-01-01 0:30 52
2021-01-01 0:40 63
2021-01-01 0:50 68
2021-01-01 1:00 80
2021-01-01 1:10 90
2021-01-01 1:20 98
2021-01-01 1:30 110
2021-01-01 1:40 120
2021-01-01 1:50 125
2021-01-01 2:00 134
2021-01-01 2:10 145
2021-01-01 2:20 151
2021-01-01 2:30 164
2021-01-01 2:40 179
2021-01-01 2:50 191
2021-01-01 3:00 203
2021-01-01 3:10 215
2021-01-01 3:20 226
2021-01-01 3:30 233
2021-01-01 3:40 242
2021-01-01 3:50 250
2021-01-01 4:00 260
2021-01-01 4:10 272
2021-01-01 4:20 281
2021-01-01 4:30 292
2021-01-01 4:40 303
2021-01-01 4:50 313
2021-01-01 5:00 325
2021-01-01 5:10 334
2021-01-01 5:20 349
2021-01-01 5:30 362
2021-01-01 5:40 368
2021-01-01 5:50 379
2021-01-01 6:00 393
2021-01-01 6:10 405
2021-01-01 6:20 417
2021-01-01 6:30 424
2021-01-01 6:40 438
2021-01-01 6:50 451
2021-01-01 7:00 461
2021-01-01 7:10 476
2021-01-01 7:20 483
2021-01-01 7:30 497
2021-01-01 7:40 510
2021-01-01 7:50 525
2021-01-01 8:00 535
2021-01-01 8:10 541
2021-01-01 8:20 546
2021-01-01 8:30 557
2021-01-01 8:40 568
2021-01-01 8:50 574
2021-01-01 9:00 579
2021-01-01 9:10 589
2021-01-01 9:20 596
2021-01-01 9:30 601
2021-01-01 9:40 612
2021-01-01 9:50 627
2021-01-01 10:00 633
2021-01-01 10:10 646
2021-01-01 10:20 659
2021-01-01 10:30 674
2021-01-01 10:40 687
2021-01-01 10:50 694
2021-01-01 11:00 699
2021-01-01 11:10 706
2021-01-01 11:20 719
2021-01-01 11:30 732
2021-01-01 11:40 741
2021-01-01 11:50 753
2021-01-01 12:00 763
2021-01-01 12:10 773
2021-01-01 12:20 779
2021-01-01 12:30 784
2021-01-01 12:40 791
2021-01-01 12:50 803
2021-01-01 13:00 815
2021-01-01 13:10 828
2021-01-01 13:20 840
2021-01-01 13:30 853
2021-01-01 13:40 862
2021-01-01 13:50 872
2021-01-01 14:00 879
2021-01-01 14:10 893
2021-01-01 14:20 900
2021-01-01 14:30 907
2021-01-01 14:40 915
2021-01-01 14:50 927
2021-01-01 15:00 932
2021-01-01 15:10 946
2021-01-01 15:20 955
2021-01-01 15:30 963
2021-01-01 15:40 968
2021-01-01 15:50 973
2021-01-01 16:00 985
2021-01-01 16:10 999
2021-01-01 16:20 1011
2021-01-01 16:30 1024
2021-01-01 16:40 1034
2021-01-01 16:50 1047
2021-01-01 17:00 1054
2021-01-01 17:10 1066
2021-01-01 17:20 1075
2021-01-01 17:30 1087
2021-01-01 17:40 1101
2021-01-01 17:50 1112
2021-01-01 18:00 1119
2021-01-01 18:10 1129
2021-01-01 18:20 1139
2021-01-01 18:30 1152
2021-01-01 18:40 1161
2021-01-01 18:50 1173
2021-01-01 19:00 1182
2021-01-01 19:10 1194
2021-01-01 19:20 1201
2021-01-01 19:30 1209
2021-01-01 19:40 1219
2021-01-01 19:50 1229
2021-01-01 20:00 1236
2021-01-01 20:10 1251
2021-01-01 20:20 1260
2021-01-01 20:30 1273
2021-01-01 20:40 1288
2021-01-01 20:50 1296
2021-01-01 21:00 1308
2021-01-01 21:10 1316
2021-01-01 21:20 1326
2021-01-01 21:30 1337
2021-01-01 21:40 1352
2021-01-01 21:50 1358
2021-01-01 22:00 1363
2021-01-01 22:10 1373
2021-01-01 22:20 1383
2021-01-01 22:30 1391
2021-01-01 22:40 1405
2021-01-01 22:50 1411
2021-01-01 23:00 1424
2021-01-01 23:10 1434
2021-01-01 23:20 1441
2021-01-01 23:30 1452
2021-01-01 23:40 1457
2021-01-01 23:50 1467
2021-01-02 0:00 1475
2021-01-02 0:10 1480
2021-01-02 0:20 1489
2021-01-02 0:30 1499
2021-01-02 0:40 1508
2021-01-02 0:50 1513
2021-01-02 1:00 1522
2021-01-02 1:10 1531
2021-01-02 1:20 1544
2021-01-02 1:30 1549
2021-01-02 1:40 1563
2021-01-02 1:50 1571
2021-01-02 2:00 1581
2021-01-02 2:10 1595
2021-01-02 2:20 1609
2021-01-02 2:30 1620
2021-01-02 2:40 1630
2021-01-02 2:50 1643
2021-01-02 3:00 1656
2021-01-02 3:10 1671
2021-01-02 3:20 1686
2021-01-02 3:30 1694
2021-01-02 3:40 1705
2021-01-02 3:50 1710
2021-01-02 4:00 1719
2021-01-02 4:10 1734
2021-01-02 4:20 1749
2021-01-02 4:30 1755
2021-01-02 4:40 1766
2021-01-02 4:50 1781
2021-01-02 5:00 1791
2021-01-02 5:10 1796
2021-01-02 5:20 1804
2021-01-02 5:30 1816
2021-01-02 5:40 1825
2021-01-02 5:50 1839
2021-01-02 6:00 1848
2021-01-02 6:10 1861
2021-01-02 6:20 1870
2021-01-02 6:30 1875
2021-01-02 6:40 1882
2021-01-02 6:50 1893
2021-01-02 7:00 1899
2021-01-02 7:10 1905
2021-01-02 7:20 1912
2021-01-02 7:30 1926
2021-01-02 7:40 1939
2021-01-02 7:50 1953
2021-01-02 8:00 1961
2021-01-02 8:10 1969
2021-01-02 8:20 1977
2021-01-02 8:30 1987
2021-01-02 8:40 1999
2021-01-02 8:50 2004
2021-01-02 9:00 2012
2021-01-02 9:10 2021
2021-01-02 9:20 2029
2021-01-02 9:30 2039
2021-01-02 9:40 2044
2021-01-02 9:50 2052
2021-01-02 10:00 2067
2021-01-02 10:10 2076
2021-01-02 10:20 2084
2021-01-02 10:30 2093
2021-01-02 10:40 2108
2021-01-02 10:50 2121
2021-01-02 11:00 2130
2021-01-02 11:10 2141
2021-01-02 11:20 2152
2021-01-02 11:30 2157
2021-01-02 11:40 2164
2021-01-02 11:50 2173
2021-01-02 12:00 2182
2021-01-02 12:10 2197
2021-01-02 12:20 2205
2021-01-02 12:30 2214
2021-01-02 12:40 2225
2021-01-02 12:50 2234
2021-01-02 13:00 2242
2021-01-02 13:10 2254
2021-01-02 13:20 2260
2021-01-02 13:30 2272
2021-01-02 13:40 2278
2021-01-02 13:50 2287
2021-01-02 14:00 2293
2021-01-02 14:10 2299
2021-01-02 14:20 2313
2021-01-02 14:30 2318
2021-01-02 14:40 2329
2021-01-02 14:50 2334
2021-01-02 15:00 2341
2021-01-02 15:10 2348
2021-01-02 15:20 2362
2021-01-02 15:30 2375
2021-01-02 15:40 2380
2021-01-02 15:50 2390
2021-01-02 16:00 2401
2021-01-02 16:10 2413
2021-01-02 16:20 2426
2021-01-02 16:30 2434
2021-01-02 16:40 2442
2021-01-02 16:50 2449
2021-01-02 17:00 2464
2021-01-02 17:10 2477
2021-01-02 17:20 2485
2021-01-02 17:30 2499
2021-01-02 17:40 2511
2021-01-02 17:50 2525
2021-01-02 18:00 2533
2021-01-02 18:10 2542
2021-01-02 18:20 2557
2021-01-02 18:30 2566
2021-01-02 18:40 2575
2021-01-02 18:50 2583
2021-01-02 19:00 2597
2021-01-02 19:10 2603
2021-01-02 19:20 2613
2021-01-02 19:30 2623
2021-01-02 19:40 2636
2021-01-02 19:50 2648
2021-01-02 20:00 2660
2021-01-02 20:10 2673
2021-01-02 20:20 2682
2021-01-02 20:30 2697
2021-01-02 20:40 2702
2021-01-02 20:50 2712
2021-01-02 21:00 2726
2021-01-02 21:10 2732
2021-01-02 21:20 2745
2021-01-02 21:30 2760
2021-01-02 21:40 2774
2021-01-02 21:50 2784
2021-01-02 22:00 2795
2021-01-02 22:10 2803
2021-01-02 22:20 2812
2021-01-02 22:30 2825
2021-01-02 22:40 2836
2021-01-02 22:50 2842
2021-01-02 23:00 2853
2021-01-02 23:10 2865
2021-01-02 23:20 2877
2021-01-02 23:30 2882
2021-01-02 23:40 2896
2021-01-02 23:50 2908
2021-01-03 0:00 2914
2021-01-03 0:10 2921
2021-01-03 0:20 2933
2021-01-03 0:30 2945
2021-01-03 0:40 2960
2021-01-03 0:50 2967
2021-01-03 1:00 2972
2021-01-03 1:10 2977
2021-01-03 1:20 2990
2021-01-03 1:30 2999
2021-01-03 1:40 3007
2021-01-03 1:50 3012
2021-01-03 2:00 3025
2021-01-03 2:10 3031
2021-01-03 2:20 3045
2021-01-03 2:30 3050
2021-01-03 2:40 3056
2021-01-03 2:50 3063
2021-01-03 3:00 3077
2021-01-03 3:10 3088
2021-01-03 3:20 3098
2021-01-03 3:30 3104
2021-01-03 3:40 3113
2021-01-03 3:50 3121
2021-01-03 4:00 3136
2021-01-03 4:10 3151
2021-01-03 4:20 3157
2021-01-03 4:30 3170
2021-01-03 4:40 3181
2021-01-03 4:50 3195
2021-01-03 5:00 3201
2021-01-03 5:10 3208
2021-01-03 5:20 3217
2021-01-03 5:30 3222
2021-01-03 5:40 3237
2021-01-03 5:50 3245
2021-01-03 6:00 3258
2021-01-03 6:10 3272
2021-01-03 6:20 3286
2021-01-03 6:30 3294
2021-01-03 6:40 3308
2021-01-03 6:50 3320
2021-01-03 7:00 3332
2021-01-03 7:10 3346
2021-01-03 7:20 3361
2021-01-03 7:30 3371
2021-01-03 7:40 3376
2021-01-03 7:50 3385
2021-01-03 8:00 3390
2021-01-03 8:10 3399
2021-01-03 8:20 3413
2021-01-03 8:30 3423
2021-01-03 8:40 3436
2021-01-03 8:50 3444
2021-01-03 9:00 3455
2021-01-03 9:10 3466
2021-01-03 9:20 3480
2021-01-03 9:30 3489
2021-01-03 9:40 3500
2021-01-03 9:50 3513
2021-01-03 10:00 3526
2021-01-03 10:10 3532
2021-01-03 10:20 3539
2021-01-03 10:30 3546
2021-01-03 10:40 3559
2021-01-03 10:50 3566
2021-01-03 11:00 3579
2021-01-03 11:10 3590
2021-01-03 11:20 3600
2021-01-03 11:30 3608
2021-01-03 11:40 3618
2021-01-03 11:50 3624
2021-01-03 12:00 3636
2021-01-03 12:10 3649
2021-01-03 12:20 3663
2021-01-03 12:30 3669
2021-01-03 12:40 3681
2021-01-03 12:50 3692
2021-01-03 13:00 3707
2021-01-03 13:10 3719
2021-01-03 13:20 3730
2021-01-03 13:30 3743
2021-01-03 13:40 3751
2021-01-03 13:50 3764
2021-01-03 14:00 3770
2021-01-03 14:10 3783
2021-01-03 14:20 3796
2021-01-03 14:30 3809
2021-01-03 14:40 3820
2021-01-03 14:50 3835
2021-01-03 15:00 3840
2021-01-03 15:10 3850
2021-01-03 15:20 3865
2021-01-03 15:30 3873
2021-01-03 15:40 3885
2021-01-03 15:50 3894
2021-01-03 16:00 3901
2021-01-03 16:10 3908
2021-01-03 16:20 3917
2021-01-03 16:30 3930
2021-01-03 16:40 3937
2021-01-03 16:50 3943
2021-01-03 17:00 3956
2021-01-03 17:10 3966
2021-01-03 17:20 3976
2021-01-03 17:30 3981
2021-01-03 17:40 3996
2021-01-03 17:50 4010
2021-01-03 18:00 4016
2021-01-03 18:10 4028
2021-01-03 18:20 4035
2021-01-03 18:30 4050
2021-01-03 18:40 4060
2021-01-03 18:50 4075
2021-01-03 19:00 4085
2021-01-03 19:10 4095
2021-01-03 19:20 4102
2021-01-03 19:30 4114
2021-01-03 19:40 4122
2021-01-03 19:50 4135
2021-01-03 20:00 4148
2021-01-03 20:10 4162
2021-01-03 20:20 4169
2021-01-03 20:30 4174
2021-01-03 20:40 4189
2021-01-03 20:50 4198
2021-01-03 21:00 4213
2021-01-03 21:10 4221
2021-01-03 21:20 4236
2021-01-03 21:30 4241
2021-01-03 21:40 4252
2021-01-03 21:50 4259
2021-01-03 22:00 4274
2021-01-03 22:10 4279
2021-01-03 22:20 4290
2021-01-03 22:30 4300
2021-01-03 22:40 4309
2021-01-03 22:50 4322
2021-01-03 23:00 4332
2021-01-03 23:10 4346
2021-01-03 23:20 4361
2021-01-03 23:30 4375
2021-01-03 23:40 4385
2021-01-03 23:50 4393
2021-01-04 0:00 4401
2021-01-04 0:10 4415
2021-01-04 0:20 4421
2021-01-04 0:30 4435
2021-01-04 0:40 4447
2021-01-04 0:50 4460
2021-01-04 1:00 4470
2021-01-04 1:10 4478
2021-01-04 1:20 4483
2021-01-04 1:30 4493
2021-01-04 1:40 4498
2021-01-04 1:50 4512
2021-01-04 2:00 4517
2021-01-04 2:10 4523
2021-01-04 2:20 4537
2021-01-04 2:30 4542
2021-01-04 2:40 4552
2021-01-04 2:50 4565
2021-01-04 3:00 4571
2021-01-04 3:10 4586
2021-01-04 3:20 4598
2021-01-04 3:30 4603
2021-01-04 3:40 4608
2021-01-04 3:50 4622
2021-01-04 4:00 4633
2021-01-04 4:10 4643
2021-01-04 4:20 4654
2021-01-04 4:30 4660
2021-01-04 4:40 4673
2021-01-04 4:50 4687
2021-01-04 5:00 4702
2021-01-04 5:10 4715
2021-01-04 5:20 4727
2021-01-04 5:30 4740
2021-01-04 5:40 4748
2021-01-04 5:50 4757
2021-01-04 6:00 4765
2021-01-04 6:10 4772
2021-01-04 6:20 4779
2021-01-04 6:30 4786
2021-01-04 6:40 4801
2021-01-04 6:50 4806
2021-01-04 7:00 4818
2021-01-04 7:10 4828
2021-01-04 7:20 4841
2021-01-04 7:30 4851
2021-01-04 7:40 4859
2021-01-04 7:50 4868
2021-01-04 8:00 4873
2021-01-04 8:10 4886
2021-01-04 8:20 4899
2021-01-04 8:30 4908
2021-01-04 8:40 4923
2021-01-04 8:50 4935
2021-01-04 9:00 4949
2021-01-04 9:10 4954
2021-01-04 9:20 4967
2021-01-04 9:30 4979
2021-01-04 9:40 4992
2021-01-04 9:50 5004
2021-01-04 10:00 5013
2021-01-04 10:10 5019
2021-01-04 10:20 5033
2021-01-04 10:30 5045
2021-01-04 10:40 5055
2021-01-04 10:50 5067
2021-01-04 11:00 5072
2021-01-04 11:10 5077
2021-01-04 11:20 5085
2021-01-04 11:30 5092
2021-01-04 11:40 5097
2021-01-04 11:50 5109
2021-01-04 12:00 5114
2021-01-04 12:10 5127
2021-01-04 12:20 5137
2021-01-04 12:30 5150
2021-01-04 12:40 5155
2021-01-04 12:50 5170
2021-01-04 13:00 5177
2021-01-04 13:10 5188
2021-01-04 13:20 5197
2021-01-04 13:30 5207
2021-01-04 13:40 5212
2021-01-04 13:50 5223
2021-01-04 14:00 5238
2021-01-04 14:10 5251
2021-01-04 14:20 5257
2021-01-04 14:30 5264
2021-01-04 14:40 5269
2021-01-04 14:50 5281
2021-01-04 15:00 5287
2021-01-04 15:10 5295
2021-01-04 15:20 5304
2021-01-04 15:30 5309
2021-01-04 15:40 5317
2021-01-04 15:50 5328
2021-01-04 16:00 5336
2021-01-04 16:10 5349
2021-01-04 16:20 5364
2021-01-04 16:30 5376
2021-01-04 16:40 5391
2021-01-04 16:50 5403
2021-01-04 17:00 5414
2021-01-04 17:10 5427
2021-01-04 17:20 5437
2021-01-04 17:30 5445
2021-01-04 17:40 5453
2021-01-04 17:50 5465
2021-01-04 18:00 5477
2021-01-04 18:10 5491
2021-01-04 18:20 5505
2021-01-04 18:30 5512
2021-01-04 18:40 5518
2021-01-04 18:50 5531
2021-01-04 19:00 5544
2021-01-04 19:10 5556
2021-01-04 19:20 5566
2021-01-04 19:30 5578
2021-01-04 19:40 5590
2021-01-04 19:50 5595
2021-01-04 20:00 5608
2021-01-04 20:10 5615
2021-01-04 20:20 5628
2021-01-04 20:30 5636
2021-01-04 20:40 5646
2021-01-04 20:50 5655
2021-01-04 21:00 5667
2021-01-04 21:10 5675
2021-01-04 21:20 5687
2021-01-04 21:30 5696
2021-01-04 21:40 5707
2021-01-04 21:50 5712
2021-01-04 22:00 5721
2021-01-04 22:10 5736
2021-01-04 22:20 5747
2021-01-04 22:30 5755
2021-01-04 22:40 5762
2021-01-04 22:50 5774
2021-01-04 23:00 5780
2021-01-04 23:10 5795
2021-01-04 23:20 5810
2021-01-04 23:30 5820
2021-01-04 23:40 5827
2021-01-04 23:50 5833
2021-01-05 0:00 5842
2021-01-05 0:10 5847
2021-01-05 0:20 5861

 

accu 는 누적 데이터 값입니다.

run_dt 는 datatime이며, accu 는 int로 설정되어 있습니다.

 

제가 얻고 싶은 결과는 다음과 같습니다.

2021-01-01 00:00:00 는 2021-01-01 01:00:00 의 accu 와 2021-01-01 00:00:00 의 accu 의 차이값으로 구하고 싶습니다.

그리고 해당 결과는 1시간 마다 얻으려고 합니다.

2021-01-01 00:00:00 : 2021-01-01 01:00:00 의 accu - 2021-01-01 00:00:00 의 accu

2021-01-01 01:00:00 : 2021-01-01 02:00:00 의 accu - 2021-01-01 01:00:00 의 accu

처럼 구하고 싶습니다.

 

그리고, 마지막의 경우 

2021-01-05 00:00:00 은 2021-01-05 00:00:00보다 큰 시간의 accu 값에서 2021-01-05 00:00:00 의 accu 값의 차이로 얻고 싶습니다.

2021-01-01 00:00:00 : 2021-01-01 01:00:00 의 accu - 2021-01-01 00:00:00 의 accu

2021-01-01 01:00:00 : 2021-01-01 02:00:00 의 accu - 2021-01-01 01:00:00 의 accu

......

2021-01-05 00:00:00 : 2021-01-05 00:20:00 의 accu - 2021-01-05 00:00:00 의 accu

 

이럴 때는 쿼리를 어떻게 작성해야 하는 지 문의드립니다.

 

감사합니다.

 
by 마농 [2024.10.16 10:23:45]

구하고자 하는 방향을 바꿔야 할 듯 합니다.
B - A 를 해서 A 에 표현하는게 아니라
B - A 를 해서 B 에 표현하는게 맞다고 생각합니다.


by 마농 [2024.10.16 11:03:40]
WITH t AS
(
SELECT '2021-01-01 00:00' run_dt, 12 accu
UNION ALL SELECT '2021-01-01 01:00',   80
UNION ALL SELECT '2021-01-01 02:00',  134
UNION ALL SELECT '2021-01-01 03:00',  203
UNION ALL SELECT '2021-01-01 04:00',  260
UNION ALL SELECT '2021-01-01 05:00',  325
UNION ALL SELECT '2021-01-01 06:00',  393
UNION ALL SELECT '2021-01-01 07:00',  461
UNION ALL SELECT '2021-01-01 08:00',  535
UNION ALL SELECT '2021-01-01 09:00',  579
UNION ALL SELECT '2021-01-01 10:00',  633
UNION ALL SELECT '2021-01-01 11:00',  699
UNION ALL SELECT '2021-01-01 12:00',  763
UNION ALL SELECT '2021-01-01 13:00',  815
UNION ALL SELECT '2021-01-01 14:00',  879
UNION ALL SELECT '2021-01-01 15:00',  932
UNION ALL SELECT '2021-01-01 16:00',  985
UNION ALL SELECT '2021-01-01 17:00', 1054
UNION ALL SELECT '2021-01-01 18:00', 1119
UNION ALL SELECT '2021-01-01 19:00', 1182
UNION ALL SELECT '2021-01-01 20:00', 1236
UNION ALL SELECT '2021-01-01 21:00', 1308
UNION ALL SELECT '2021-01-01 22:00', 1363
UNION ALL SELECT '2021-01-01 23:00', 1424
UNION ALL SELECT '2021-01-02 00:00', 1475
UNION ALL SELECT '2021-01-02 01:00', 1522
UNION ALL SELECT '2021-01-02 02:00', 1581
UNION ALL SELECT '2021-01-02 03:00', 1656
UNION ALL SELECT '2021-01-02 04:00', 1719
UNION ALL SELECT '2021-01-02 05:00', 1791
UNION ALL SELECT '2021-01-02 06:00', 1848
UNION ALL SELECT '2021-01-02 07:00', 1899
UNION ALL SELECT '2021-01-02 08:00', 1961
UNION ALL SELECT '2021-01-02 09:00', 2012
UNION ALL SELECT '2021-01-02 10:00', 2067
UNION ALL SELECT '2021-01-02 11:00', 2130
UNION ALL SELECT '2021-01-02 12:00', 2182
UNION ALL SELECT '2021-01-02 13:00', 2242
UNION ALL SELECT '2021-01-02 14:00', 2293
UNION ALL SELECT '2021-01-02 15:00', 2341
UNION ALL SELECT '2021-01-02 16:00', 2401
UNION ALL SELECT '2021-01-02 17:00', 2464
UNION ALL SELECT '2021-01-02 18:00', 2533
UNION ALL SELECT '2021-01-02 19:00', 2597
UNION ALL SELECT '2021-01-02 20:00', 2660
UNION ALL SELECT '2021-01-02 21:00', 2726
UNION ALL SELECT '2021-01-02 22:00', 2795
UNION ALL SELECT '2021-01-02 23:00', 2853
UNION ALL SELECT '2021-01-03 00:00', 2914
UNION ALL SELECT '2021-01-03 01:00', 2972
UNION ALL SELECT '2021-01-03 02:00', 3025
UNION ALL SELECT '2021-01-03 03:00', 3077
UNION ALL SELECT '2021-01-03 04:00', 3136
UNION ALL SELECT '2021-01-03 05:00', 3201
UNION ALL SELECT '2021-01-03 06:00', 3258
UNION ALL SELECT '2021-01-03 07:00', 3332
UNION ALL SELECT '2021-01-03 08:00', 3390
UNION ALL SELECT '2021-01-03 09:00', 3455
UNION ALL SELECT '2021-01-03 10:00', 3526
UNION ALL SELECT '2021-01-03 11:00', 3579
UNION ALL SELECT '2021-01-03 12:00', 3636
UNION ALL SELECT '2021-01-03 13:00', 3707
UNION ALL SELECT '2021-01-03 14:00', 3770
UNION ALL SELECT '2021-01-03 15:00', 3840
UNION ALL SELECT '2021-01-03 16:00', 3901
UNION ALL SELECT '2021-01-03 17:00', 3956
UNION ALL SELECT '2021-01-03 18:00', 4016
UNION ALL SELECT '2021-01-03 19:00', 4085
UNION ALL SELECT '2021-01-03 20:00', 4148
UNION ALL SELECT '2021-01-03 21:00', 4213
UNION ALL SELECT '2021-01-03 22:00', 4274
UNION ALL SELECT '2021-01-03 23:00', 4332
UNION ALL SELECT '2021-01-04 00:00', 4401
UNION ALL SELECT '2021-01-04 01:00', 4470
UNION ALL SELECT '2021-01-04 02:00', 4517
UNION ALL SELECT '2021-01-04 03:00', 4571
UNION ALL SELECT '2021-01-04 04:00', 4633
UNION ALL SELECT '2021-01-04 05:00', 4702
UNION ALL SELECT '2021-01-04 06:00', 4765
UNION ALL SELECT '2021-01-04 07:00', 4818
UNION ALL SELECT '2021-01-04 08:00', 4873
UNION ALL SELECT '2021-01-04 09:00', 4949
UNION ALL SELECT '2021-01-04 10:00', 5013
UNION ALL SELECT '2021-01-04 11:00', 5072
UNION ALL SELECT '2021-01-04 12:00', 5114
UNION ALL SELECT '2021-01-04 13:00', 5177
UNION ALL SELECT '2021-01-04 14:00', 5238
UNION ALL SELECT '2021-01-04 15:00', 5287
UNION ALL SELECT '2021-01-04 16:00', 5336
UNION ALL SELECT '2021-01-04 17:00', 5414
UNION ALL SELECT '2021-01-04 18:00', 5477
UNION ALL SELECT '2021-01-04 19:00', 5544
UNION ALL SELECT '2021-01-04 20:00', 5608
UNION ALL SELECT '2021-01-04 21:00', 5667
UNION ALL SELECT '2021-01-04 22:00', 5721
UNION ALL SELECT '2021-01-04 23:00', 5780
UNION ALL SELECT '2021-01-05 00:00', 5842
UNION ALL SELECT '2021-01-05 00:10', 5847
UNION ALL SELECT '2021-01-05 00:20', 5861
)
SELECT run_dt
     , accu
     , LEAD(accu) OVER(ORDER BY run_dt) - accu      v1  -- 요청한 것
     , accu - LAG(accu, 1, 0) OVER(ORDER BY run_dt) v2  -- 추천하는 것
  FROM (SELECT run_dt, accu
             , MAX(run_dt) OVER() max_run_dt
          FROM t
        ) a
 WHERE run_dt = max_run_dt
    OR RIGHT(run_dt, 2) = '00'
;

 


by tosswin [2024.10.16 11:17:12]

마농님 답변 감사합니다.

제가 원하는 질문을 제대로 드리지 못한 것 같습니다.

질문을 수정해서 다시 올리겠습니다.

감사합니다.

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