github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/window/window.result (about) 1 drop table if exists t1; 2 create table t1 (a int, b datetime); 3 insert into t1 values(1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'); 4 select sum(a) over(partition by a order by b range between interval 1 day preceding and interval 2 day following) from t1; 5 sum(a) over (partition by a order by b range between interval(1, day) preceding and interval(2, day) following) 6 1 7 2 8 3 9 drop table t1; 10 drop table if exists t1; 11 create table t1 (a int, b date); 12 insert into t1 values(1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'), (1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'), (1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'); 13 select max(a) over(order by b range between interval 1 day preceding and interval 2 day following) from t1; 14 max(a) over (order by b range between interval(1, day) preceding and interval(2, day) following) 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 drop table t1; 25 drop table if exists t1; 26 create table t1 (a int, b time); 27 insert into t1 values(1, 112233), (2, 122233), (3, 132233), (1, 112233), (2, 122233), (3, 132233), (1, 112233), (2, 122233), (3, 132233); 28 select min(a) over(order by b range between interval 1 hour preceding and current row) from t1; 29 min(a) over (order by b range between interval(1, hour) preceding and current row) 30 1 31 1 32 1 33 1 34 1 35 1 36 2 37 2 38 2 39 drop table t1; 40 drop table if exists t1; 41 create table t1 (a int, b timestamp); 42 insert into t1 values(1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'), (1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'), (1, '2020-11-11'), (2, '2020-11-12'), (3, '2020-11-13'); 43 select count(*) over(order by b range current row) from t1; 44 count(*) over (order by b range current row) 45 3 46 3 47 3 48 3 49 3 50 3 51 3 52 3 53 3 54 drop table t1; 55 drop table if exists t1; 56 create table t1 (a int, b int, c int); 57 insert into t1 values(1, 2, 1), (3, 4, 2), (5, 6, 3), (7, 8, 4), (3, 4, 5), (3, 4, 6), (3, 4, 7); 58 select a, rank() over (partition by a) from t1 group by a, c; 59 a rank() over (partition by a) 60 1 1 61 3 1 62 3 1 63 3 1 64 3 1 65 5 1 66 7 1 67 select a, c, rank() over (partition by a order by c) from t1 group by a, c; 68 a c rank() over (partition by a order by c) 69 1 1 1 70 3 2 1 71 3 5 2 72 3 6 3 73 3 7 4 74 5 3 1 75 7 4 1 76 select a, c, rank() over (partition by a order by c) from t1 group by a, c; 77 a c rank() over (partition by a order by c) 78 1 1 1 79 3 2 1 80 3 5 2 81 3 6 3 82 3 7 4 83 5 3 1 84 7 4 1 85 select a, c, b, rank() over (partition by a, c, b) from t1; 86 a c b rank() over (partition by a, c, b) 87 1 1 2 1 88 3 2 4 1 89 3 5 4 1 90 3 6 4 1 91 3 7 4 1 92 5 3 6 1 93 7 4 8 1 94 select a, b, rank() over (partition by a, b) from t1; 95 a b rank() over (partition by a, b) 96 1 2 1 97 3 4 1 98 3 4 1 99 3 4 1 100 3 4 1 101 5 6 1 102 7 8 1 103 select a, c, sum(a) over (), sum(c) over () from t1; 104 a c sum(a) over () sum(c) over () 105 1 1 25 28 106 3 2 25 28 107 5 3 25 28 108 7 4 25 28 109 3 5 25 28 110 3 6 25 28 111 3 7 25 28 112 select a, c, sum(a) over (order by c), sum(c) over (order by a) from t1; 113 a c sum(a) over (order by c) sum(c) over (order by a) 114 1 1 1 1 115 3 2 4 21 116 3 5 19 21 117 3 6 22 21 118 3 7 25 21 119 5 3 9 24 120 7 4 16 28 121 select a, sum(b), sum(sum(b)) over (partition by a), sum(sum(b)) over (partition by c) from t1 group by a, c; 122 a sum(b) sum(sum(b)) over (partition by a) sum(sum(b)) over (partition by c) 123 1 2 2 2 124 3 4 16 4 125 5 6 6 6 126 7 8 8 8 127 3 4 16 4 128 3 4 16 4 129 3 4 16 4 130 select a, sum(b), rank() over (partition by a +1), rank() over (partition by c), c from t1 group by a, c; 131 a sum(b) rank() over (partition by a + 1) rank() over (partition by c) c 132 1 2 1 1 1 133 3 4 1 1 2 134 5 6 1 1 3 135 7 8 1 1 4 136 3 4 1 1 5 137 3 4 1 1 6 138 3 4 1 1 7 139 select a, sum(b), sum(sum(b)) over (partition by a) as o from t1 group by a, c; 140 a sum(b) o 141 1 2 2 142 3 4 16 143 3 4 16 144 3 4 16 145 3 4 16 146 5 6 6 147 7 8 8 148 select a, sum(b), cast(sum(sum(b)) over (partition by a+1 order by a+1 rows between 2 preceding and CURRENT row) as float) as o from t1 group by a, c; 149 a sum(b) o 150 1 2 2.0 151 3 4 4.0 152 3 4 8.0 153 3 4 12.0 154 3 4 12.0 155 5 6 6.0 156 7 8 8.0 157 select a, sum(b), sum(sum(b)) over (partition by a rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) from 158 t1 group by a, c; 159 a sum(b) sum(sum(b)) over (partition by a rows between unbounded preceding and current row) 160 1 2 2 161 3 4 4 162 3 4 8 163 3 4 12 164 3 4 16 165 5 6 6 166 7 8 8 167 select a, sum(a) over (partition by c order by b range BETWEEN 3 preceding and 4 following), c, b from t1; 168 a sum(a) over (partition by c order by b range between 3 preceding and 4 following) c b 169 1 1 1 2 170 3 3 2 4 171 5 5 3 6 172 7 7 4 8 173 3 3 5 4 174 3 3 6 4 175 3 3 7 4 176 select a, sum(a) over (order by a) from t1; 177 a sum(a) over (order by a) 178 1 1 179 3 13 180 3 13 181 3 13 182 3 13 183 5 18 184 7 25 185 select a, rank() over (partition by a) from t1; 186 a rank() over (partition by a) 187 1 1 188 3 1 189 3 1 190 3 1 191 3 1 192 5 1 193 7 1 194 select a, rank() over () from t1; 195 a rank() over () 196 1 1 197 3 1 198 5 1 199 7 1 200 3 1 201 3 1 202 3 1 203 select a, sum(a) over (partition by a rows current row) from t1; 204 a sum(a) over (partition by a rows current row) 205 1 1 206 3 3 207 3 3 208 3 3 209 3 3 210 5 5 211 7 7 212 select c, sum(c) over (order by c range between 1 preceding and 1 following) from t1; 213 c sum(c) over (order by c range between 1 preceding and 1 following) 214 1 3 215 2 6 216 3 9 217 4 12 218 5 15 219 6 18 220 7 13 221 select c, sum(100) over (order by c range between 1 preceding and 1 following), a, b from t1; 222 c sum(100) over (order by c range between 1 preceding and 1 following) a b 223 1 200 1 2 224 2 300 3 4 225 3 300 5 6 226 4 300 7 8 227 5 300 3 4 228 6 300 3 4 229 7 200 3 4 230 select c, sum(null) over (order by c range between 1 preceding and 1 following), a, b from t1; 231 c sum(null) over (order by c range between 1 preceding and 1 following) a b 232 1 null 1 2 233 2 null 3 4 234 3 null 5 6 235 4 null 7 8 236 5 null 3 4 237 6 null 3 4 238 7 null 3 4 239 select a, b, c, rank() over (partition by a, b order by c) from t1; 240 a b c rank() over (partition by a, b order by c) 241 1 2 1 1 242 3 4 2 1 243 3 4 5 2 244 3 4 6 3 245 3 4 7 4 246 5 6 3 1 247 7 8 4 1 248 select a, c, rank() over(partition by a order by c rows current row) from t1; 249 a c rank() over (partition by a order by c rows current row) 250 1 1 1 251 3 2 1 252 3 5 2 253 3 6 3 254 3 7 4 255 5 3 1 256 7 4 1 257 select a, row_number() over (partition by a) from t1 group by a, c; 258 a row_number() over (partition by a) 259 1 1 260 3 1 261 3 2 262 3 3 263 3 4 264 5 1 265 7 1 266 select a, c, row_number() over (partition by a order by c) from t1 group by a, c; 267 a c row_number() over (partition by a order by c) 268 1 1 1 269 3 2 1 270 3 5 2 271 3 6 3 272 3 7 4 273 5 3 1 274 7 4 1 275 select a, c, row_number() over (partition by a order by c) from t1 group by a, c; 276 a c row_number() over (partition by a order by c) 277 1 1 1 278 3 2 1 279 3 5 2 280 3 6 3 281 3 7 4 282 5 3 1 283 7 4 1 284 select a, c, b, row_number() over (partition by a, c, b) from t1; 285 a c b row_number() over (partition by a, c, b) 286 1 1 2 1 287 3 2 4 1 288 3 5 4 1 289 3 6 4 1 290 3 7 4 1 291 5 3 6 1 292 7 4 8 1 293 select a, b, row_number() over (partition by a, b) from t1; 294 a b row_number() over (partition by a, b) 295 1 2 1 296 3 4 1 297 3 4 2 298 3 4 3 299 3 4 4 300 5 6 1 301 7 8 1 302 select a, sum(b), row_number() over (partition by a +1), row_number() over (partition by c), c from t1 group by a, c; 303 a sum(b) row_number() over (partition by a + 1) row_number() over (partition by c) c 304 1 2 1 1 1 305 3 4 1 1 2 306 5 6 1 1 3 307 7 8 1 1 4 308 3 4 2 1 5 309 3 4 3 1 6 310 3 4 4 1 7 311 select a, row_number() over (partition by a) from t1; 312 a row_number() over (partition by a) 313 1 1 314 3 1 315 3 2 316 3 3 317 3 4 318 5 1 319 7 1 320 select a, row_number() over () from t1; 321 a row_number() over () 322 1 1 323 3 2 324 5 3 325 7 4 326 3 5 327 3 6 328 3 7 329 select a, b, c, row_number() over (partition by a, b order by c) from t1; 330 a b c row_number() over (partition by a, b order by c) 331 1 2 1 1 332 3 4 2 1 333 3 4 5 2 334 3 4 6 3 335 3 4 7 4 336 5 6 3 1 337 7 8 4 1 338 select a, c, row_number() over(partition by a order by c rows current row) from t1; 339 a c row_number() over (partition by a order by c rows current row) 340 1 1 1 341 3 2 1 342 3 5 2 343 3 6 3 344 3 7 4 345 5 3 1 346 7 4 1 347 select a, dense_rank() over (partition by a) from t1 group by a, c; 348 a dense_rank() over (partition by a) 349 1 1 350 3 1 351 3 1 352 3 1 353 3 1 354 5 1 355 7 1 356 select a, c, dense_rank() over (partition by a order by c) from t1 group by a, c; 357 a c dense_rank() over (partition by a order by c) 358 1 1 1 359 3 2 1 360 3 5 2 361 3 6 3 362 3 7 4 363 5 3 1 364 7 4 1 365 select a, c, dense_rank() over (partition by a order by c) from t1 group by a, c; 366 a c dense_rank() over (partition by a order by c) 367 1 1 1 368 3 2 1 369 3 5 2 370 3 6 3 371 3 7 4 372 5 3 1 373 7 4 1 374 select a, c, b, dense_rank() over (partition by a, c, b) from t1; 375 a c b dense_rank() over (partition by a, c, b) 376 1 1 2 1 377 3 2 4 1 378 3 5 4 1 379 3 6 4 1 380 3 7 4 1 381 5 3 6 1 382 7 4 8 1 383 select a, b, dense_rank() over (partition by a, b) from t1; 384 a b dense_rank() over (partition by a, b) 385 1 2 1 386 3 4 1 387 3 4 1 388 3 4 1 389 3 4 1 390 5 6 1 391 7 8 1 392 select a, sum(b), dense_rank() over (partition by a +1), dense_rank() over (partition by c), c from t1 group by a, c; 393 a sum(b) dense_rank() over (partition by a + 1) dense_rank() over (partition by c) c 394 1 2 1 1 1 395 3 4 1 1 2 396 5 6 1 1 3 397 7 8 1 1 4 398 3 4 1 1 5 399 3 4 1 1 6 400 3 4 1 1 7 401 select a, dense_rank() over (partition by a) from t1; 402 a dense_rank() over (partition by a) 403 1 1 404 3 1 405 3 1 406 3 1 407 3 1 408 5 1 409 7 1 410 select a, dense_rank() over () from t1; 411 a dense_rank() over () 412 1 1 413 3 1 414 5 1 415 7 1 416 3 1 417 3 1 418 3 1 419 select a, b, c, dense_rank() over (partition by a, b order by c) from t1; 420 a b c dense_rank() over (partition by a, b order by c) 421 1 2 1 1 422 3 4 2 1 423 3 4 5 2 424 3 4 6 3 425 3 4 7 4 426 5 6 3 1 427 7 8 4 1 428 select a, c, dense_rank() over(partition by a order by c rows current row) from t1; 429 a c dense_rank() over (partition by a order by c rows current row) 430 1 1 1 431 3 2 1 432 3 5 2 433 3 6 3 434 3 7 4 435 5 3 1 436 7 4 1 437 select a, c, rank() over(order by a), row_number() over(order by a), dense_rank() over(order by a) from t1; 438 a c rank() over (order by a) row_number() over (order by a) dense_rank() over (order by a) 439 1 1 1 1 1 440 3 2 2 2 2 441 3 5 2 3 2 442 3 6 2 4 2 443 3 7 2 5 2 444 5 3 6 6 3 445 7 4 7 7 4 446 drop table t1; 447 drop table if exists t1; 448 create table t1 (a int, b decimal(7, 2)); 449 insert into t1 values(1, 12.12), (2, 123.13), (3, 456.66), (4, 1111.34); 450 select a, sum(b) over (partition by a order by a) from t1; 451 a sum(b) over (partition by a order by a) 452 1 12.12 453 2 123.13 454 3 456.66 455 4 1111.34 456 drop table t1; 457 drop table if exists wf01; 458 create table wf01(i int,j int); 459 insert into wf01 values(1,1); 460 insert into wf01 values(1,4); 461 insert into wf01 values(1,2); 462 insert into wf01 values(1,4); 463 select * from wf01; 464 i j 465 1 1 466 1 4 467 1 2 468 1 4 469 select i, j, sum(i+j) over (order by j desc rows unbounded preceding) foo from wf01; 470 i j foo 471 1 4 5 472 1 4 10 473 1 2 13 474 1 1 15 475 select i, j, sum(i+j) over (order by j desc rows between 2 preceding and 2 following) as foo from wf01; 476 i j foo 477 1 4 13 478 1 4 15 479 1 2 15 480 1 1 10 481 select i, j, sum(i+j) over (order by j desc rows unbounded preceding) foo from wf01 order by foo; 482 i j foo 483 1 4 5 484 1 4 10 485 1 2 13 486 1 1 15 487 select i, j, sum(i+j) over (order by j desc rows unbounded preceding) foo from wf01 order by foo desc; 488 i j foo 489 1 1 15 490 1 2 13 491 1 4 10 492 1 4 5 493 drop table wf01; 494 drop table if exists wf08; 495 create table wf08(d decimal(10,2), date date); 496 insert into wf08 values (10.4, '2002-06-09'); 497 insert into wf08 values (20.5, '2002-06-09'); 498 insert into wf08 values (10.4, '2002-06-10'); 499 insert into wf08 values (3, '2002-06-09'); 500 insert into wf08 values (40.2, '2015-08-01'); 501 insert into wf08 values (40.2, '2002-06-09'); 502 insert into wf08 values (5, '2015-08-01'); 503 select * from (select rank() over (order by d) as `rank`, d, date from wf08) alias order by `rank`, d, date; 504 rank d date 505 1 3.00 2002-06-09 506 2 5.00 2015-08-01 507 3 10.40 2002-06-09 508 3 10.40 2002-06-10 509 5 20.50 2002-06-09 510 6 40.20 2002-06-09 511 6 40.20 2015-08-01 512 select * from (select dense_rank() over (order by d) as `d_rank`, d, date from wf08) alias order by `d_rank`, d, date; 513 d_rank d date 514 1 3.00 2002-06-09 515 2 5.00 2015-08-01 516 3 10.40 2002-06-09 517 3 10.40 2002-06-10 518 4 20.50 2002-06-09 519 5 40.20 2002-06-09 520 5 40.20 2015-08-01 521 drop table wf08; 522 drop table if exists wf07; 523 create table wf07 (user_id integer not null, date date); 524 insert into wf07 values (1, '2002-06-09'); 525 insert into wf07 values (2, '2002-06-09'); 526 insert into wf07 values (1, '2002-06-09'); 527 insert into wf07 values (3, '2002-06-09'); 528 insert into wf07 values (4, '2002-06-09'); 529 insert into wf07 values (4, '2002-06-09'); 530 insert into wf07 values (5, '2002-06-09'); 531 select rank() over () r from wf07; 532 r 533 1 534 1 535 1 536 1 537 1 538 1 539 1 540 select dense_rank() over () r from wf07; 541 r 542 1 543 1 544 1 545 1 546 1 547 1 548 1 549 drop table wf07; 550 drop table if exists wf12; 551 create table wf12(d double); 552 insert into wf12 values (1.7976931348623157e+307); 553 insert into wf12 values (1); 554 select d, sum(d) over (rows between current row and 1 following) from wf12; 555 d sum(d) over (rows between current row and 1 following) 556 1.7976931348623158E307 1.7976931348623158E307 557 1.0 1.0 558 drop table wf12; 559 drop table if exists wf06; 560 create table wf06 (id integer, sex char(1)); 561 insert into wf06 values (1, 'm'); 562 insert into wf06 values (2, 'f'); 563 insert into wf06 values (3, 'f'); 564 insert into wf06 values (4, 'f'); 565 insert into wf06 values (5, 'm'); 566 drop table if exists wf07; 567 create table wf07 (user_id integer not null, date date); 568 insert into wf07 values (1, '2002-06-09'); 569 insert into wf07 values (2, '2002-06-09'); 570 insert into wf07 values (1, '2002-06-09'); 571 insert into wf07 values (3, '2002-06-09'); 572 insert into wf07 values (4, '2002-06-09'); 573 insert into wf07 values (4, '2002-06-09'); 574 insert into wf07 values (5, '2002-06-09'); 575 select id value, sum(id) over (rows unbounded preceding) from wf06 inner join wf07 on wf07.user_id = wf06.id; 576 value sum(id) over (rows unbounded preceding) 577 1 1 578 1 2 579 2 4 580 3 7 581 4 11 582 4 15 583 5 20 584 drop table wf06; 585 drop table wf07; 586 drop table if exists row01; 587 create table row01(i int,j int); 588 insert into row01 values(1,1); 589 insert into row01 values(1,4); 590 insert into row01 values(1,2); 591 insert into row01 values(1,4); 592 select i, j, sum(i+j) over (order by j rows between 2 preceding and 1 preceding) foo from row01 order by foo desc; 593 i j foo 594 1 4 8 595 1 4 5 596 1 2 2 597 1 1 null 598 select i, j, sum(i+j) over (order by j rows between 2 following and 1 following) foo from row01 order by foo desc; 599 i j foo 600 1 1 null 601 1 2 null 602 1 4 null 603 1 4 null 604 drop table row01; 605 drop table if exists test01; 606 create table test01(i int, j int); 607 insert into test01 values (1,null); 608 insert into test01 values (1,null); 609 insert into test01 values (1,1); 610 insert into test01 values (1,null); 611 insert into test01 values (1,2); 612 insert into test01 values (2,1); 613 insert into test01 values (2,2); 614 insert into test01 values (2,null); 615 insert into test01 values (2,null); 616 select i, j, min(j) over (partition by i order by j rows unbounded preceding) from test01; 617 i j min(j) over (partition by i order by j rows unbounded preceding) 618 1 null null 619 1 null null 620 1 null null 621 1 1 1 622 1 2 1 623 2 null null 624 2 null null 625 2 1 1 626 2 2 1 627 drop table test01; 628 drop table if exists double01; 629 create table double01(d double); 630 insert into double01 values (2); 631 insert into double01 values (2); 632 insert into double01 values (3); 633 insert into double01 values (1); 634 insert into double01 values (1); 635 insert into double01 values (1.2); 636 insert into double01 values (null); 637 insert into double01 values (null); 638 select d, sum(d) over (partition by d order by d), avg(d) over (order by d rows between 1 preceding and 1 following) from double01; 639 d sum(d) over (partition by d order by d) avg(d) over (order by d rows between 1 preceding and 1 following) 640 null null null 641 null null 1.0 642 1.0 2.0 1.0 643 1.0 2.0 1.0666666666666667 644 1.2 1.2 1.4000000000000001 645 2.0 4.0 1.7333333333333334 646 2.0 4.0 2.3333333333333335 647 3.0 3.0 2.5 648 select d, sum(d) over (partition by d order by d), avg(d) over (order by d rows between 2 preceding and 1 following) from double01; 649 d sum(d) over (partition by d order by d) avg(d) over (order by d rows between 2 preceding and 1 following) 650 null null null 651 null null 1.0 652 1.0 2.0 1.0 653 1.0 2.0 1.0666666666666667 654 1.2 1.2 1.3 655 2.0 4.0 1.55 656 2.0 4.0 2.05 657 3.0 3.0 2.3333333333333335 658 drop table double01; 659 drop table if exists wf01; 660 create table wf01(d float); 661 insert into wf01 values (10); 662 insert into wf01 values (1); 663 insert into wf01 values (2); 664 insert into wf01 values (3); 665 insert into wf01 values (4); 666 insert into wf01 values (5); 667 insert into wf01 values (6); 668 insert into wf01 values (7); 669 insert into wf01 values (8); 670 insert into wf01 values (9); 671 select d, sum(d) over (order by d range between current row and 2 following), avg(d) over (order by d range between current row and 2 following) from wf01; 672 d sum(d) over (order by d range between current row and 2 following) avg(d) over (order by d range between current row and 2 following) 673 1.0 6.0 2.0 674 2.0 9.0 3.0 675 3.0 12.0 4.0 676 4.0 15.0 5.0 677 5.0 18.0 6.0 678 6.0 21.0 7.0 679 7.0 24.0 8.0 680 8.0 27.0 9.0 681 9.0 19.0 9.5 682 10.0 10.0 10.0 683 select d, sum(d) over (order by d range between 2 preceding and 2 following), avg(d) over (order by d range between current row and 2 following) from wf01; 684 d sum(d) over (order by d range between 2 preceding and 2 following) avg(d) over (order by d range between current row and 2 following) 685 1.0 6.0 2.0 686 2.0 10.0 3.0 687 3.0 15.0 4.0 688 4.0 20.0 5.0 689 5.0 25.0 6.0 690 6.0 30.0 7.0 691 7.0 35.0 8.0 692 8.0 40.0 9.0 693 9.0 34.0 9.5 694 10.0 27.0 10.0 695 drop table wf01; 696 drop table if exists dense_rank01; 697 create table dense_rank01 (id integer, sex char(1)); 698 insert into dense_rank01 values (1, 'm'); 699 insert into dense_rank01 values (2, 'f'); 700 insert into dense_rank01 values (3, 'f'); 701 insert into dense_rank01 values (4, 'f'); 702 insert into dense_rank01 values (5, 'm'); 703 select sex, id, rank() over (partition by sex order by id desc) from dense_rank01; 704 sex id rank() over (partition by sex order by id desc) 705 f 4 1 706 f 3 2 707 f 2 3 708 m 5 1 709 m 1 2 710 select sex, id, dense_rank() over (partition by sex order by id desc) from dense_rank01; 711 sex id dense_rank() over (partition by sex order by id desc) 712 f 4 1 713 f 3 2 714 f 2 3 715 m 5 1 716 m 1 2 717 drop table dense_rank01; 718 drop table if exists sales; 719 create table sales (customer_id varchar(1), order_date date, product_id integer); 720 insert into sales(customer_id, order_date, product_id) values ('a', '2021-01-01', '1'), ('a', '2021-01-01', '2'), ('a', '2021-01-07', '2'), ('a', '2021-01-10', '3'), ('a', '2021-01-11', '3'), ('a', '2021-01-11', '3'),('b', '2021-01-01', '2'),('b', '2021-01-02', '2'),('b', '2021-01-04', '1'),('b', '2021-01-11', '1'),('b', '2021-01-16', '3'),('b', '2021-02-01', '3'),('c', '2021-01-01', '3'),('c', '2021-01-01', '3'),('c', '2021-01-07', '3'); 721 drop table if exists menu; 722 create table menu (product_id integer,product_name varchar(5),price integer); 723 insert into menu(product_id, product_name, price) values ('1', 'sushi', '10'),('2', 'curry', '15'),('3', 'ramen', '12'); 724 with ordered_sales as (select sales.customer_id, sales.order_date, menu.product_name,dense_rank() over (partition by sales.customer_id order by sales.order_date) as `rank` from sales inner join menu on sales.product_id = menu.product_id) select customer_id, product_name from ordered_sales where `rank` = 1 group by customer_id, product_name; 725 customer_id product_name 726 a sushi 727 a curry 728 b curry 729 c ramen 730 drop table sales; 731 drop table if exists test01; 732 create table test01(i int, j int); 733 insert into test01 values (1,null); 734 insert into test01 values (1,null); 735 insert into test01 values (1,1); 736 insert into test01 values (1,null); 737 insert into test01 values (1,2); 738 insert into test01 values (2,1); 739 insert into test01 values (2,2); 740 insert into test01 values (2,null); 741 insert into test01 values (2,null); 742 select rank() over (order by t0.a) as b from (select i as a from test01) as t0; 743 b 744 1 745 1 746 1 747 1 748 1 749 6 750 6 751 6 752 6 753 select rank() over(order by j) as col, j from test01; 754 col j 755 1 null 756 1 null 757 1 null 758 1 null 759 1 null 760 6 1 761 6 1 762 8 2 763 8 2 764 drop table test01; 765 drop table if exists wf14; 766 create table wf14 (id integer, sex char(1)); 767 insert into wf14 values (1, 'm'); 768 insert into wf14 values (2, 'f'); 769 insert into wf14 values (3, 'f'); 770 insert into wf14 values (4, 'f'); 771 insert into wf14 values (5, 'm'); 772 insert into wf14 values (10, null); 773 insert into wf14 values (11, null); 774 insert into wf14 values (10, null); 775 select id, sex, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) as a from wf14; 776 id sex a 777 10 null 20 778 10 null 31 779 11 null 31 780 2 f 5 781 3 f 9 782 4 f 9 783 1 m 6 784 5 m 6 785 select id, sex, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) as a from wf14; 786 id sex a 787 10 null 20 788 10 null 31 789 11 null 31 790 2 f 5 791 3 f 9 792 4 f 9 793 1 m 6 794 5 m 6 795 select id, sex, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) as a from wf14; 796 id sex a 797 10 null 20 798 10 null 31 799 11 null 31 800 2 f 5 801 3 f 9 802 4 f 9 803 1 m 6 804 5 m 6 805 drop table wf14; 806 drop database if exists test; 807 create database test; 808 use test; 809 drop table if exists bool01; 810 create table bool01(col1 int,col2 bool,col3 datetime); 811 insert into bool01 values(1, true, '2023-05-16 00:12:12'); 812 insert into bool01 values(2, false, '1997-01-13 12:12:00'); 813 insert into bool01 values(3, true, '2000-10-10 11:11:11'); 814 insert into bool01 values(4, false, '1020-10-01 01:01:01'); 815 insert into bool01 values(5, null, null); 816 insert into bool01 values(6, null, '1997-11-10 10:10:10'); 817 select * from bool01; 818 col1 col2 col3 819 1 true 2023-05-16 00:12:12 820 2 false 1997-01-13 12:12:00 821 3 true 2000-10-10 11:11:11 822 4 false 1020-10-01 01:01:01 823 5 null null 824 6 null 1997-11-10 10:10:10 825 select rank() over (partition by col2 order by col1), sum(col1) over (partition by col2 order by col3) from bool01; 826 rank() over (partition by col2 order by col1) sum(col1) over (partition by col2 order by col3) 827 1 5 828 2 11 829 2 4 830 1 6 831 2 3 832 1 4 833 select dense_rank() over (partition by col2 order by col1), sum(col1) over (partition by col2 order by col3) from bool01; 834 dense_rank() over (partition by col2 order by col1) sum(col1) over (partition by col2 order by col3) 835 1 5 836 2 11 837 2 4 838 1 6 839 2 3 840 1 4 841 drop table bool01; 842 drop table varchar01 if exists; 843 SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 1 column 23 near " if exists;"; 844 create table varchar01(col1 int, col2 varchar(12) primary key); 845 insert into varchar01 values(1, 'dhwenfewrfew'); 846 insert into varchar01 values(2, 'wyeuijdew'); 847 insert into varchar01 values(3, '数据库'); 848 insert into varchar01 values(4, 'hejwkvrewvre'); 849 insert into varchar01 values(5, '**&'); 850 insert into varchar01 values(6, '12345'); 851 insert into varchar01 values(7, 'database'); 852 select *, rank() over (partition by col2 order by col1) as tmp from varchar01; 853 col1 col2 tmp 854 5 **& 1 855 6 12345 1 856 7 database 1 857 1 dhwenfewrfew 1 858 4 hejwkvrewvre 1 859 2 wyeuijdew 1 860 3 数据库 1 861 select dense_rank() over (partition by col2 order by col1) as tmp from varchar01; 862 tmp 863 1 864 1 865 1 866 1 867 1 868 1 869 1 870 drop table varchar01; 871 drop table if exists char01; 872 create table char01 (col1 integer, col2 char(1)); 873 create table char01 (col1 integer, col2 char(1)); 874 table char01 already exists 875 insert into char01 values (1, 'm'); 876 insert into char01 values (2, 'f'); 877 insert into char01 values (3, 'f'); 878 insert into char01 values (4, 'f'); 879 insert into char01 values (5, 'm'); 880 select * from char01; 881 col1 col2 882 1 m 883 2 f 884 3 f 885 4 f 886 5 m 887 select *, rank() over (partition by col2 order by col1) as tmp from char01; 888 col1 col2 tmp 889 2 f 1 890 3 f 2 891 4 f 3 892 1 m 1 893 5 m 2 894 select dense_rank() over (partition by col2 order by col1) as tmp from char01; 895 tmp 896 1 897 2 898 3 899 1 900 2 901 drop table char01; 902 drop table if exists text01; 903 create table text01(col1 int, col2 text); 904 insert into text01 values(1, 'vdjnekwvrewvrjewkrmbew bkejwkvmekrememwkvrewvrew re'); 905 insert into text01 values(2, 'vdjnekwvrewvrjewkrmbew bkejwkvmekrememwkvrewvrew re'); 906 insert into text01 values(3, null); 907 insert into text01 values(4, '数据库,数据库,数据库,mo,mo,mo!'); 908 insert into text01 values(5, null); 909 insert into text01 values(6, '数据库,数据库,数据库,mo,mo,mo!'); 910 insert into text01 values(7, null); 911 select * from text01; 912 col1 col2 913 1 vdjnekwvrewvrjewkrmbew bkejwkvmekrememwkvrewvrew re 914 2 vdjnekwvrewvrjewkrmbew bkejwkvmekrememwkvrewvrew re 915 3 null 916 4 数据库,数据库,数据库,mo,mo,mo! 917 5 null 918 6 数据库,数据库,数据库,mo,mo,mo! 919 7 null 920 select *, rank() over (partition by col2 order by col1) as tmp from text01; 921 col1 col2 tmp 922 3 null 1 923 5 null 2 924 7 null 3 925 1 vdjnekwvrewvrjewkrmbew bkejwkvmekrememwkvrewvrew re 1 926 2 vdjnekwvrewvrjewkrmbew bkejwkvmekrememwkvrewvrew re 2 927 4 数据库,数据库,数据库,mo,mo,mo! 1 928 6 数据库,数据库,数据库,mo,mo,mo! 2 929 select dense_rank() over (partition by col2 order by col1) as tmp from text01; 930 tmp 931 1 932 2 933 3 934 1 935 2 936 1 937 2 938 drop table text01; 939 drop table if exists int01; 940 create table int01(col1 tinyint unsigned, col2 int, col3 timestamp); 941 insert into int01 values(100, 100, '2023-05-16 00:12:12'); 942 insert into int01 values(98, -10, '2023-05-16 00:12:12'); 943 insert into int01 values(100, null, '1997-05-16 00:12:12'); 944 insert into int01 values(null, 100, '2023-05-16 00:12:12'); 945 insert into int01 values(0, null, '1997-05-16 00:12:12'); 946 insert into int01 values(null, null, null); 947 select * from int01; 948 col1 col2 col3 949 100 100 2023-05-16 00:12:12 950 98 -10 2023-05-16 00:12:12 951 100 null 1997-05-16 00:12:12 952 null 100 2023-05-16 00:12:12 953 0 null 1997-05-16 00:12:12 954 null null null 955 select col1, avg(col2) over (partition by col1 order by col2) as tmp from int01; 956 col1 tmp 957 null null 958 null 100.0 959 0 null 960 98 -10.0 961 100 null 962 100 100.0 963 select col1, sum(col2) over (partition by col2 order by col1) as tmp from int01; 964 col1 tmp 965 null null 966 0 null 967 100 null 968 98 -10 969 null 100 970 100 200 971 select col1, max(col1) over (partition by col1 rows between 1 preceding and 1 following) from int01; 972 col1 max(col1) over (partition by col1 rows between 1 preceding and 1 following) 973 null null 974 null null 975 0 0 976 98 98 977 100 100 978 100 100 979 select col1, min(col2) over (partition by col3 order by col2) from int01; 980 col1 min(col2) over (partition by col3 order by col2) 981 null null 982 100 null 983 0 null 984 98 -10 985 null -10 986 100 -10 987 drop table int01; 988 drop table if exists float01; 989 create table float01(col1 float, col2 date); 990 insert into float01 values(12434321313.213213,'2020-01-01'); 991 insert into float01 values(null,'1997-01-13'); 992 insert into float01 values(-12434321313.213213,'1000-10-10'); 993 insert into float01 values(null,'2020-01-01'); 994 insert into float01 values(null,null); 995 insert into float01 values(12434321313.213213,null); 996 insert into float01 values(0,'1997-01-13'); 997 insert into float01 values(0,'1000-12-12'); 998 insert into float01 values(12434321313.213213,null); 999 select * from float01; 1000 col1 col2 1001 1.2434321E10 2020-01-01 1002 null 1997-01-13 1003 -1.2434321E10 1000-10-10 1004 null 2020-01-01 1005 null null 1006 1.2434321E10 null 1007 0.0 1997-01-13 1008 0.0 1000-12-12 1009 1.2434321E10 null 1010 select col2, avg(col1) over (partition by col1 order by col2) as tmp from float01; 1011 col2 tmp 1012 null null 1013 1997-01-13 null 1014 2020-01-01 null 1015 1000-10-10 -1.2434321408E10 1016 1000-12-12 0.0 1017 1997-01-13 0.0 1018 null 1.2434321408E10 1019 null 1.2434321408E10 1020 2020-01-01 1.2434321408E10 1021 select col2, sum(col1) over (partition by col2 order by col1) as tmp from float01; 1022 col2 tmp 1023 null null 1024 null 2.4868642816E10 1025 null 2.4868642816E10 1026 1000-10-10 -1.2434321408E10 1027 1000-12-12 0.0 1028 1997-01-13 null 1029 1997-01-13 0.0 1030 2020-01-01 null 1031 2020-01-01 1.2434321408E10 1032 select col2, max(col1) over (partition by col1 rows between 1 preceding and 1 following) from float01; 1033 col2 max(col1) over (partition by col1 rows between 1 preceding and 1 following) 1034 1997-01-13 null 1035 2020-01-01 null 1036 null null 1037 1000-10-10 -1.2434321E10 1038 1997-01-13 0.0 1039 1000-12-12 0.0 1040 2020-01-01 1.2434321E10 1041 null 1.2434321E10 1042 null 1.2434321E10 1043 select col2, min(col1) over (partition by col2 order by col2) from float01; 1044 col2 min(col1) over (partition by col2 order by col2) 1045 null null 1046 null 1.2434321E10 1047 null 1.2434321E10 1048 1000-10-10 -1.2434321E10 1049 1000-12-12 0.0 1050 1997-01-13 0.0 1051 1997-01-13 0.0 1052 2020-01-01 1.2434321E10 1053 2020-01-01 1.2434321E10 1054 drop table float01; 1055 drop table if exists double01; 1056 create table double01(d double); 1057 insert into double01 values (2); 1058 insert into double01 values (2); 1059 insert into double01 values (3); 1060 insert into double01 values (1); 1061 insert into double01 values (1); 1062 insert into double01 values (1.2); 1063 insert into double01 values (null); 1064 insert into double01 values (null); 1065 select * from double01; 1066 d 1067 2.0 1068 2.0 1069 3.0 1070 1.0 1071 1.0 1072 1.2 1073 null 1074 null 1075 select d, sum(d) over (partition by d order by d), avg(d) over (order by d) from double01; 1076 d sum(d) over (partition by d order by d) avg(d) over (order by d) 1077 null null null 1078 null null null 1079 1.0 2.0 1.0 1080 1.0 2.0 1.0 1081 1.2 1.2 1.0666666666666667 1082 2.0 4.0 1.44 1083 2.0 4.0 1.44 1084 3.0 3.0 1.7 1085 select d, sum(d) over (partition by d order by d), avg(d) over (order by d rows between 1 preceding and 1 following) from double01; 1086 d sum(d) over (partition by d order by d) avg(d) over (order by d rows between 1 preceding and 1 following) 1087 null null null 1088 null null 1.0 1089 1.0 2.0 1.0 1090 1.0 2.0 1.0666666666666667 1091 1.2 1.2 1.4000000000000001 1092 2.0 4.0 1.7333333333333334 1093 2.0 4.0 2.3333333333333335 1094 3.0 3.0 2.5 1095 select d, max(d) over (partition by d) from double01; 1096 d max(d) over (partition by d) 1097 null null 1098 null null 1099 1.0 1.0 1100 1.0 1.0 1101 1.2 1.2 1102 2.0 2.0 1103 2.0 2.0 1104 3.0 3.0 1105 select d, sum(d) over (partition by d order by d) from double01; 1106 d sum(d) over (partition by d order by d) 1107 null null 1108 null null 1109 1.0 2.0 1110 1.0 2.0 1111 1.2 1.2 1112 2.0 4.0 1113 2.0 4.0 1114 3.0 3.0 1115 truncate double01; 1116 select * from double01; 1117 d 1118 insert into double01 values (1.7976931348623157e+307); 1119 insert into double01 values (1); 1120 select * from double01; 1121 d 1122 1.7976931348623158E307 1123 1.0 1124 select d, sum(d) over (rows between current row and 1 following) from double01; 1125 d sum(d) over (rows between current row and 1 following) 1126 1.7976931348623158E307 1.7976931348623158E307 1127 1.0 1.0 1128 drop table double01; 1129 drop table if exists decimal01; 1130 create table decimal01(d decimal(38,3)); 1131 insert into decimal01 values (28888888888888888888888888888888888.1234); 1132 insert into decimal01 values (99999999999999999999999999999999999.83293323); 1133 insert into decimal01 values (0); 1134 insert into decimal01 values (-7841512312154312313158786541.342152121242143); 1135 insert into decimal01 values (-7841512312154312313158786541.342152121242143); 1136 insert into decimal01 values (99999999999999999999999999999999999.83293323); 1137 insert into decimal01 values (null); 1138 insert into decimal01 values (null); 1139 select * from decimal01; 1140 d 1141 28888888888888888888888888888888888.123 1142 99999999999999999999999999999999999.833 1143 0.000 1144 -7841512312154312313158786541.342 1145 -7841512312154312313158786541.342 1146 99999999999999999999999999999999999.833 1147 null 1148 null 1149 select max(d) over (partition by d order by d) from decimal01; 1150 max(d) over (partition by d order by d) 1151 null 1152 null 1153 -7841512312154312313158786541.342 1154 -7841512312154312313158786541.342 1155 0.000 1156 28888888888888888888888888888888888.123 1157 99999999999999999999999999999999999.833 1158 99999999999999999999999999999999999.833 1159 select min(d) over (partition by d order by d) from decimal01; 1160 min(d) over (partition by d order by d) 1161 null 1162 null 1163 -7841512312154312313158786541.342 1164 -7841512312154312313158786541.342 1165 0.000 1166 28888888888888888888888888888888888.123 1167 99999999999999999999999999999999999.833 1168 99999999999999999999999999999999999.833 1169 select avg(d) over (partition by d) from decimal01; 1170 invalid input: Decimal128 Div overflow: 28888888888888888888888888888888888123(Scale:3)/1(Scale:0) 1171 select sum(d) over (partition by d order by d rows between 1 preceding and 1 following) from decimal01; 1172 [unknown result because it is related to issue#10043] 1173 drop table decimal01; 1174 drop table if exists date01; 1175 create table date01(id date); 1176 insert into date01 values ('2002-06-09'); 1177 insert into date01 values ('2002-06-09'); 1178 insert into date01 values ('2002-06-10'); 1179 insert into date01 values ('2002-06-09'); 1180 insert into date01 values ('2015-08-01'); 1181 insert into date01 values ('2002-06-09'); 1182 insert into date01 values ('2015-08-01'); 1183 select id, rank() over () from date01; 1184 id rank() over () 1185 2002-06-09 1 1186 2002-06-09 1 1187 2002-06-10 1 1188 2002-06-09 1 1189 2015-08-01 1 1190 2002-06-09 1 1191 2015-08-01 1 1192 select id, dense_rank() over (order by id) from date01; 1193 id dense_rank() over (order by id) 1194 2002-06-09 1 1195 2002-06-09 1 1196 2002-06-09 1 1197 2002-06-09 1 1198 2002-06-10 2 1199 2015-08-01 3 1200 2015-08-01 3 1201 select id, max(id) over (order by id rows 2 preceding) from date01; 1202 id max(id) over (order by id rows 2 preceding) 1203 2002-06-09 2002-06-09 1204 2002-06-09 2002-06-09 1205 2002-06-09 2002-06-09 1206 2002-06-09 2002-06-09 1207 2002-06-10 2002-06-10 1208 2015-08-01 2015-08-01 1209 2015-08-01 2015-08-01 1210 select min(id) over (partition by id order by id range interval 2 day preceding) from date01; 1211 min(id) over (partition by id order by id range interval(2, day) preceding) 1212 2002-06-09 1213 2002-06-09 1214 2002-06-09 1215 2002-06-09 1216 2002-06-10 1217 2015-08-01 1218 2015-08-01 1219 select id, count(id) over (order by id rows between 2 preceding and 1 following) from date01; 1220 id count(id) over (order by id rows between 2 preceding and 1 following) 1221 2002-06-09 2 1222 2002-06-09 3 1223 2002-06-09 4 1224 2002-06-09 4 1225 2002-06-10 4 1226 2015-08-01 4 1227 2015-08-01 3 1228 select id, count(id) over (order by date_add(id,interval 3 day) rows between 2 preceding and 1 following) from date01; 1229 id count(id) over (order by date_add(id, interval(3, day)) rows between 2 preceding and 1 following) 1230 2002-06-09 2 1231 2002-06-09 3 1232 2002-06-09 4 1233 2002-06-09 4 1234 2002-06-10 4 1235 2015-08-01 4 1236 2015-08-01 3 1237 drop table date01; 1238 drop table if exists test01; 1239 create table test01(i int, j int); 1240 insert into test01 values (1,null); 1241 insert into test01 values (1,null); 1242 insert into test01 values (1,1); 1243 insert into test01 values (1,null); 1244 insert into test01 values (1,2); 1245 insert into test01 values (2,1); 1246 insert into test01 values (2,2); 1247 insert into test01 values (2,null); 1248 insert into test01 values (2,null); 1249 select * from test01; 1250 i j 1251 1 null 1252 1 null 1253 1 1 1254 1 null 1255 1 2 1256 2 1 1257 2 2 1258 2 null 1259 2 null 1260 select i, j, sum(j) over (partition by i order by j rows unbounded preceding) from test01; 1261 i j sum(j) over (partition by i order by j rows unbounded preceding) 1262 1 null null 1263 1 null null 1264 1 null null 1265 1 1 1 1266 1 2 3 1267 2 null null 1268 2 null null 1269 2 1 1 1270 2 2 3 1271 select i, j, avg(j) over (partition by i order by j rows unbounded preceding) from test01; 1272 i j avg(j) over (partition by i order by j rows unbounded preceding) 1273 1 null null 1274 1 null null 1275 1 null null 1276 1 1 1.0 1277 1 2 1.5 1278 2 null null 1279 2 null null 1280 2 1 1.0 1281 2 2 1.5 1282 select i, j, max(j) over (partition by i order by j rows unbounded preceding) from test01; 1283 i j max(j) over (partition by i order by j rows unbounded preceding) 1284 1 null null 1285 1 null null 1286 1 null null 1287 1 1 1 1288 1 2 2 1289 2 null null 1290 2 null null 1291 2 1 1 1292 2 2 2 1293 select i, j, min(j) over (partition by i order by j rows unbounded preceding) from test01; 1294 i j min(j) over (partition by i order by j rows unbounded preceding) 1295 1 null null 1296 1 null null 1297 1 null null 1298 1 1 1 1299 1 2 1 1300 2 null null 1301 2 null null 1302 2 1 1 1303 2 2 1 1304 drop table test01; 1305 drop table if exists row01; 1306 create table row01(i int,j int); 1307 insert into row01 values(1,1); 1308 insert into row01 values(1,4); 1309 insert into row01 values(1,2); 1310 insert into row01 values(1,4); 1311 select * from row01; 1312 i j 1313 1 1 1314 1 4 1315 1 2 1316 1 4 1317 select i, j, sum(i+j) over (rows unbounded preceding) foo from row01; 1318 i j foo 1319 1 1 2 1320 1 4 7 1321 1 2 10 1322 1 4 15 1323 select i, j, sum(i+j) over (rows between unbounded preceding and current row) foo from row01; 1324 i j foo 1325 1 1 2 1326 1 4 7 1327 1 2 10 1328 1 4 15 1329 select i, j, sum(i+j) over (rows unbounded preceding) foo from row01 order by foo; 1330 i j foo 1331 1 1 2 1332 1 4 7 1333 1 2 10 1334 1 4 15 1335 select i, j, sum(i+j) over (rows unbounded preceding) foo from row01 order by foo desc; 1336 i j foo 1337 1 4 15 1338 1 2 10 1339 1 4 7 1340 1 1 2 1341 select i, j, sum(i+j) over (order by j desc rows unbounded preceding) foo from row01; 1342 i j foo 1343 1 4 5 1344 1 4 10 1345 1 2 13 1346 1 1 15 1347 select i, j, sum(i+j) over (order by j desc rows between 2 preceding and 2 following) as foo from row01; 1348 i j foo 1349 1 4 13 1350 1 4 15 1351 1 2 15 1352 1 1 10 1353 select i, j, sum(i+j) over (order by j desc rows unbounded preceding) foo from row01 order by foo; 1354 i j foo 1355 1 4 5 1356 1 4 10 1357 1 2 13 1358 1 1 15 1359 select i, j, sum(i+j) over (order by j desc rows unbounded preceding) foo from row01 order by foo desc; 1360 i j foo 1361 1 1 15 1362 1 2 13 1363 1 4 10 1364 1 4 5 1365 select i, j, sum(i+j) over (rows unbounded preceding) foo from row01 order by foo desc limit 3; 1366 i j foo 1367 1 4 15 1368 1 2 10 1369 1 4 7 1370 select i, j, sum(i+j) over (order by j rows unbounded preceding) foo from row01; 1371 i j foo 1372 1 1 2 1373 1 2 5 1374 1 4 10 1375 1 4 15 1376 select i, j, sum(i+j) over (order by j rows unbounded preceding) foo from row01 order by foo; 1377 i j foo 1378 1 1 2 1379 1 2 5 1380 1 4 10 1381 1 4 15 1382 select i, j, sum(i+j) over (order by j rows unbounded preceding) foo from row01 order by foo desc; 1383 i j foo 1384 1 4 15 1385 1 4 10 1386 1 2 5 1387 1 1 2 1388 select i, j, sum(i+j) over (order by j rows between 2 preceding and 1 preceding) foo from row01 order by foo desc; 1389 i j foo 1390 1 4 8 1391 1 4 5 1392 1 2 2 1393 1 1 null 1394 select i, j, sum(i+j) over (order by j rows between 2 following and 1 following) foo from row01 order by foo desc; 1395 i j foo 1396 1 1 null 1397 1 2 null 1398 1 4 null 1399 1 4 null 1400 select i, j, sum(i+j) over (order by j rows between -1 following and 1 following) foo from row01 order by foo desc; 1401 SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 1 column 53 near " -1 following and 1 following) foo from row01 order by foo desc;"; 1402 select i, j, sum(i+j) over (order by j rows between 2 preceding and -10 following) foo from row01 order by foo desc; 1403 SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 1 column 69 near " -10 following) foo from row01 order by foo desc;"; 1404 drop table row01; 1405 drop table if exists wf02; 1406 create table wf02 (i int) ; 1407 insert into wf02 (i) values (1); 1408 insert into wf02 (i) values (2); 1409 insert into wf02 (i) values (3); 1410 insert into wf02 (i) values (4); 1411 insert into wf02 (i) values (5); 1412 select * from wf02; 1413 i 1414 1 1415 2 1416 3 1417 4 1418 5 1419 select i, sum(i) over (rows between 0 preceding and 2 following) from wf02; 1420 i sum(i) over (rows between 0 preceding and 2 following) 1421 1 6 1422 2 9 1423 3 12 1424 4 9 1425 5 5 1426 select i, sum(i) over (order by i rows between 2 preceding and 2 following) from wf02 limit 3; 1427 i sum(i) over (order by i rows between 2 preceding and 2 following) 1428 1 6 1429 2 10 1430 3 15 1431 select i, sum(i * 20) over (rows between 2 preceding and 2 following) from wf02 order by i desc limit 3; 1432 i sum(i * 20) over (rows between 2 preceding and 2 following) 1433 5 240 1434 4 280 1435 3 300 1436 select i, avg(i) over (rows between 2 preceding and 2 following) from wf02; 1437 i avg(i) over (rows between 2 preceding and 2 following) 1438 1 2.0 1439 2 2.5 1440 3 3.0 1441 4 3.5 1442 5 4.0 1443 select i, avg(i + 100) over (rows between 2 preceding and 2 following) from wf02; 1444 i avg(i + 100) over (rows between 2 preceding and 2 following) 1445 1 102.0 1446 2 102.5 1447 3 103.0 1448 4 103.5 1449 5 104.0 1450 select i, sum(i) over (rows between 1 preceding and 2 following) from wf02; 1451 i sum(i) over (rows between 1 preceding and 2 following) 1452 1 6 1453 2 10 1454 3 14 1455 4 12 1456 5 9 1457 drop table wf02; 1458 drop table if exists og01; 1459 create table og01(i int, j int, k int); 1460 insert into og01 values (1,1,1); 1461 insert into og01 values (1,4,1); 1462 insert into og01 values (1,2,1); 1463 insert into og01 values (1,4,1); 1464 insert into og01 values (1,1,2); 1465 insert into og01 values (1,4,2); 1466 insert into og01 values (1,2,2); 1467 insert into og01 values (1,4,2); 1468 insert into og01 values (1,1,3); 1469 insert into og01 values (1,4,3); 1470 insert into og01 values (1,2,3); 1471 insert into og01 values (1,4,3); 1472 insert into og01 values (1,1,4); 1473 insert into og01 values (1,4,4); 1474 insert into og01 values (1,2,4); 1475 insert into og01 values (1,4,4); 1476 select * from og01; 1477 i j k 1478 1 1 1 1479 1 4 1 1480 1 2 1 1481 1 4 1 1482 1 1 2 1483 1 4 2 1484 1 2 2 1485 1 4 2 1486 1 1 3 1487 1 4 3 1488 1 2 3 1489 1 4 3 1490 1 1 4 1491 1 4 4 1492 1 2 4 1493 1 4 4 1494 select k, sum(k) over (rows unbounded preceding) wf from og01; 1495 k wf 1496 1 1 1497 1 2 1498 1 3 1499 1 4 1500 2 6 1501 2 8 1502 2 10 1503 2 12 1504 3 15 1505 3 18 1506 3 21 1507 3 24 1508 4 28 1509 4 32 1510 4 36 1511 4 40 1512 select k, min(i), sum(j), sum(k) over (rows unbounded preceding) wf from og01 group by (k); 1513 k min(i) sum(j) wf 1514 1 1 11 1 1515 2 1 11 3 1516 3 1 11 6 1517 4 1 11 10 1518 select k, min(i), sum(j), sum(k) over (rows unbounded preceding) wf from og01 group by (k) order by wf desc; 1519 k min(i) sum(j) wf 1520 4 1 11 10 1521 3 1 11 6 1522 2 1 11 3 1523 1 1 11 1 1524 select k, sum(k) over (rows unbounded preceding) foo from og01 group by (k); 1525 k foo 1526 1 1 1527 2 3 1528 3 6 1529 4 10 1530 select k, avg(distinct j), sum(k) over (rows unbounded preceding) foo from og01 group by (k); 1531 k avg(distinct j) foo 1532 1 2.3333333333333335 1 1533 2 2.3333333333333335 3 1534 3 2.3333333333333335 6 1535 4 2.3333333333333335 10 1536 select k, sum(k+1) over (rows unbounded preceding) foo from og01 group by (k); 1537 k foo 1538 1 2 1539 2 5 1540 3 9 1541 4 14 1542 select k, sum(k+1) over (order by k desc rows unbounded preceding) foo from og01 group by (k); 1543 k foo 1544 4 5 1545 3 9 1546 2 12 1547 1 14 1548 drop table og01; 1549 drop table if exists og02; 1550 create table og02 (id integer, sex char(1)); 1551 insert into og02 values (1, 'm'); 1552 insert into og02 values (2, 'f'); 1553 insert into og02 values (3, 'f'); 1554 insert into og02 values (4, 'f'); 1555 insert into og02 values (5, 'm'); 1556 insert into og02 values (10, null); 1557 insert into og02 values (11, null); 1558 select * from og02; 1559 id sex 1560 1 m 1561 2 f 1562 3 f 1563 4 f 1564 5 m 1565 10 null 1566 11 null 1567 drop table if exists og03; 1568 create table og03(c char(1)); 1569 insert into og03 values ('m'); 1570 select * from og03; 1571 c 1572 m 1573 select sex, avg(id), row_number() over (partition by sex) from og02 1574 group by sex order by sex desc; 1575 sex avg(id) row_number() over (partition by sex) 1576 m 3.0 1 1577 f 3.0 1 1578 null 10.5 1 1579 select sex, avg(id), row_number() over (partition by sex) from og02 1580 group by sex order by sex desc; 1581 sex avg(id) row_number() over (partition by sex) 1582 m 3.0 1 1583 f 3.0 1 1584 null 10.5 1 1585 select sex, avg(id), sum(avg(id) + 10) over (rows unbounded preceding) from og02 1586 group by sex order by sex desc; 1587 sex avg(id) sum(avg(id) + 10) over (rows unbounded preceding) 1588 m 3.0 13.0 1589 f 3.0 26.0 1590 null 10.5 46.5 1591 select sex, avg(id), row_number() over (partition by sex) from og02 1592 group by sex having sex='m' or sex is null order by sex desc; 1593 sex avg(id) row_number() over (partition by sex) 1594 m 3.0 1 1595 null 10.5 1 1596 select sex, avg(id), sum(avg(id)) over (rows unbounded preceding) from og02 1597 group by sex having sex='m' or sex='f' or sex is null 1598 order by sex desc; 1599 sex avg(id) sum(avg(id)) over (rows unbounded preceding) 1600 m 3.0 3.0 1601 f 3.0 6.0 1602 null 10.5 16.5 1603 select sex, avg(id), row_number() over (partition by sex) from og02 1604 group by sex having sex=(select c from og03 limit 1) or sex is null 1605 order by sex desc; 1606 sex avg(id) row_number() over (partition by sex) 1607 m 3.0 1 1608 null 10.5 1 1609 select sex, avg(id), sum(avg(id)) over (rows unbounded preceding) from og02 1610 group by sex having sex=(select c from og03 limit 1) or sex='f' or sex is null 1611 order by sex desc; 1612 sex avg(id) sum(avg(id)) over (rows unbounded preceding) 1613 m 3.0 3.0 1614 f 3.0 6.0 1615 null 10.5 16.5 1616 select sex, avg(id), sum(avg(id)) over (order by sex rows unbounded preceding) from og02 1617 group by sex 1618 order by sex desc; 1619 sex avg(id) sum(avg(id)) over (order by sex rows unbounded preceding) 1620 m 3.0 16.5 1621 f 3.0 13.5 1622 null 10.5 10.5 1623 select sex, avg(id), sum(avg(id)) over (order by sex rows unbounded preceding) from og02 1624 group by sex having sex=(select c from og03 limit 1) or sex='f' or sex is null 1625 order by sex desc; 1626 sex avg(id) sum(avg(id)) over (order by sex rows unbounded preceding) 1627 m 3.0 16.5 1628 f 3.0 13.5 1629 null 10.5 10.5 1630 drop table og02; 1631 drop table og03; 1632 drop table if exists date02; 1633 create table date02(col1 date,col2 datetime, col3 time, col4 timestamp); 1634 insert into date02 values ('2002-06-09','1997-01-13 00:00:00','12:00:59','2023-05-16 00:12:12'); 1635 insert into date02 values ('2002-06-09','2020-02-20 00:00:00','11:12:12','2023-05-18 12:12:12'); 1636 insert into date02 values ('2002-06-10','1997-01-13 00:00:00','12:00:59','2023-05-16 00:12:12'); 1637 insert into date02 values ('2002-06-09','2020-02-20 00:00:00','11:12:12','2023-05-16 00:12:12'); 1638 insert into date02 values ('2015-08-01',null,null,'2023-05-18 12:12:12'); 1639 insert into date02 values ('2002-06-09',null,'01:01:01',null); 1640 insert into date02 values ('2015-08-01','1990-01-01 01:02:03',null,null); 1641 select * from date02; 1642 col1 col2 col3 col4 1643 2002-06-09 1997-01-13 00:00:00 12:00:59 2023-05-16 00:12:12 1644 2002-06-09 2020-02-20 00:00:00 11:12:12 2023-05-18 12:12:12 1645 2002-06-10 1997-01-13 00:00:00 12:00:59 2023-05-16 00:12:12 1646 2002-06-09 2020-02-20 00:00:00 11:12:12 2023-05-16 00:12:12 1647 2015-08-01 null null 2023-05-18 12:12:12 1648 2002-06-09 null 01:01:01 null 1649 2015-08-01 1990-01-01 01:02:03 null null 1650 select dense_rank() over (partition by col1 order by date_format(col1,'%m-%d-%Y')) from date02; 1651 SQL parser error: Window '<unnamed window>' with RANGE N PRECEDING/FOLLOWING frame requires exactly one ORDER BY expression, of numeric or temporal type 1652 select max(col2) over (partition by col3 order by date(col2) desc) from date02; 1653 max(col2) over (partition by col3 order by date(col2) desc) 1654 1990-01-01 01:02:03 1655 1990-01-01 01:02:03 1656 null 1657 2020-02-20 00:00:00 1658 2020-02-20 00:00:00 1659 1997-01-13 00:00:00 1660 1997-01-13 00:00:00 1661 select rank() over (order by col1 range interval 2 day preceding) from date02; 1662 rank() over (order by col1 range interval(2, day) preceding) 1663 1 1664 1 1665 1 1666 1 1667 5 1668 6 1669 6 1670 select max(col3) over (order by date_add(col2,interval 2 minute) rows between 2 preceding and 1 following) from date02; 1671 max(col3) over (order by date_add(col2, interval(2, minute)) rows between 2 preceding and 1 following) 1672 01:01:01 1673 01:01:01 1674 12:00:59 1675 12:00:59 1676 12:00:59 1677 12:00:59 1678 12:00:59 1679 select min(col3) over (partition by col4 order by date_sub(col2,interval 2 minute) rows between 2 preceding and 1 following) from date02; 1680 min(col3) over (partition by col4 order by date_sub(col2, interval(2, minute)) rows between 2 preceding and 1 following) 1681 01:01:01 1682 01:01:01 1683 12:00:59 1684 11:12:12 1685 11:12:12 1686 11:12:12 1687 11:12:12 1688 select max(col3) over (order by year(col2) rows between current row and unbounded following) from date02; 1689 max(col3) over (order by year(col2) rows between current row and unbounded following) 1690 12:00:59 1691 12:00:59 1692 12:00:59 1693 12:00:59 1694 12:00:59 1695 11:12:12 1696 11:12:12 1697 select dense_rank() over (order by month(col3)) from date02; 1698 dense_rank() over (order by month(col3)) 1699 1 1700 1 1701 2 1702 2 1703 2 1704 2 1705 2 1706 drop table date02; 1707 drop table if exists dense_rank01; 1708 create table dense_rank01 (id integer, sex char(1)); 1709 insert into dense_rank01 values (1, 'm'); 1710 insert into dense_rank01 values (2, 'f'); 1711 insert into dense_rank01 values (3, 'f'); 1712 insert into dense_rank01 values (4, 'f'); 1713 insert into dense_rank01 values (5, 'm'); 1714 select * from dense_rank01; 1715 id sex 1716 1 m 1717 2 f 1718 3 f 1719 4 f 1720 5 m 1721 drop table if exists dense_rank02; 1722 create table dense_rank02 (user_id integer not null, date date); 1723 insert into dense_rank02 values (1, '2002-06-09'); 1724 insert into dense_rank02 values (2, '2002-06-09'); 1725 insert into dense_rank02 values (1, '2002-06-09'); 1726 insert into dense_rank02 values (3, '2002-06-09'); 1727 insert into dense_rank02 values (4, '2002-06-09'); 1728 insert into dense_rank02 values (4, '2002-06-09'); 1729 insert into dense_rank02 values (5, '2002-06-09'); 1730 select * from dense_rank02; 1731 user_id date 1732 1 2002-06-09 1733 2 2002-06-09 1734 1 2002-06-09 1735 3 2002-06-09 1736 4 2002-06-09 1737 4 2002-06-09 1738 5 2002-06-09 1739 select rank() over (order by user_id) r from dense_rank02; 1740 r 1741 1 1742 1 1743 3 1744 4 1745 5 1746 5 1747 7 1748 select dense_rank() over (order by user_id) r from dense_rank02; 1749 r 1750 1 1751 1 1752 2 1753 3 1754 4 1755 4 1756 5 1757 select rank() over () r from dense_rank02; 1758 r 1759 1 1760 1 1761 1 1762 1 1763 1 1764 1 1765 1 1766 select dense_rank() over () r from dense_rank02; 1767 r 1768 1 1769 1 1770 1 1771 1 1772 1 1773 1 1774 1 1775 select id, sex, rank() over (order by sex rows unbounded preceding) from dense_rank01 order by id; 1776 id sex rank() over (order by sex rows unbounded preceding) 1777 1 m 4 1778 2 f 1 1779 3 f 1 1780 4 f 1 1781 5 m 4 1782 select id, sex, dense_rank() over (order by sex rows unbounded preceding) from dense_rank01 order by id; 1783 id sex dense_rank() over (order by sex rows unbounded preceding) 1784 1 m 2 1785 2 f 1 1786 3 f 1 1787 4 f 1 1788 5 m 2 1789 select sex, rank() over (order by sex desc rows unbounded preceding) `rank`, avg(distinct id) as uids from dense_rank01 u, dense_rank02 1790 where dense_rank02.user_id = u.id group by sex order by sex; 1791 sex rank uids 1792 f 2 3.0 1793 m 1 3.0 1794 select sex, dense_rank() over (order by sex desc rows unbounded preceding) `rank`, avg(distinct id) as uids from dense_rank01 u, dense_rank02 1795 where dense_rank02.user_id = u.id group by sex order by sex; 1796 sex rank uids 1797 f 2 3.0 1798 m 1 3.0 1799 select sex, avg(id) as uids, rank() over (order by avg(id)) `rank` from dense_rank01 u, dense_rank02 1800 where dense_rank02.user_id = u.id group by sex; 1801 sex uids rank 1802 m 2.3333333333333335 1 1803 f 3.25 2 1804 select sex, avg(id) as uids, dense_rank() over (order by avg(id)) `rank` from dense_rank01 u, dense_rank02 1805 where dense_rank02.user_id = u.id group by sex; 1806 sex uids rank 1807 m 2.3333333333333335 1 1808 f 3.25 2 1809 select sex, avg(distinct id) as uids, rank() over (order by avg(distinct id) desc) `rank` from dense_rank01 u, dense_rank02 1810 where dense_rank02.user_id = u.id group by sex 1811 order by sex; 1812 sex uids rank 1813 f 3.0 1 1814 m 3.0 1 1815 select sex, avg(distinct id) as uids, dense_rank() over (order by avg(distinct id) desc) `p_rank` from dense_rank01 u, dense_rank02 1816 where dense_rank02.user_id = u.id group by sex 1817 order by sex; 1818 sex uids p_rank 1819 f 3.0 1 1820 m 3.0 1 1821 select sex, avg(id) as uids, rank() over (order by avg(id) desc) `rank` from dense_rank01 u, dense_rank02 1822 where dense_rank02.user_id = u.id group by sex 1823 order by `rank` desc; 1824 sex uids rank 1825 m 2.3333333333333335 2 1826 f 3.25 1 1827 select sex, avg(id) as uids, dense_rank() over (order by avg(id) desc) `p_rank` 1828 from dense_rank01 u, dense_rank02 1829 where dense_rank02.user_id = u.id group by sex 1830 order by `p_rank` desc; 1831 sex uids p_rank 1832 m 2.3333333333333335 2 1833 f 3.25 1 1834 insert into dense_rank01 values (10, null); 1835 insert into dense_rank01 values (11, null); 1836 select id, sex, rank() over (order by sex rows unbounded preceding)from dense_rank01 order by id; 1837 id sex rank() over (order by sex rows unbounded preceding) 1838 1 m 6 1839 2 f 3 1840 3 f 3 1841 4 f 3 1842 5 m 6 1843 10 null 1 1844 11 null 1 1845 select id, sex, dense_rank() over (order by sex rows unbounded preceding) from dense_rank01 order by id; 1846 id sex dense_rank() over (order by sex rows unbounded preceding) 1847 1 m 3 1848 2 f 2 1849 3 f 2 1850 4 f 2 1851 5 m 3 1852 10 null 1 1853 11 null 1 1854 select id, sex, rank() over (order by sex desc rows unbounded preceding) from dense_rank01 order by id; 1855 id sex rank() over (order by sex desc rows unbounded preceding) 1856 1 m 1 1857 2 f 3 1858 3 f 3 1859 4 f 3 1860 5 m 1 1861 10 null 6 1862 11 null 6 1863 select id value, 1864 sum(id) over (rows unbounded preceding) 1865 from dense_rank01 left join dense_rank02 on dense_rank02.user_id = dense_rank01.id; 1866 value sum(id) over (rows unbounded preceding) 1867 1 1 1868 1 2 1869 2 4 1870 3 7 1871 4 11 1872 4 15 1873 5 20 1874 10 30 1875 11 41 1876 select id value, 1877 sum(id) over (rows unbounded preceding) 1878 from dense_rank01 right join dense_rank02 on dense_rank02.user_id = dense_rank01.id; 1879 value sum(id) over (rows unbounded preceding) 1880 1 1 1881 2 3 1882 1 4 1883 3 7 1884 4 11 1885 4 15 1886 5 20 1887 select id value, 1888 sum(id) over (rows unbounded preceding) 1889 from dense_rank01 inner join dense_rank02 on dense_rank02.user_id = dense_rank01.id; 1890 value sum(id) over (rows unbounded preceding) 1891 1 1 1892 1 2 1893 2 4 1894 3 7 1895 4 11 1896 4 15 1897 5 20 1898 select id value, 1899 sum(id) over (partition by id order by id rows unbounded preceding) 1900 from dense_rank01 natural join dense_rank02; 1901 value sum(id) over (partition by id order by id rows unbounded preceding) 1902 1 1 1903 1 2 1904 1 3 1905 1 4 1906 1 5 1907 1 6 1908 1 7 1909 2 2 1910 2 4 1911 2 6 1912 2 8 1913 2 10 1914 2 12 1915 2 14 1916 3 3 1917 3 6 1918 3 9 1919 3 12 1920 3 15 1921 3 18 1922 3 21 1923 4 4 1924 4 8 1925 4 12 1926 4 16 1927 4 20 1928 4 24 1929 4 28 1930 5 5 1931 5 10 1932 5 15 1933 5 20 1934 5 25 1935 5 30 1936 5 35 1937 10 10 1938 10 20 1939 10 30 1940 10 40 1941 10 50 1942 10 60 1943 10 70 1944 11 11 1945 11 22 1946 11 33 1947 11 44 1948 11 55 1949 11 66 1950 11 77 1951 select id value, 1952 sum(id) over (partition by id order by id rows unbounded preceding) 1953 from dense_rank01 full join dense_rank02; 1954 value sum(id) over (partition by id order by id rows unbounded preceding) 1955 1 1 1956 1 2 1957 1 3 1958 1 4 1959 1 5 1960 1 6 1961 1 7 1962 2 2 1963 2 4 1964 2 6 1965 2 8 1966 2 10 1967 2 12 1968 2 14 1969 3 3 1970 3 6 1971 3 9 1972 3 12 1973 3 15 1974 3 18 1975 3 21 1976 4 4 1977 4 8 1978 4 12 1979 4 16 1980 4 20 1981 4 24 1982 4 28 1983 5 5 1984 5 10 1985 5 15 1986 5 20 1987 5 25 1988 5 30 1989 5 35 1990 10 10 1991 10 20 1992 10 30 1993 10 40 1994 10 50 1995 10 60 1996 10 70 1997 11 11 1998 11 22 1999 11 33 2000 11 44 2001 11 55 2002 11 66 2003 11 77 2004 select sex, avg(id), rank() over (order by avg(id) desc) from dense_rank01 group by sex order by sex; 2005 sex avg(id) rank() over (order by avg(id) desc) 2006 null 10.5 1 2007 f 3.0 2 2008 m 3.0 2 2009 select sex, dense_rank() over (order by avg(id) desc) from dense_rank01 group by sex order by sex; 2010 sex dense_rank() over (order by avg(id) desc) 2011 null 1 2012 f 2 2013 m 2 2014 select sex, rank() over (order by avg(id) desc) from dense_rank01 group by sex order by sex; 2015 sex rank() over (order by avg(id) desc) 2016 null 1 2017 f 2 2018 m 2 2019 select rank() over (order by avg(id)) from dense_rank01; 2020 rank() over (order by avg(id)) 2021 1 2022 select dense_rank() over (order by avg(id)) from dense_rank01; 2023 dense_rank() over (order by avg(id)) 2024 1 2025 select avg(id), rank() over (order by avg(id)) from dense_rank01; 2026 avg(id) rank() over (order by avg(id)) 2027 5.142857142857143 1 2028 select avg(id), dense_rank() over (order by avg(id)) from dense_rank01; 2029 avg(id) dense_rank() over (order by avg(id)) 2030 5.142857142857143 1 2031 select avg(id), sum(avg(id)) over (order by avg(id) rows unbounded preceding) from dense_rank01; 2032 avg(id) sum(avg(id)) over (order by avg(id) rows unbounded preceding) 2033 5.142857142857143 5.142857142857143 2034 select sex, id, rank() over (partition by sex order by id desc) from dense_rank01; 2035 sex id rank() over (partition by sex order by id desc) 2036 null 11 1 2037 null 10 2 2038 f 4 1 2039 f 3 2 2040 f 2 3 2041 m 5 1 2042 m 1 2 2043 select sex, id, dense_rank() over (partition by sex order by id desc) from dense_rank01; 2044 sex id dense_rank() over (partition by sex order by id desc) 2045 null 11 1 2046 null 10 2 2047 f 4 1 2048 f 3 2 2049 f 2 3 2050 m 5 1 2051 m 1 2 2052 select sex, id, rank() over (partition by sex order by id asc) from dense_rank01; 2053 sex id rank() over (partition by sex order by id asc) 2054 null 10 1 2055 null 11 2 2056 f 2 1 2057 f 3 2 2058 f 4 3 2059 m 1 1 2060 m 5 2 2061 select sex, id, dense_rank() over (partition by sex order by id asc) from dense_rank01; 2062 sex id dense_rank() over (partition by sex order by id asc) 2063 null 10 1 2064 null 11 2 2065 f 2 1 2066 f 3 2 2067 f 4 3 2068 m 1 1 2069 m 5 2 2070 select sex, id, sum(id) over (partition by sex order by id asc rows unbounded preceding) summ, 2071 rank() over (partition by sex order by id asc rows unbounded preceding) `rank` from dense_rank01; 2072 sex id summ rank 2073 null 10 10 1 2074 null 11 21 2 2075 f 2 2 1 2076 f 3 5 2 2077 f 4 9 3 2078 m 1 1 1 2079 m 5 6 2 2080 select sex, id, sum(id) over (partition by sex order by id asc rows unbounded preceding) summ, 2081 dense_rank() over (partition by sex order by id asc rows unbounded preceding) `d_rank` from dense_rank01; 2082 sex id summ d_rank 2083 null 10 10 1 2084 null 11 21 2 2085 f 2 2 1 2086 f 3 5 2 2087 f 4 9 3 2088 m 1 1 1 2089 m 5 6 2 2090 select sex, id, sum(id) over (partition by sex order by id asc rows unbounded preceding) summ, 2091 rank() over (partition by sex order by id asc rows unbounded preceding) `rank` from dense_rank01 2092 order by summ; 2093 sex id summ rank 2094 m 1 1 1 2095 f 2 2 1 2096 f 3 5 2 2097 m 5 6 2 2098 f 4 9 3 2099 null 10 10 1 2100 null 11 21 2 2101 select sex, id, sum(id) over (partition by sex order by id asc rows unbounded preceding) summ, 2102 dense_rank() over (partition by sex order by id asc rows unbounded preceding) `p_rank` from dense_rank01 2103 order by summ; 2104 sex id summ p_rank 2105 m 1 1 1 2106 f 2 2 1 2107 f 3 5 2 2108 m 5 6 2 2109 f 4 9 3 2110 null 10 10 1 2111 null 11 21 2 2112 select sex, avg(distinct id),rank() over (order by uids desc) `uids` 2113 from dense_rank01 u, dense_rank01 where dense_rank01.user_id = u.id group by sex 2114 order by sex; 2115 invalid input: ambiguouse column reference to 'sex' 2116 select sex, avg(distinct id),rank() over (order by uids desc) `uids` 2117 from dense_rank01 u, dense_rank02 where dense_rank02.user_id = u.id 2118 group by sex order by sex; 2119 invalid input: column uids does not exist 2120 drop table dense_rank01; 2121 drop table dense_rank02; 2122 drop table if exists dense_rank03; 2123 create table dense_rank03(d decimal(10,2), date date); 2124 insert into dense_rank03 values (10.4, '2002-06-09'); 2125 insert into dense_rank03 values (20.5, '2002-06-09'); 2126 insert into dense_rank03 values (10.4, '2002-06-10'); 2127 insert into dense_rank03 values (3, '2002-06-09'); 2128 insert into dense_rank03 values (40.2, '2015-08-01'); 2129 insert into dense_rank03 values (40.2, '2002-06-09'); 2130 insert into dense_rank03 values (5, '2015-08-01'); 2131 select * from dense_rank03; 2132 d date 2133 10.40 2002-06-09 2134 20.50 2002-06-09 2135 10.40 2002-06-10 2136 3.00 2002-06-09 2137 40.20 2015-08-01 2138 40.20 2002-06-09 2139 5.00 2015-08-01 2140 select * from (select rank() over (order by d) as `rank`, d, date from dense_rank03) alias order by `rank`, d, date; 2141 rank d date 2142 1 3.00 2002-06-09 2143 2 5.00 2015-08-01 2144 3 10.40 2002-06-09 2145 3 10.40 2002-06-10 2146 5 20.50 2002-06-09 2147 6 40.20 2002-06-09 2148 6 40.20 2015-08-01 2149 select * from (select dense_rank() over (order by d) as `d_rank`, d, date from dense_rank03) alias order by `d_rank`, d, date; 2150 d_rank d date 2151 1 3.00 2002-06-09 2152 2 5.00 2015-08-01 2153 3 10.40 2002-06-09 2154 3 10.40 2002-06-10 2155 4 20.50 2002-06-09 2156 5 40.20 2002-06-09 2157 5 40.20 2015-08-01 2158 select * from (select rank() over (order by date) as `rank`, date, d from dense_rank03) alias order by `rank`, d desc; 2159 rank date d 2160 1 2002-06-09 40.20 2161 1 2002-06-09 20.50 2162 1 2002-06-09 10.40 2163 1 2002-06-09 3.00 2164 5 2002-06-10 10.40 2165 6 2015-08-01 40.20 2166 6 2015-08-01 5.00 2167 select * from (select dense_rank() over (order by date) as `p_rank`, date, d from dense_rank03) alias order by `p_rank`, d desc; 2168 p_rank date d 2169 1 2002-06-09 40.20 2170 1 2002-06-09 20.50 2171 1 2002-06-09 10.40 2172 1 2002-06-09 3.00 2173 2 2002-06-10 10.40 2174 3 2015-08-01 40.20 2175 3 2015-08-01 5.00 2176 drop table dense_rank03; 2177 drop table if exists rank01; 2178 create table rank01(i int, j int, k int); 2179 insert into rank01 values (1,1,1); 2180 insert into rank01 values (1,1,2); 2181 insert into rank01 values (1,1,2); 2182 insert into rank01 values (1,2,1); 2183 insert into rank01 values (1,2,2); 2184 insert into rank01 values (2,1,1); 2185 insert into rank01 values (2,1,1); 2186 insert into rank01 values (2,1,2); 2187 insert into rank01 values (2,2,1); 2188 insert into rank01 values (2,2,2); 2189 select * from rank01; 2190 i j k 2191 1 1 1 2192 1 1 2 2193 1 1 2 2194 1 2 1 2195 1 2 2 2196 2 1 1 2197 2 1 1 2198 2 1 2 2199 2 2 1 2200 2 2 2 2201 select *, rank() over (order by i,j,k) as o_ijk, 2202 rank() over (order by j) as o_j, 2203 rank() over (order by k,j) as o_kj from rank01 order by i,j,k; 2204 i j k o_ijk o_j o_kj 2205 1 1 1 1 1 1 2206 1 1 2 2 1 6 2207 1 1 2 2 1 6 2208 1 2 1 4 7 4 2209 1 2 2 5 7 9 2210 2 1 1 6 1 1 2211 2 1 1 6 1 1 2212 2 1 2 8 1 6 2213 2 2 1 9 7 4 2214 2 2 2 10 7 9 2215 drop table rank01; 2216 drop table if exists row_number01; 2217 create table row_number01 (id integer, sex char(1)); 2218 insert into row_number01 values (1, 'm'); 2219 insert into row_number01 values (2, 'f'); 2220 insert into row_number01 values (3, 'f'); 2221 insert into row_number01 values (4, 'f'); 2222 insert into row_number01 values (5, 'm'); 2223 select * from row_number01; 2224 id sex 2225 1 m 2226 2 f 2227 3 f 2228 4 f 2229 5 m 2230 drop table if exists row_number02; 2231 create table row_number02 (user_id integer not null, date date); 2232 insert into row_number02 values (1, '2002-06-09'); 2233 insert into row_number02 values (2, '2002-06-09'); 2234 insert into row_number02 values (1, '2002-06-09'); 2235 insert into row_number02 values (3, '2002-06-09'); 2236 insert into row_number02 values (4, '2002-06-09'); 2237 insert into row_number02 values (4, '2002-06-09'); 2238 insert into row_number02 values (5, '2002-06-09'); 2239 select * from row_number02; 2240 user_id date 2241 1 2002-06-09 2242 2 2002-06-09 2243 1 2002-06-09 2244 3 2002-06-09 2245 4 2002-06-09 2246 4 2002-06-09 2247 5 2002-06-09 2248 select user_id, row_number() over (partition by user_id) from row_number02 row_number01; 2249 user_id row_number() over (partition by user_id) 2250 1 1 2251 1 2 2252 2 1 2253 3 1 2254 4 1 2255 4 2 2256 5 1 2257 select sex, id, date, row_number() over (partition by date order by id) as row_no, rank() over (partition by date order by id) as `rank` from row_number01,row_number02 2258 where row_number01.id=row_number02.user_id; 2259 sex id date row_no rank 2260 m 1 2002-06-09 1 1 2261 m 1 2002-06-09 2 1 2262 f 2 2002-06-09 3 3 2263 f 3 2002-06-09 4 4 2264 f 4 2002-06-09 5 5 2265 f 4 2002-06-09 6 5 2266 m 5 2002-06-09 7 7 2267 select date,id, rank() over (partition by date order by id) as `rank` from row_number01,row_number02; 2268 date id rank 2269 2002-06-09 1 1 2270 2002-06-09 1 1 2271 2002-06-09 1 1 2272 2002-06-09 1 1 2273 2002-06-09 1 1 2274 2002-06-09 1 1 2275 2002-06-09 1 1 2276 2002-06-09 2 8 2277 2002-06-09 2 8 2278 2002-06-09 2 8 2279 2002-06-09 2 8 2280 2002-06-09 2 8 2281 2002-06-09 2 8 2282 2002-06-09 2 8 2283 2002-06-09 3 15 2284 2002-06-09 3 15 2285 2002-06-09 3 15 2286 2002-06-09 3 15 2287 2002-06-09 3 15 2288 2002-06-09 3 15 2289 2002-06-09 3 15 2290 2002-06-09 4 22 2291 2002-06-09 4 22 2292 2002-06-09 4 22 2293 2002-06-09 4 22 2294 2002-06-09 4 22 2295 2002-06-09 4 22 2296 2002-06-09 4 22 2297 2002-06-09 5 29 2298 2002-06-09 5 29 2299 2002-06-09 5 29 2300 2002-06-09 5 29 2301 2002-06-09 5 29 2302 2002-06-09 5 29 2303 2002-06-09 5 29 2304 select * from (select date,id, rank() over (partition by date order by id) as `rank` from row_number01,row_number02) alias; 2305 date id rank 2306 2002-06-09 1 1 2307 2002-06-09 1 1 2308 2002-06-09 1 1 2309 2002-06-09 1 1 2310 2002-06-09 1 1 2311 2002-06-09 1 1 2312 2002-06-09 1 1 2313 2002-06-09 2 8 2314 2002-06-09 2 8 2315 2002-06-09 2 8 2316 2002-06-09 2 8 2317 2002-06-09 2 8 2318 2002-06-09 2 8 2319 2002-06-09 2 8 2320 2002-06-09 3 15 2321 2002-06-09 3 15 2322 2002-06-09 3 15 2323 2002-06-09 3 15 2324 2002-06-09 3 15 2325 2002-06-09 3 15 2326 2002-06-09 3 15 2327 2002-06-09 4 22 2328 2002-06-09 4 22 2329 2002-06-09 4 22 2330 2002-06-09 4 22 2331 2002-06-09 4 22 2332 2002-06-09 4 22 2333 2002-06-09 4 22 2334 2002-06-09 5 29 2335 2002-06-09 5 29 2336 2002-06-09 5 29 2337 2002-06-09 5 29 2338 2002-06-09 5 29 2339 2002-06-09 5 29 2340 2002-06-09 5 29 2341 select * from (select date,id, dense_rank() over (partition by date order by id) as `p_rank` from row_number01,row_number02) t; 2342 date id p_rank 2343 2002-06-09 1 1 2344 2002-06-09 1 1 2345 2002-06-09 1 1 2346 2002-06-09 1 1 2347 2002-06-09 1 1 2348 2002-06-09 1 1 2349 2002-06-09 1 1 2350 2002-06-09 2 2 2351 2002-06-09 2 2 2352 2002-06-09 2 2 2353 2002-06-09 2 2 2354 2002-06-09 2 2 2355 2002-06-09 2 2 2356 2002-06-09 2 2 2357 2002-06-09 3 3 2358 2002-06-09 3 3 2359 2002-06-09 3 3 2360 2002-06-09 3 3 2361 2002-06-09 3 3 2362 2002-06-09 3 3 2363 2002-06-09 3 3 2364 2002-06-09 4 4 2365 2002-06-09 4 4 2366 2002-06-09 4 4 2367 2002-06-09 4 4 2368 2002-06-09 4 4 2369 2002-06-09 4 4 2370 2002-06-09 4 4 2371 2002-06-09 5 5 2372 2002-06-09 5 5 2373 2002-06-09 5 5 2374 2002-06-09 5 5 2375 2002-06-09 5 5 2376 2002-06-09 5 5 2377 2002-06-09 5 5 2378 select row_number01.*, rank() over (order by sex rows unbounded preceding), sum(id) over (order by sex,id rows unbounded preceding) from row_number01; 2379 id sex rank() over (order by sex rows unbounded preceding) sum(id) over (order by sex, id rows unbounded preceding) 2380 2 f 1 2 2381 3 f 1 5 2382 4 f 1 9 2383 1 m 4 10 2384 5 m 4 15 2385 select row_number01.*, dense_rank() over (order by sex rows unbounded preceding), sum(id) over (order by sex,id rows unbounded preceding) from row_number01; 2386 id sex dense_rank() over (order by sex rows unbounded preceding) sum(id) over (order by sex, id rows unbounded preceding) 2387 2 f 1 2 2388 3 f 1 5 2389 4 f 1 9 2390 1 m 2 10 2391 5 m 2 15 2392 select * from (select row_number01.*, sum(id) over (rows unbounded preceding), rank() over (order by sex rows unbounded preceding) from row_number01) alias order by id; 2393 id sex sum(id) over (rows unbounded preceding) rank() over (order by sex rows unbounded preceding) 2394 1 m 1 4 2395 2 f 3 1 2396 3 f 6 1 2397 4 f 10 1 2398 5 m 15 4 2399 select * from (select row_number01.*, sum(id) over (rows unbounded preceding), dense_rank() over (order by sex rows unbounded preceding) from row_number01) alias order by id; 2400 id sex sum(id) over (rows unbounded preceding) dense_rank() over (order by sex rows unbounded preceding) 2401 1 m 1 2 2402 2 f 3 1 2403 3 f 6 1 2404 4 f 10 1 2405 5 m 15 2 2406 select row_number01.*, sum(id) over (order by id rows unbounded preceding), 2407 rank() over (order by sex,id rows between 1 preceding and 2 following), 2408 row_number() over (order by sex,id rows unbounded preceding) 2409 from row_number01; 2410 id sex sum(id) over (order by id rows unbounded preceding) rank() over (order by sex, id rows between 1 preceding and 2 following) row_number() over (order by sex, id rows unbounded preceding) 2411 2 f 3 1 1 2412 3 f 6 2 2 2413 4 f 10 3 3 2414 1 m 1 4 4 2415 5 m 15 5 5 2416 select row_number01.*, sum(id) over (order by id rows unbounded preceding), 2417 dense_rank() over (order by sex,id rows between 1 preceding and 2 following) 2418 from row_number01; 2419 id sex sum(id) over (order by id rows unbounded preceding) dense_rank() over (order by sex, id rows between 1 preceding and 2 following) 2420 2 f 3 1 2421 3 f 6 2 2422 4 f 10 3 2423 1 m 1 4 2424 5 m 15 5 2425 select sum(id),avg(id) over (partition by sex), count(id) over (partition by sex) from row_number01; 2426 SQL syntax error: column "row_number01.id" must appear in the GROUP BY clause or be used in an aggregate function 2427 select * from (select id, sum(id) over (partition by sex), count(*) over (partition by sex), sex from row_number01 alias order by id) alias; 2428 id sum(id) over (partition by sex) count(*) over (partition by sex) sex 2429 1 6 2 m 2430 2 9 3 f 2431 3 9 3 f 2432 4 9 3 f 2433 5 6 2 m 2434 select sum(id) over (partition by sex) from row_number01; 2435 sum(id) over (partition by sex) 2436 9 2437 9 2438 9 2439 6 2440 6 2441 select id, sum(id) over (partition by sex order by id 2442 rows between 2 preceding and 1 following), sex from row_number01; 2443 id sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) sex 2444 2 5 f 2445 3 9 f 2446 4 9 f 2447 1 6 m 2448 5 6 m 2449 create view v as select id, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following), sex from row_number01; 2450 show create view v; 2451 View Create View character_set_client collation_connection 2452 v create view v as select id, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following), sex from row_number01; utf8mb4 utf8mb4_general_ci 2453 select * from v; 2454 id sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) sex 2455 2 5 f 2456 3 9 f 2457 4 9 f 2458 1 6 m 2459 5 6 m 2460 drop view v; 2461 drop table row_number01; 2462 drop table row_number02; 2463 drop table if exists wf01; 2464 create table wf01(d float); 2465 insert into wf01 values (10); 2466 insert into wf01 values (1); 2467 insert into wf01 values (2); 2468 insert into wf01 values (3); 2469 insert into wf01 values (4); 2470 insert into wf01 values (5); 2471 insert into wf01 values (6); 2472 insert into wf01 values (7); 2473 insert into wf01 values (8); 2474 insert into wf01 values (9); 2475 select * from wf01; 2476 d 2477 10.0 2478 1.0 2479 2.0 2480 3.0 2481 4.0 2482 5.0 2483 6.0 2484 7.0 2485 8.0 2486 9.0 2487 select d, sum(d) over (order by d range between 2 preceding and current row), 2488 avg(d) over (order by d range between 2 preceding and current row) from wf01; 2489 d sum(d) over (order by d range between 2 preceding and current row) avg(d) over (order by d range between 2 preceding and current row) 2490 1.0 1.0 1.0 2491 2.0 3.0 1.5 2492 3.0 6.0 2.0 2493 4.0 9.0 3.0 2494 5.0 12.0 4.0 2495 6.0 15.0 5.0 2496 7.0 18.0 6.0 2497 8.0 21.0 7.0 2498 9.0 24.0 8.0 2499 10.0 27.0 9.0 2500 select d, sum(d) over (order by d range between 1 preceding and 2 following), 2501 avg(d) over (order by d range between 2 preceding and 3 following) from wf01; 2502 d sum(d) over (order by d range between 1 preceding and 2 following) avg(d) over (order by d range between 2 preceding and 3 following) 2503 1.0 6.0 2.5 2504 2.0 10.0 3.0 2505 3.0 14.0 3.5 2506 4.0 18.0 4.5 2507 5.0 22.0 5.5 2508 6.0 26.0 6.5 2509 7.0 30.0 7.5 2510 8.0 34.0 8.0 2511 9.0 27.0 8.5 2512 10.0 19.0 9.0 2513 select d, sum(d) over (order by d range between 2 preceding and current row), 2514 avg(d) over (order by d range between 1 preceding and current row) from wf01; 2515 d sum(d) over (order by d range between 2 preceding and current row) avg(d) over (order by d range between 1 preceding and current row) 2516 1.0 1.0 1.0 2517 2.0 3.0 1.5 2518 3.0 6.0 2.5 2519 4.0 9.0 3.5 2520 5.0 12.0 4.5 2521 6.0 15.0 5.5 2522 7.0 18.0 6.5 2523 8.0 21.0 7.5 2524 9.0 24.0 8.5 2525 10.0 27.0 9.5 2526 select d, sum(d) over (order by d range between 2 preceding and 2 following), 2527 avg(d) over (order by d range between 1 preceding and 2 following) from wf01; 2528 d sum(d) over (order by d range between 2 preceding and 2 following) avg(d) over (order by d range between 1 preceding and 2 following) 2529 1.0 6.0 2.0 2530 2.0 10.0 2.5 2531 3.0 15.0 3.5 2532 4.0 20.0 4.5 2533 5.0 25.0 5.5 2534 6.0 30.0 6.5 2535 7.0 35.0 7.5 2536 8.0 40.0 8.5 2537 9.0 34.0 9.0 2538 10.0 27.0 9.5 2539 select d, sum(d) over (order by d range between current row and 0 following), 2540 avg(d) over (order by d range between current row and 2 following) from wf01; 2541 d sum(d) over (order by d range between current row and 0 following) avg(d) over (order by d range between current row and 2 following) 2542 1.0 1.0 2.0 2543 2.0 2.0 3.0 2544 3.0 3.0 4.0 2545 4.0 4.0 5.0 2546 5.0 5.0 6.0 2547 6.0 6.0 7.0 2548 7.0 7.0 8.0 2549 8.0 8.0 9.0 2550 9.0 9.0 9.5 2551 10.0 10.0 10.0 2552 select d, sum(d) over (order by d range between 2 preceding and 2 following), 2553 avg(d) over (order by d range between current row and 2 following) from wf01; 2554 d sum(d) over (order by d range between 2 preceding and 2 following) avg(d) over (order by d range between current row and 2 following) 2555 1.0 6.0 2.0 2556 2.0 10.0 3.0 2557 3.0 15.0 4.0 2558 4.0 20.0 5.0 2559 5.0 25.0 6.0 2560 6.0 30.0 7.0 2561 7.0 35.0 8.0 2562 8.0 40.0 9.0 2563 9.0 34.0 9.5 2564 10.0 27.0 10.0 2565 insert into wf01 select * from wf01; 2566 select * from wf01; 2567 d 2568 10.0 2569 1.0 2570 2.0 2571 3.0 2572 4.0 2573 5.0 2574 6.0 2575 7.0 2576 8.0 2577 9.0 2578 10.0 2579 1.0 2580 2.0 2581 3.0 2582 4.0 2583 5.0 2584 6.0 2585 7.0 2586 8.0 2587 9.0 2588 select d, sum(d) over (order by d range between 2 preceding and current row), 2589 avg(d) over (order by d range between 1 preceding and current row) from wf01; 2590 d sum(d) over (order by d range between 2 preceding and current row) avg(d) over (order by d range between 1 preceding and current row) 2591 1.0 2.0 1.0 2592 1.0 2.0 1.0 2593 2.0 6.0 1.5 2594 2.0 6.0 1.5 2595 3.0 12.0 2.5 2596 3.0 12.0 2.5 2597 4.0 18.0 3.5 2598 4.0 18.0 3.5 2599 5.0 24.0 4.5 2600 5.0 24.0 4.5 2601 6.0 30.0 5.5 2602 6.0 30.0 5.5 2603 7.0 36.0 6.5 2604 7.0 36.0 6.5 2605 8.0 42.0 7.5 2606 8.0 42.0 7.5 2607 9.0 48.0 8.5 2608 9.0 48.0 8.5 2609 10.0 54.0 9.5 2610 10.0 54.0 9.5 2611 select d, sum(d) over (order by d range between 2 preceding and 2 following), 2612 avg(d) over (order by d range between 3 preceding and 2 following) from wf01; 2613 d sum(d) over (order by d range between 2 preceding and 2 following) avg(d) over (order by d range between 3 preceding and 2 following) 2614 1.0 12.0 2.0 2615 1.0 12.0 2.0 2616 2.0 20.0 2.5 2617 2.0 20.0 2.5 2618 3.0 30.0 3.0 2619 3.0 30.0 3.0 2620 4.0 40.0 3.5 2621 4.0 40.0 3.5 2622 5.0 50.0 4.5 2623 5.0 50.0 4.5 2624 6.0 60.0 5.5 2625 6.0 60.0 5.5 2626 7.0 70.0 6.5 2627 7.0 70.0 6.5 2628 8.0 80.0 7.5 2629 8.0 80.0 7.5 2630 9.0 68.0 8.0 2631 9.0 68.0 8.0 2632 10.0 54.0 8.5 2633 10.0 54.0 8.5 2634 select d, sum(d) over (order by d range between 2 preceding and current row), 2635 avg(d) over (order by d range between 2 preceding and current row) from wf01; 2636 d sum(d) over (order by d range between 2 preceding and current row) avg(d) over (order by d range between 2 preceding and current row) 2637 1.0 2.0 1.0 2638 1.0 2.0 1.0 2639 2.0 6.0 1.5 2640 2.0 6.0 1.5 2641 3.0 12.0 2.0 2642 3.0 12.0 2.0 2643 4.0 18.0 3.0 2644 4.0 18.0 3.0 2645 5.0 24.0 4.0 2646 5.0 24.0 4.0 2647 6.0 30.0 5.0 2648 6.0 30.0 5.0 2649 7.0 36.0 6.0 2650 7.0 36.0 6.0 2651 8.0 42.0 7.0 2652 8.0 42.0 7.0 2653 9.0 48.0 8.0 2654 9.0 48.0 8.0 2655 10.0 54.0 9.0 2656 10.0 54.0 9.0 2657 select d, sum(d) over (order by d range between 1 preceding and 2 following), 2658 avg(d) over (order by d range between 2 preceding and 2 following) from wf01; 2659 d sum(d) over (order by d range between 1 preceding and 2 following) avg(d) over (order by d range between 2 preceding and 2 following) 2660 1.0 12.0 2.0 2661 1.0 12.0 2.0 2662 2.0 20.0 2.5 2663 2.0 20.0 2.5 2664 3.0 28.0 3.0 2665 3.0 28.0 3.0 2666 4.0 36.0 4.0 2667 4.0 36.0 4.0 2668 5.0 44.0 5.0 2669 5.0 44.0 5.0 2670 6.0 52.0 6.0 2671 6.0 52.0 6.0 2672 7.0 60.0 7.0 2673 7.0 60.0 7.0 2674 8.0 68.0 8.0 2675 8.0 68.0 8.0 2676 9.0 54.0 8.5 2677 9.0 54.0 8.5 2678 10.0 38.0 9.0 2679 10.0 38.0 9.0 2680 select d, sum(d) over (order by d range between current row and 2 following), 2681 avg(d) over (order by d range between current row and 2 following) from wf01; 2682 d sum(d) over (order by d range between current row and 2 following) avg(d) over (order by d range between current row and 2 following) 2683 1.0 12.0 2.0 2684 1.0 12.0 2.0 2685 2.0 18.0 3.0 2686 2.0 18.0 3.0 2687 3.0 24.0 4.0 2688 3.0 24.0 4.0 2689 4.0 30.0 5.0 2690 4.0 30.0 5.0 2691 5.0 36.0 6.0 2692 5.0 36.0 6.0 2693 6.0 42.0 7.0 2694 6.0 42.0 7.0 2695 7.0 48.0 8.0 2696 7.0 48.0 8.0 2697 8.0 54.0 9.0 2698 8.0 54.0 9.0 2699 9.0 38.0 9.5 2700 9.0 38.0 9.5 2701 10.0 20.0 10.0 2702 10.0 20.0 10.0 2703 select d, sum(d) over (order by d range between current row and 2 following), 2704 avg(d) over (order by d range between current row and 2 following) from wf01; 2705 d sum(d) over (order by d range between current row and 2 following) avg(d) over (order by d range between current row and 2 following) 2706 1.0 12.0 2.0 2707 1.0 12.0 2.0 2708 2.0 18.0 3.0 2709 2.0 18.0 3.0 2710 3.0 24.0 4.0 2711 3.0 24.0 4.0 2712 4.0 30.0 5.0 2713 4.0 30.0 5.0 2714 5.0 36.0 6.0 2715 5.0 36.0 6.0 2716 6.0 42.0 7.0 2717 6.0 42.0 7.0 2718 7.0 48.0 8.0 2719 7.0 48.0 8.0 2720 8.0 54.0 9.0 2721 8.0 54.0 9.0 2722 9.0 38.0 9.5 2723 9.0 38.0 9.5 2724 10.0 20.0 10.0 2725 10.0 20.0 10.0 2726 drop table wf01; 2727 drop table if exists wf02; 2728 create table wf02 (id integer, sex varchar(10)); 2729 insert into wf02 values (1, 'moolol'); 2730 insert into wf02 values (2, 'fdhsajhd'); 2731 insert into wf02 values (3, 'fdhsajhd'); 2732 insert into wf02 values (4, 'fdhsajhd'); 2733 insert into wf02 values (5, 'moolol'); 2734 insert into wf02 values (10, null); 2735 insert into wf02 values (11, null); 2736 select * from wf02; 2737 id sex 2738 1 moolol 2739 2 fdhsajhd 2740 3 fdhsajhd 2741 4 fdhsajhd 2742 5 moolol 2743 10 null 2744 11 null 2745 select row_number() over (partition by sex order by id rows between unbounded preceding and unbounded following), id, 2746 sum(id) over (partition by sex order by id rows between 1 following and 2 following), sex from wf02; 2747 row_number() over (partition by sex order by id rows between unbounded preceding and unbounded following) id sum(id) over (partition by sex order by id rows between 1 following and 2 following) sex 2748 1 10 11 null 2749 2 11 null null 2750 1 2 7 fdhsajhd 2751 2 3 4 fdhsajhd 2752 3 4 null fdhsajhd 2753 1 1 5 moolol 2754 2 5 null moolol 2755 select row_number() over (partition by sex order by id rows between 1 following and 2 following), sum(id) over (partition by sex order by id 2756 rows between 1 following and 2 following) from wf02; 2757 row_number() over (partition by sex order by id rows between 1 following and 2 following) sum(id) over (partition by sex order by id rows between 1 following and 2 following) 2758 1 11 2759 2 null 2760 1 7 2761 2 4 2762 3 null 2763 1 5 2764 2 null 2765 insert into wf02 values (10, null); 2766 select rank() over (partition by sex order by id), id, sum(id) over (partition by sex order by id) as abc, sex from wf02; 2767 rank() over (partition by sex order by id) id abc sex 2768 1 10 20 null 2769 1 10 20 null 2770 3 11 31 null 2771 1 2 2 fdhsajhd 2772 2 3 5 fdhsajhd 2773 3 4 9 fdhsajhd 2774 1 1 1 moolol 2775 2 5 6 moolol 2776 select id, sex, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) as a from wf02; 2777 id sex a 2778 10 null 20 2779 10 null 31 2780 11 null 31 2781 2 fdhsajhd 5 2782 3 fdhsajhd 9 2783 4 fdhsajhd 9 2784 1 moolol 6 2785 5 moolol 6 2786 select id, sex, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) as a, 2787 row_number() over (partition by sex order by id rows between 2 preceding and 1 following) as b, 2788 rank() over (partition by sex order by id rows between 2 preceding and 1 following) as c from wf02; 2789 id sex a b c 2790 10 null 20 1 1 2791 10 null 31 2 1 2792 11 null 31 3 3 2793 2 fdhsajhd 5 1 1 2794 3 fdhsajhd 9 2 2 2795 4 fdhsajhd 9 3 3 2796 1 moolol 6 1 1 2797 5 moolol 6 2 2 2798 select id, sex, sum(id) over (partition by sex order by id rows between 2 preceding and 1 following) as a, 2799 row_number() over (partition by sex order by id rows between 2 preceding and 1 following) as b from wf02; 2800 id sex a b 2801 10 null 20 1 2802 10 null 31 2 2803 11 null 31 3 2804 2 fdhsajhd 5 1 2805 3 fdhsajhd 9 2 2806 4 fdhsajhd 9 3 2807 1 moolol 6 1 2808 5 moolol 6 2 2809 select row_number() over (partition by sex order by id rows between unbounded preceding and unbounded following), id, 2810 sex from wf02; 2811 row_number() over (partition by sex order by id rows between unbounded preceding and unbounded following) id sex 2812 1 10 null 2813 2 10 null 2814 3 11 null 2815 1 2 fdhsajhd 2816 2 3 fdhsajhd 2817 3 4 fdhsajhd 2818 1 1 moolol 2819 2 5 moolol 2820 select row_number() over (partition by sex order by id rows between 1 preceding and 2 following), sum(id) over (partition by sex order by id 2821 rows between 1 preceding and 2 following) from wf02; 2822 row_number() over (partition by sex order by id rows between 1 preceding and 2 following) sum(id) over (partition by sex order by id rows between 1 preceding and 2 following) 2823 1 31 2824 2 31 2825 3 21 2826 1 9 2827 2 9 2828 3 7 2829 1 6 2830 2 6 2831 drop table wf02; 2832 drop table if exists cte01; 2833 drop table if exists cte02; 2834 create table cte01 ( 2835 customer_id varchar(1), 2836 order_date date, 2837 product_id integer 2838 ); 2839 insert into cte01 values('a', '2021-01-01', '1'); 2840 insert into cte01 values('a', '2021-01-01', '2'); 2841 insert into cte01 values('a', '2021-01-07', '2'); 2842 insert into cte01 values('a', '2021-01-10', '3'); 2843 insert into cte01 values('a', '2021-01-11', '3'); 2844 insert into cte01 values('a', '2021-01-11', '3'); 2845 insert into cte01 values('b', '2021-01-01', '2'); 2846 insert into cte01 values('b', '2021-01-02', '2'); 2847 insert into cte01 values('b', '2021-01-04', '1'); 2848 insert into cte01 values('b', '2021-01-11', '1'); 2849 insert into cte01 values('b', '2021-01-16', '3'); 2850 insert into cte01 values('b', '2021-02-01', '3'); 2851 insert into cte01 values('c', '2021-01-01', '3'); 2852 insert into cte01 values('c', '2021-01-01', '3'); 2853 insert into cte01 values('c', '2021-01-07', '3'); 2854 select * from cte01; 2855 customer_id order_date product_id 2856 a 2021-01-01 1 2857 a 2021-01-01 2 2858 a 2021-01-07 2 2859 a 2021-01-10 3 2860 a 2021-01-11 3 2861 a 2021-01-11 3 2862 b 2021-01-01 2 2863 b 2021-01-02 2 2864 b 2021-01-04 1 2865 b 2021-01-11 1 2866 b 2021-01-16 3 2867 b 2021-02-01 3 2868 c 2021-01-01 3 2869 c 2021-01-01 3 2870 c 2021-01-07 3 2871 create table cte02 ( 2872 product_id integer, 2873 product_name varchar(5), 2874 price integer 2875 ); 2876 insert into cte02 values('1', 'sushi', '10'); 2877 insert into cte02 values('2', 'curry', '15'); 2878 insert into cte02 values('3', 'ramen', '12'); 2879 select * from cte02; 2880 product_id product_name price 2881 1 sushi 10 2882 2 curry 15 2883 3 ramen 12 2884 with test as ( 2885 select cte01.customer_id, cte01.order_date, cte02.product_name, dense_rank() over (partition by cte01.customer_id 2886 order by cte01.order_date) as `rank` from cte01 inner join cte02 on cte01.product_id = cte02.product_id) 2887 select customer_id, product_name from test where `rank` = 1 group by customer_id, product_name; 2888 customer_id product_name 2889 a sushi 2890 a curry 2891 b curry 2892 c ramen 2893 with test as ( 2894 select cte01.customer_id, cte01.order_date, cte02.product_name, rank() over (partition by cte01.customer_id 2895 order by cte01.order_date) as `rank` from cte01 inner join cte02 on cte01.product_id = cte02.product_id) 2896 select customer_id, product_name from test where `rank` = 1 group by customer_id, product_name; 2897 customer_id product_name 2898 a sushi 2899 a curry 2900 b curry 2901 c ramen 2902 with test as ( 2903 select cte01.customer_id, cte01.order_date, cte02.product_name, row_number() over (partition by cte01.customer_id 2904 order by cte01.order_date) as `rank` from cte01 inner join cte02 on cte01.product_id = cte02.product_id) 2905 select customer_id, product_name from test where `rank` = 1 group by customer_id, product_name; 2906 customer_id product_name 2907 a sushi 2908 b curry 2909 c ramen 2910 drop table cte01; 2911 drop table cte02; 2912 drop table if exists td; 2913 create table td(d int); 2914 insert into td(d) values (10),(1),(2),(3),(4),(5),(6),(7),(8),(9); 2915 insert into td(d) select d+10 from td; 2916 insert into td(d) select d+20 from td; 2917 insert into td(d) select d+30 from td; 2918 insert into td(d) select d+40 from td; 2919 insert into td(d) select d+50 from td; 2920 insert into td(d) select d+60 from td; 2921 insert into td(d) select d+70 from td; 2922 insert into td(d) select d+80 from td; 2923 insert into td(d) select d+90 from td; 2924 insert into td(d) select d+100 from td; 2925 insert into td(d) select d+200 from td; 2926 insert into td(d) select d+300 from td; 2927 insert into td(d) select d+400 from td; 2928 insert into td(d) select d+500 from td; 2929 insert into td(d) select d+600 from td; 2930 insert into td(d) select d+700 from td; 2931 insert into td(d) select d+800 from td; 2932 insert into td(d) select d+900 from td; 2933 insert into td(d) select d+1000 from td; 2934 insert into td(d) select d+2000 from td; 2935 insert into td(d) select d+3000 from td; 2936 select count(*) from td; 2937 count(*) 2938 20971520 2939 select avg(d) over (order by d range between 2 preceding and 2 following) from td limit 10; 2940 [unknown result because it is related to issue#13008] 2941 select sum(d) over (order by d rows between 10 preceding and 10 following) from td limit 10; 2942 sum(d) over (order by d rows between 10 preceding and 10 following) 2943 66 2944 78 2945 91 2946 105 2947 120 2948 136 2949 153 2950 171 2951 190 2952 210 2953 select d,min(d) over (partition by d%7 order by d rows between 2 preceding and 1 following) from td limit 10; 2954 d min(d) over (partition by d % 7 order by d rows between 2 preceding and 1 following) 2955 7 7 2956 14 7 2957 21 7 2958 28 14 2959 35 21 2960 35 28 2961 42 35 2962 42 35 2963 49 42 2964 49 42 2965 drop table td; 2966 drop table if exists `c`; 2967 create table `c` ( 2968 `pk` int(11) not null auto_increment, 2969 `col_int` int(11) not null, 2970 `col_date` date not null, 2971 `col_datetime` datetime not null, 2972 `col_time` time not null, 2973 `col_varchar` varchar(15) not null, 2974 primary key (`pk`), 2975 unique key `col_date_key` (`col_date`), 2976 unique key `col_date_key_2` (`col_date`,`col_datetime`), 2977 key `col_int_key_1` (`col_int`,`col_date`), 2978 key `col_int_key_2` (`col_int`,`col_time`), 2979 key `col_int_key_3` (`col_int`,`col_datetime`) 2980 ); 2981 insert into `c` (`pk`, `col_int`, `col_date`, `col_datetime`, `col_time`, `col_varchar`) 2982 values (1, 9, '2009-11-04', '2006-10-12 19:52:02', '18:19:40', 'a'), 2983 (2, 4, '2009-05-21', '2005-09-13 00:00:00', '07:45:25', 'tef'), 2984 (3, 0, '1900-01-01', '2002-09-03 04:42:41', '13:17:14', 'efqsd'), 2985 (4, 149, '2000-11-05', '2007-02-08 07:29:31', '10:38:21', 'fqsdk'), 2986 (5, 8, '2001-06-12', '2000-11-07 15:28:31', '23:04:47', 'qsdksji'), 2987 (6, 8, '2002-06-07', '2007-09-19 02:35:12', '07:33:31', 'sdks'), 2988 (7, 5, '2008-06-02', '1900-01-01 00:00:00', '14:41:02', 'dksjij'), 2989 (8, 7, '2000-07-26', '2007-11-27 00:19:33', '23:30:25', 'sjijcsz'), 2990 (9, 8, '2008-09-16', '2004-12-17 11:22:46', '06:11:14', 'i'), 2991 (10, 104, '2002-03-06', '2007-02-04 13:09:16', '22:24:50', 'jcszxw'), 2992 (11, 1, '2004-01-10', '2008-03-19 08:36:41', '00:03:00', 'csz'), 2993 (12, 4, '2002-02-21', '2008-03-27 03:09:30', '06:52:39', 'szxwbjj'), 2994 (13, 8, '2004-07-01', '2001-10-20 06:42:39', '08:49:41', 'xwb'), 2995 (14, 7, '2008-08-13', '2002-04-05 00:00:00', '05:52:03', 'wbjjvvk'), 2996 (15, 8, '2008-12-18', '1900-01-01 00:00:00', '00:00:00', 'bj'), 2997 (16, 6, '2002-08-03', '2008-04-14 09:20:36', '00:00:00', 'jjvvk'), 2998 (17, 97, '2001-06-11', '2002-11-07 00:00:00', '13:30:55', 'j'); 2999 drop table if exists `dd`; 3000 create table `dd` ( 3001 `pk` int(11) not null auto_increment, 3002 `col_int` int(11) not null, 3003 `col_date` date not null, 3004 `col_datetime` datetime not null, 3005 `col_time` time not null, 3006 `col_varchar` varchar(15) not null, 3007 primary key (`pk`), 3008 unique key `col_date_key` (`col_date`), 3009 unique key `col_date_key_1` (`col_date`,`col_time`,`col_datetime`), 3010 key `col_int_key` (`col_int`), 3011 key `col_time_key` (`col_time`), 3012 key `col_datetime_key` (`col_datetime`), 3013 key `col_int_key_5` (`col_int`), 3014 key `col_int_key_6` (`col_int`), 3015 key `col_int_key_7` (`col_int`,`col_date`), 3016 key `col_int_key_8` (`col_int`,`col_time`), 3017 key `col_int_key_9` (`col_int`,`col_datetime`)); 3018 insert into `dd` (`pk`, `col_int`, `col_date`, `col_datetime`, `col_time`, `col_varchar`) 3019 values (10,7,'1992-01-01','2000-02-09 06:46:23','03:56:10','i'), 3020 (11,5,'2008-12-11','2004-03-07 18:05:11','00:00:00','jrll'), 3021 (12,7,'2005-11-18','2001-01-18 08:29:29','20:17:57','rllqunt'), 3022 (13,9,'2009-02-08','2005-10-25 00:00:00','08:09:49','l'), 3023 (14,3,'2002-05-26','2009-09-01 10:19:05','09:40:42','lq'), 3024 (15,66,'2002-03-10','2002-09-06 04:43:02','08:28:55','quntp'), 3025 (16,3,'2003-07-07','2006-04-07 00:00:00','20:12:00','untppi'), 3026 (17,95,'2006-06-22','2004-05-08 00:00:00','18:50:24','ntppirz'), 3027 (18,7,'2004-01-21','2000-01-23 03:34:04','17:01:57','tppirzd'), 3028 (19,5,'2001-05-01','2005-12-26 20:42:01','15:11:27','pirzdp'), 3029 (20,8,'2008-12-15','1900-01-01 00:00:00','05:49:51','irzd'), 3030 (21,3,'2000-08-28','2003-02-28 16:30:52','14:58:44','zdphpdu'), 3031 (22,96,'2008-06-08','2005-09-15 03:55:22','02:20:01','dp'), 3032 (23,9,'2002-04-02','2001-01-08 10:44:10','19:03:57','p'), 3033 (24,3,'2005-03-04','2001-03-23 00:00:00','00:27:13','h'), 3034 (25,8,'2001-01-21','2004-03-02 00:00:00','13:39:32','pduhwq'), 3035 (26,8,'2006-10-05','1900-01-01 00:00:00','08:06:08','uhwqh'), 3036 (27,4,'2001-12-26','2006-10-24 05:59:20','16:15:34','hwqh'), 3037 (28,7,'1900-01-01','2005-06-14 00:00:00','12:04:50','wqhnsm'), 3038 (29,6,'2007-12-02','2001-08-25 03:00:31','00:00:00','qh'), 3039 (30,4,'2009-02-06','2001-06-14 19:13:14','06:00:42','nsmu'), 3040 (31,9,'2007-01-15','2006-12-18 07:54:16','11:18:35','smujjj'), 3041 (32,5,'2004-11-07','2000-09-18 04:53:37','16:20:06','muj'), 3042 (33,1,'2003-12-07','2002-08-18 04:47:11','01:41:35','jj'), 3043 (34,1,'2008-09-07','2000-10-14 16:58:18','17:42:13','jbld'), 3044 (35,5,'2005-03-08','2008-11-22 16:40:01','00:59:59','bldnki'), 3045 (36,181,'2006-11-18','1900-01-01 00:00:00','00:00:00','nkiws'), 3046 (37,5,'2007-01-26','2008-01-21 00:00:00','02:16:04','kiwsr'), 3047 (38,1,'2003-08-24','1900-01-01 00:00:00','00:00:00','iwsrsx'), 3048 (39,162,'2001-12-01','2008-05-17 00:00:00','14:34:36','srsxnd'), 3049 (40,8,'2003-07-02','2000-06-07 00:00:00','23:02:05','r'), 3050 (41,2,'2007-03-01','2009-01-03 12:22:04','00:00:00','sxndo'), 3051 (42,7,'2009-08-04','2009-10-05 04:15:15','00:00:00','xndolp'), 3052 (43,119,'2000-05-03','2002-02-17 23:12:12','23:23:35','olpujd'), 3053 (44,3,'2001-05-18','2008-03-27 11:51:54','11:26:20','lp'), 3054 (45,119,'2004-02-22','1900-01-01 00:00:00','00:00:00','pu'), 3055 (46,8,'2002-07-15','2008-08-24 21:36:28','12:51:37','dnozrhh'), 3056 (47,2,'2008-04-22','2005-01-12 08:50:22','20:55:45','no'), 3057 (48,4,'2006-06-01','2000-04-20 00:00:00','13:02:05','ozrhhcx'), 3058 (49,8,'2009-09-12','2000-02-16 03:57:05','17:04:35','zrhhcxs'), 3059 (50,6,'2009-01-06','1900-01-01 00:00:00','05:15:45','rhhcxsx'), 3060 (51,6,'2008-07-13','2002-04-27 14:13:27','00:00:00','hhcxsxw'), 3061 (52,8,'2002-03-15','2008-01-17 20:30:57','07:09:22','hcxsxw'), 3062 (53,6,'2007-10-14','2006-10-11 22:48:02','06:11:59','cxs'), 3063 (54,1,'2008-07-23','2005-09-11 07:19:40','03:05:06','x'), 3064 (55,1,'2007-05-22','2002-11-24 16:25:27','10:10:42','s'), 3065 (56,6,'2008-01-08','2005-06-09 01:11:17','06:03:27','w'), 3066 (57,9,'2006-10-18','1900-01-01 00:00:00','00:00:00','uju'), 3067 (58,7,'2000-07-22','1900-01-01 00:00:00','00:00:00','ju'), 3068 (59,6,'2004-07-21','2009-10-25 16:05:29','11:04:39','ul'), 3069 (60,2,'2001-10-03','2002-06-13 11:41:55','10:20:49','lpjd'), 3070 (61,8,'2002-08-17','1900-01-01 00:00:00','00:00:00','jdz'), 3071 (62,0,'2009-11-10','2000-05-04 05:15:19','00:00:00','zvkpaij'), 3072 (63,6,'2005-06-26','2002-08-19 00:00:00','09:21:09','vkpaij'), 3073 (64,6,'2000-06-04','2002-03-22 04:37:00','00:00:00','kp'), 3074 (65,9,'2005-10-02','2009-01-10 09:03:59','04:56:37','paiju'), 3075 (66,0,'2009-11-13','1900-01-01 00:00:00','00:00:00','aij'), 3076 (67,0,'2006-11-26','2001-09-21 00:00:00','08:16:28','ijurspr'), 3077 (68,6,'2007-09-24','2003-08-27 05:11:09','19:55:11','j'), 3078 (69,0,'2009-01-24','1900-01-01 00:00:00','11:25:58','urspr'), 3079 (70,5,'2001-06-22','2005-07-07 00:00:00','14:38:03','rsprn'), 3080 (71,4,'2006-07-18','2000-07-16 06:17:20','15:32:00','sprnw'), 3081 (72,5,'2009-05-12','2007-07-26 00:00:00','09:25:59','rnwgrp'); 3082 drop table if exists `e`; 3083 create table `e` ( 3084 `pk` int(11) not null auto_increment, 3085 `col_int` int(11) not null, 3086 `col_date` date not null, 3087 `col_datetime` datetime not null, 3088 `col_time` time not null, 3089 `col_varchar` varchar(15) not null, 3090 primary key (`pk`), 3091 unique key `col_date` (`col_date`,`col_time`,`col_datetime`), 3092 unique key `col_varchar_key_2` (`col_varchar`(5)), 3093 unique key `col_int_key_1` (`col_int`,`col_varchar`(5)), 3094 unique key `col_int_key_2` (`col_int`,`col_varchar`(5),`col_date`,`col_time`,`col_datetime`), 3095 key `col_int_key` (`col_int`), 3096 key `col_time_key` (`col_time`), 3097 key `col_datetime_key` (`col_datetime`), 3098 key `col_int_key_7` (`col_int`,`col_date`), 3099 key `col_int_key_8` (`col_int`,`col_time`), 3100 key `col_int_key_9` (`col_int`,`col_datetime`)); 3101 insert into `e` (`pk`, `col_int`, `col_date`, `col_datetime`, `col_time`, `col_varchar`) 3102 values (1, 202, '1997-01-13', '2008-11-25 09:14:26', '07:23:12', 'en'), 3103 (2, 4, '2005-07-10', '2005-03-15 22:48:25', '23:28:02', 'nchyhu'), 3104 (3, 7, '2005-06-09', '2006-11-22 00:00:00', '10:51:23', 'chy'), 3105 (4, 2, '2007-12-08', '2007-11-01 09:02:50', '01:12:13', 'hyhu'), 3106 (5, 7, '2007-12-22', '2001-04-08 00:00:00', '06:34:46', 'yhuoo'), 3107 (6, 1, '1900-01-01', '2001-11-27 19:47:15', '10:16:53', 'huoo'), 3108 (7, 7, '2002-10-07', '2009-09-15 04:42:26', '07:07:58', 'uoowit'), 3109 (8, 7, '2005-01-09', '2001-08-12 02:07:43', '06:15:07', 'oo'), 3110 (9, 3, '2007-10-12', '2009-05-09 17:06:27', '00:00:00', 'ow'), 3111 (10, 3, '2004-01-22', '1900-01-01 00:00:00', '06:41:21', 'wityzg'), 3112 (11, 5, '2007-10-11', '2000-03-03 23:40:04', '22:28:00', 'ityzg'), 3113 (12, 8, '2001-08-19', '2005-10-18 17:41:54', '04:47:49', 'tyz'), 3114 (13, 9, '2001-02-12', '2000-03-23 23:22:54', '03:24:01', 'gktbkjr'), 3115 (14, 0, '2000-07-14', '2007-01-25 11:00:51', '14:37:06', 'ktbkjrk'), 3116 (15, 4, '2007-11-14', '2003-12-21 10:46:23', '05:53:49', 'tbkjrkm'), 3117 (16, 9, '2004-01-25', '2003-09-02 01:45:27', '00:00:00', 'k'), 3118 (17, 2, '2003-12-15', '2009-05-28 08:03:38', '23:41:09', 'j'), 3119 (18, 4, '2002-01-25', '2003-10-23 18:22:15', '09:26:45', 'kmqm'), 3120 (19, 0, '2009-09-08', '2001-12-28 00:00:00', '17:04:03', 'mq'), 3121 (20, 7, '2008-03-15', '2005-05-06 19:42:18', '02:15:17', 'mkn'), 3122 (21, 0, '2005-11-10', '2003-03-05 00:00:00', '00:00:00', 'knbtoe'), 3123 (22, 1, '2008-11-12', '2001-12-26 16:47:05', '19:09:36', 'n'), 3124 (23, 2, '2007-11-22', '2003-02-09 00:00:00', '07:55:11', 'btoer'), 3125 (24, 4, '2002-04-25', '2008-10-13 00:00:00', '11:24:50', 'toe'), 3126 (25, 4, '2004-02-14', '2001-07-16 16:05:48', '08:46:01', 'oervq'), 3127 (26, 4, '2004-04-21', '2004-04-23 14:00:22', '20:16:19', 'rvqlzs'), 3128 (27, 3, '2003-03-26', '2002-11-10 08:15:17', '13:03:14', 'vqlzs'), 3129 (28, 0, '2007-06-18', '2006-06-24 03:59:58', '06:11:33', 'qlzsva'), 3130 (29, 5, '2006-12-09', '2008-04-08 18:06:18', '09:40:31', 'lzsvasu'), 3131 (30, 8, '2001-10-01', '2000-10-12 16:32:35', '03:34:01', 'zsvasu'), 3132 (31, 6, '2001-01-07', '2005-09-11 10:09:54', '00:00:00', 'svas'), 3133 (32, 0, '2007-11-02', '2009-09-10 01:44:18', '12:23:27', 'v'), 3134 (33, 9, '2005-07-23', '2002-10-20 21:55:02', '05:12:10', 'surqdhu'), 3135 (34, 4, '2003-09-13', '2009-11-03 09:54:42', '20:54:06', 'urqdh'), 3136 (35, 165, '2001-05-14', '2002-10-19 00:00:00', '00:00:00', 'rqd'), 3137 (36, 2, '2006-07-04', '2008-10-26 00:00:00', '00:59:06', 'qdhu'), 3138 (37, 6, '2001-08-15', '2002-08-14 14:52:08', '07:22:34', 'dhu'), 3139 (38, 5, '2000-04-27', '2007-06-10 00:00:00', '11:27:19', 'hu4332cjx'), 3140 (39, 9, '2007-10-13', '2002-07-07 04:10:43', '10:03:09', 'uc'), 3141 (40, 214, '2004-02-06', '2007-08-15 13:56:29', '23:00:35', 'cjxd'), 3142 (41, 194, '2008-12-27', '1900-01-01 00:00:00', '11:59:05', 'jx'), 3143 (42, 1, '2002-08-16', '2000-08-11 11:34:38', '21:39:43', 'xdo'), 3144 (43, 220, '2001-06-17', '1900-01-01 00:00:00', '00:00:00', 'oyg'), 3145 (44, 9, '2002-10-16', '2008-12-07 23:41:33', '00:00:00', 'gx'), 3146 (45, 248, '2008-04-06', '1900-01-01 00:00:00', '12:32:24', 'x'), 3147 (46, 0, '2000-07-08', '2001-12-27 19:38:22', '00:00:00', 'vgqmw'), 3148 (47, 0, '2005-03-16', '1900-01-01 00:00:00', '06:22:01', 'qmwcid'), 3149 (48, 4, '2002-06-19', '2007-03-08 02:43:50', '07:00:21', 'mwc'), 3150 (49, 3, '2005-11-25', '2001-11-14 17:21:32', '17:59:20', 'wcidtu'), 3151 (50, 7, '2007-07-08', '1900-01-01 00:00:00', '01:58:05', 'cidtum'), 3152 (51, 7, '2000-06-20', '2004-07-20 11:05:12', '22:24:24', 'dtumxwc'), 3153 (52, 5, '2006-03-28', '2008-08-15 08:28:18', '04:22:26', 'tumxwc'), 3154 (53, 1, '2004-03-05', '1900-01-01 00:00:00', '00:00:00', 'umxwcf'), 3155 (54, 0, '2009-05-10', '2004-01-28 15:16:19', '11:46:32', 'mxwcft'), 3156 (55, 67, '2004-04-18', '2001-06-23 00:00:00', '20:12:09', 'xwcfted'), 3157 (56, 204, '2008-01-10', '2009-02-12 07:59:52', '13:58:17', 'wc'), 3158 (57, 9, '2000-07-12', '2004-12-10 07:32:31', '04:04:48', 'ftedx'), 3159 (58, 5, '2001-06-16', '2006-09-06 12:15:44', '10:14:16', 't'), 3160 (59, 6, '2000-02-20', '2003-09-13 14:23:06', '21:22:20', 'dx'), 3161 (60, 6, '2001-02-07', '2004-01-18 00:00:00', '10:15:21', 'xqyciak'), 3162 (61, 1, '2008-12-24', '2004-04-02 07:16:01', '16:30:10', 'qy'), 3163 (62, 1, '2009-12-14', '2000-01-04 14:51:24', '03:57:54', 'y'), 3164 (63, 5, '2008-03-07', '2001-06-24 00:00:00', '06:41:05', 'ciak'), 3165 (64, 4, '2005-01-19', '2001-06-02 03:41:12', '00:00:00', 'iakh'), 3166 (65, 4, '2003-02-10', '1900-01-01 00:00:00', '08:51:25', 'ak'), 3167 (66, 9, '2005-12-25', '2007-07-13 14:26:05', '14:32:55', 'hxptz'), 3168 (67, 4, '2003-10-13', '2008-03-20 21:14:50', '00:21:31', 'xptzfp'), 3169 (68, 3, '2001-08-03', '1900-01-01 00:00:00', '00:00:00', 'ptzfpjw'), 3170 (69, 0, '2006-04-01', '1900-01-01 00:00:00', '11:26:05', 'tzfpjwr'), 3171 (70, 2, '2003-12-27', '2002-05-09 18:39:28', '05:28:11', 'wrgeo'), 3172 (71, 100, '2001-10-25', '2006-01-13 00:00:00', '04:35:51', 'r'), 3173 (72, 37, '2006-09-12', '2003-12-04 05:20:00', '06:10:43', 'geo'), 3174 (73, 5, '2003-06-04', '2003-07-21 11:43:03', '17:26:47', 'eozxnby'), 3175 (74, 6, '2009-11-13', '2006-12-24 00:00:00', '22:34:54', 'oz'), 3176 (75, 1, '2006-08-13', '2005-08-25 00:00:00', '21:27:38', 'zxnbyc'), 3177 (76, 7, '2007-07-09', '2003-10-16 01:16:30', '03:14:14', 'xnbycjz'), 3178 (77, 6, '2000-01-07', '2001-06-22 00:00:00', '00:00:00', 'nby'), 3179 (78, 5, '2004-12-21', '2004-09-01 18:53:04', '16:06:30', 'bycj'), 3180 (79, 0, '2003-10-14', '2000-04-13 05:21:03', '19:04:51', 'ycjzxie'); 3181 with test01 as ( 3182 select `e`.col_int, `c`.col_varchar, row_number() over (partition by `e`.col_int 3183 order by `e`.col_date) as `rank` from `e` inner join `c` on `c`.col_int = `e`.col_int) 3184 select col_int as a from test where `rank` = 1 group by col_int; 3185 SQL parser error: table "test" does not exist 3186 with test02 as ( 3187 select `dd`.col_int, `c`.col_datetime, rank() over (partition by `dd`.col_int 3188 order by `dd`.col_date) as `rank` from `dd` left join `c` on `c`.col_int = `dd`.col_int) 3189 select col_int as a from test02 where `rank` = 1 group by col_int; 3190 a 3191 0 3192 1 3193 2 3194 3 3195 4 3196 5 3197 6 3198 7 3199 8 3200 9 3201 66 3202 95 3203 96 3204 119 3205 162 3206 181 3207 with test03 as ( 3208 select `dd`.col_int, `e`.col_varchar, dense_rank() over (partition by `dd`.col_int 3209 order by `dd`.col_datetime) as `rank` from `dd` left join `e` on `e`.col_int = `dd`.col_int) 3210 select col_int as a from test03 where `rank` = 1 group by col_int; 3211 a 3212 0 3213 1 3214 2 3215 3 3216 4 3217 5 3218 6 3219 7 3220 8 3221 9 3222 66 3223 95 3224 96 3225 119 3226 162 3227 181 3228 select `c`.col_int,`c`.col_datetime, `dd`.col_time, row_number() over (partition by `c`.col_int 3229 order by `dd`.col_time) as `rank` from `dd` left join `c` on `c`.col_int = `dd`.col_int; 3230 col_int col_datetime col_time rank 3231 null null 00:00:00 1 3232 null null 00:00:00 2 3233 null null 00:00:00 3 3234 null null 00:27:13 4 3235 null null 02:20:01 5 3236 null null 08:28:55 6 3237 null null 09:40:42 7 3238 null null 10:20:49 8 3239 null null 11:26:20 9 3240 null null 14:34:36 10 3241 null null 14:58:44 11 3242 null null 18:50:24 12 3243 null null 20:12:00 13 3244 null null 20:55:45 14 3245 null null 23:23:35 15 3246 0 2002-09-03 04:42:41 00:00:00 1 3247 0 2002-09-03 04:42:41 00:00:00 2 3248 0 2002-09-03 04:42:41 08:16:28 3 3249 0 2002-09-03 04:42:41 11:25:58 4 3250 1 2008-03-19 08:36:41 00:00:00 1 3251 1 2008-03-19 08:36:41 01:41:35 2 3252 1 2008-03-19 08:36:41 03:05:06 3 3253 1 2008-03-19 08:36:41 10:10:42 4 3254 1 2008-03-19 08:36:41 17:42:13 5 3255 4 2008-03-27 03:09:30 06:00:42 1 3256 4 2005-09-13 00:00:00 06:00:42 2 3257 4 2005-09-13 00:00:00 13:02:05 3 3258 4 2008-03-27 03:09:30 13:02:05 4 3259 4 2008-03-27 03:09:30 15:32:00 5 3260 4 2005-09-13 00:00:00 15:32:00 6 3261 4 2005-09-13 00:00:00 16:15:34 7 3262 4 2008-03-27 03:09:30 16:15:34 8 3263 5 1900-01-01 00:00:00 00:00:00 1 3264 5 1900-01-01 00:00:00 00:59:59 2 3265 5 1900-01-01 00:00:00 02:16:04 3 3266 5 1900-01-01 00:00:00 09:25:59 4 3267 5 1900-01-01 00:00:00 14:38:03 5 3268 5 1900-01-01 00:00:00 15:11:27 6 3269 5 1900-01-01 00:00:00 16:20:06 7 3270 6 2008-04-14 09:20:36 00:00:00 1 3271 6 2008-04-14 09:20:36 00:00:00 2 3272 6 2008-04-14 09:20:36 00:00:00 3 3273 6 2008-04-14 09:20:36 05:15:45 4 3274 6 2008-04-14 09:20:36 06:03:27 5 3275 6 2008-04-14 09:20:36 06:11:59 6 3276 6 2008-04-14 09:20:36 09:21:09 7 3277 6 2008-04-14 09:20:36 11:04:39 8 3278 6 2008-04-14 09:20:36 19:55:11 9 3279 7 2007-11-27 00:19:33 00:00:00 1 3280 7 2007-11-27 00:19:33 00:00:00 2 3281 7 2002-04-05 00:00:00 00:00:00 3 3282 7 2002-04-05 00:00:00 00:00:00 4 3283 7 2007-11-27 00:19:33 03:56:10 5 3284 7 2002-04-05 00:00:00 03:56:10 6 3285 7 2007-11-27 00:19:33 12:04:50 7 3286 7 2002-04-05 00:00:00 12:04:50 8 3287 7 2007-11-27 00:19:33 17:01:57 9 3288 7 2002-04-05 00:00:00 17:01:57 10 3289 7 2002-04-05 00:00:00 20:17:57 11 3290 7 2007-11-27 00:19:33 20:17:57 12 3291 8 1900-01-01 00:00:00 00:00:00 1 3292 8 2000-11-07 15:28:31 00:00:00 2 3293 8 2007-09-19 02:35:12 00:00:00 3 3294 8 2004-12-17 11:22:46 00:00:00 4 3295 8 2001-10-20 06:42:39 00:00:00 5 3296 8 2000-11-07 15:28:31 05:49:51 6 3297 8 2004-12-17 11:22:46 05:49:51 7 3298 8 2007-09-19 02:35:12 05:49:51 8 3299 8 1900-01-01 00:00:00 05:49:51 9 3300 8 2001-10-20 06:42:39 05:49:51 10 3301 8 1900-01-01 00:00:00 07:09:22 11 3302 8 2001-10-20 06:42:39 07:09:22 12 3303 8 2004-12-17 11:22:46 07:09:22 13 3304 8 2007-09-19 02:35:12 07:09:22 14 3305 8 2000-11-07 15:28:31 07:09:22 15 3306 8 1900-01-01 00:00:00 08:06:08 16 3307 8 2004-12-17 11:22:46 08:06:08 17 3308 8 2000-11-07 15:28:31 08:06:08 18 3309 8 2007-09-19 02:35:12 08:06:08 19 3310 8 2001-10-20 06:42:39 08:06:08 20 3311 8 2004-12-17 11:22:46 12:51:37 21 3312 8 2000-11-07 15:28:31 12:51:37 22 3313 8 2007-09-19 02:35:12 12:51:37 23 3314 8 2001-10-20 06:42:39 12:51:37 24 3315 8 1900-01-01 00:00:00 12:51:37 25 3316 8 2000-11-07 15:28:31 13:39:32 26 3317 8 2007-09-19 02:35:12 13:39:32 27 3318 8 1900-01-01 00:00:00 13:39:32 28 3319 8 2001-10-20 06:42:39 13:39:32 29 3320 8 2004-12-17 11:22:46 13:39:32 30 3321 8 1900-01-01 00:00:00 17:04:35 31 3322 8 2001-10-20 06:42:39 17:04:35 32 3323 8 2004-12-17 11:22:46 17:04:35 33 3324 8 2007-09-19 02:35:12 17:04:35 34 3325 8 2000-11-07 15:28:31 17:04:35 35 3326 8 2007-09-19 02:35:12 23:02:05 36 3327 8 1900-01-01 00:00:00 23:02:05 37 3328 8 2004-12-17 11:22:46 23:02:05 38 3329 8 2000-11-07 15:28:31 23:02:05 39 3330 8 2001-10-20 06:42:39 23:02:05 40 3331 9 2006-10-12 19:52:02 00:00:00 1 3332 9 2006-10-12 19:52:02 04:56:37 2 3333 9 2006-10-12 19:52:02 08:09:49 3 3334 9 2006-10-12 19:52:02 11:18:35 4 3335 9 2006-10-12 19:52:02 19:03:57 5 3336 select `c`.col_int,`c`.col_datetime, `dd`.col_time, sum(`c`.col_int) over (partition by `c`.col_int 3337 order by `dd`.col_time) as `rank` from `dd` left join `c` on `c`.col_int = `dd`.col_int; 3338 col_int col_datetime col_time rank 3339 null null 00:00:00 null 3340 null null 00:00:00 null 3341 null null 00:00:00 null 3342 null null 00:27:13 null 3343 null null 02:20:01 null 3344 null null 08:28:55 null 3345 null null 09:40:42 null 3346 null null 10:20:49 null 3347 null null 11:26:20 null 3348 null null 14:34:36 null 3349 null null 14:58:44 null 3350 null null 18:50:24 null 3351 null null 20:12:00 null 3352 null null 20:55:45 null 3353 null null 23:23:35 null 3354 0 2002-09-03 04:42:41 00:00:00 0 3355 0 2002-09-03 04:42:41 00:00:00 0 3356 0 2002-09-03 04:42:41 08:16:28 0 3357 0 2002-09-03 04:42:41 11:25:58 0 3358 1 2008-03-19 08:36:41 00:00:00 1 3359 1 2008-03-19 08:36:41 01:41:35 2 3360 1 2008-03-19 08:36:41 03:05:06 3 3361 1 2008-03-19 08:36:41 10:10:42 4 3362 1 2008-03-19 08:36:41 17:42:13 5 3363 4 2008-03-27 03:09:30 06:00:42 8 3364 4 2005-09-13 00:00:00 06:00:42 8 3365 4 2005-09-13 00:00:00 13:02:05 16 3366 4 2008-03-27 03:09:30 13:02:05 16 3367 4 2008-03-27 03:09:30 15:32:00 24 3368 4 2005-09-13 00:00:00 15:32:00 24 3369 4 2005-09-13 00:00:00 16:15:34 32 3370 4 2008-03-27 03:09:30 16:15:34 32 3371 5 1900-01-01 00:00:00 00:00:00 5 3372 5 1900-01-01 00:00:00 00:59:59 10 3373 5 1900-01-01 00:00:00 02:16:04 15 3374 5 1900-01-01 00:00:00 09:25:59 20 3375 5 1900-01-01 00:00:00 14:38:03 25 3376 5 1900-01-01 00:00:00 15:11:27 30 3377 5 1900-01-01 00:00:00 16:20:06 35 3378 6 2008-04-14 09:20:36 00:00:00 18 3379 6 2008-04-14 09:20:36 00:00:00 18 3380 6 2008-04-14 09:20:36 00:00:00 18 3381 6 2008-04-14 09:20:36 05:15:45 24 3382 6 2008-04-14 09:20:36 06:03:27 30 3383 6 2008-04-14 09:20:36 06:11:59 36 3384 6 2008-04-14 09:20:36 09:21:09 42 3385 6 2008-04-14 09:20:36 11:04:39 48 3386 6 2008-04-14 09:20:36 19:55:11 54 3387 7 2007-11-27 00:19:33 00:00:00 28 3388 7 2007-11-27 00:19:33 00:00:00 28 3389 7 2002-04-05 00:00:00 00:00:00 28 3390 7 2002-04-05 00:00:00 00:00:00 28 3391 7 2007-11-27 00:19:33 03:56:10 42 3392 7 2002-04-05 00:00:00 03:56:10 42 3393 7 2007-11-27 00:19:33 12:04:50 56 3394 7 2002-04-05 00:00:00 12:04:50 56 3395 7 2007-11-27 00:19:33 17:01:57 70 3396 7 2002-04-05 00:00:00 17:01:57 70 3397 7 2002-04-05 00:00:00 20:17:57 84 3398 7 2007-11-27 00:19:33 20:17:57 84 3399 8 1900-01-01 00:00:00 00:00:00 40 3400 8 2000-11-07 15:28:31 00:00:00 40 3401 8 2007-09-19 02:35:12 00:00:00 40 3402 8 2004-12-17 11:22:46 00:00:00 40 3403 8 2001-10-20 06:42:39 00:00:00 40 3404 8 2000-11-07 15:28:31 05:49:51 80 3405 8 2004-12-17 11:22:46 05:49:51 80 3406 8 2007-09-19 02:35:12 05:49:51 80 3407 8 1900-01-01 00:00:00 05:49:51 80 3408 8 2001-10-20 06:42:39 05:49:51 80 3409 8 1900-01-01 00:00:00 07:09:22 120 3410 8 2001-10-20 06:42:39 07:09:22 120 3411 8 2004-12-17 11:22:46 07:09:22 120 3412 8 2007-09-19 02:35:12 07:09:22 120 3413 8 2000-11-07 15:28:31 07:09:22 120 3414 8 1900-01-01 00:00:00 08:06:08 160 3415 8 2004-12-17 11:22:46 08:06:08 160 3416 8 2000-11-07 15:28:31 08:06:08 160 3417 8 2007-09-19 02:35:12 08:06:08 160 3418 8 2001-10-20 06:42:39 08:06:08 160 3419 8 2004-12-17 11:22:46 12:51:37 200 3420 8 2000-11-07 15:28:31 12:51:37 200 3421 8 2007-09-19 02:35:12 12:51:37 200 3422 8 2001-10-20 06:42:39 12:51:37 200 3423 8 1900-01-01 00:00:00 12:51:37 200 3424 8 2000-11-07 15:28:31 13:39:32 240 3425 8 2007-09-19 02:35:12 13:39:32 240 3426 8 1900-01-01 00:00:00 13:39:32 240 3427 8 2001-10-20 06:42:39 13:39:32 240 3428 8 2004-12-17 11:22:46 13:39:32 240 3429 8 1900-01-01 00:00:00 17:04:35 280 3430 8 2001-10-20 06:42:39 17:04:35 280 3431 8 2004-12-17 11:22:46 17:04:35 280 3432 8 2007-09-19 02:35:12 17:04:35 280 3433 8 2000-11-07 15:28:31 17:04:35 280 3434 8 2007-09-19 02:35:12 23:02:05 320 3435 8 1900-01-01 00:00:00 23:02:05 320 3436 8 2004-12-17 11:22:46 23:02:05 320 3437 8 2000-11-07 15:28:31 23:02:05 320 3438 8 2001-10-20 06:42:39 23:02:05 320 3439 9 2006-10-12 19:52:02 00:00:00 9 3440 9 2006-10-12 19:52:02 04:56:37 18 3441 9 2006-10-12 19:52:02 08:09:49 27 3442 9 2006-10-12 19:52:02 11:18:35 36 3443 9 2006-10-12 19:52:02 19:03:57 45 3444 select `c`.col_int,`c`.col_datetime, `dd`.col_time, avg(`dd`.col_int) over (partition by `c`.col_int 3445 order by `dd`.col_time) as `rank` from `dd` left join `c` on `c`.col_int = `dd`.col_int; 3446 col_int col_datetime col_time rank 3447 null null 00:00:00 100.66666666666667 3448 null null 00:00:00 100.66666666666667 3449 null null 00:00:00 100.66666666666667 3450 null null 00:27:13 76.25 3451 null null 02:20:01 80.2 3452 null null 08:28:55 77.83333333333333 3453 null null 09:40:42 67.14285714285714 3454 null null 10:20:49 59.0 3455 null null 11:26:20 52.77777777777778 3456 null null 14:34:36 63.7 3457 null null 14:58:44 58.18181818181818 3458 null null 18:50:24 61.25 3459 null null 20:12:00 56.76923076923077 3460 null null 20:55:45 52.857142857142854 3461 null null 23:23:35 57.266666666666666 3462 0 2002-09-03 04:42:41 00:00:00 0.0 3463 0 2002-09-03 04:42:41 00:00:00 0.0 3464 0 2002-09-03 04:42:41 08:16:28 0.0 3465 0 2002-09-03 04:42:41 11:25:58 0.0 3466 1 2008-03-19 08:36:41 00:00:00 1.0 3467 1 2008-03-19 08:36:41 01:41:35 1.0 3468 1 2008-03-19 08:36:41 03:05:06 1.0 3469 1 2008-03-19 08:36:41 10:10:42 1.0 3470 1 2008-03-19 08:36:41 17:42:13 1.0 3471 4 2008-03-27 03:09:30 06:00:42 4.0 3472 4 2005-09-13 00:00:00 06:00:42 4.0 3473 4 2005-09-13 00:00:00 13:02:05 4.0 3474 4 2008-03-27 03:09:30 13:02:05 4.0 3475 4 2008-03-27 03:09:30 15:32:00 4.0 3476 4 2005-09-13 00:00:00 15:32:00 4.0 3477 4 2005-09-13 00:00:00 16:15:34 4.0 3478 4 2008-03-27 03:09:30 16:15:34 4.0 3479 5 1900-01-01 00:00:00 00:00:00 5.0 3480 5 1900-01-01 00:00:00 00:59:59 5.0 3481 5 1900-01-01 00:00:00 02:16:04 5.0 3482 5 1900-01-01 00:00:00 09:25:59 5.0 3483 5 1900-01-01 00:00:00 14:38:03 5.0 3484 5 1900-01-01 00:00:00 15:11:27 5.0 3485 5 1900-01-01 00:00:00 16:20:06 5.0 3486 6 2008-04-14 09:20:36 00:00:00 6.0 3487 6 2008-04-14 09:20:36 00:00:00 6.0 3488 6 2008-04-14 09:20:36 00:00:00 6.0 3489 6 2008-04-14 09:20:36 05:15:45 6.0 3490 6 2008-04-14 09:20:36 06:03:27 6.0 3491 6 2008-04-14 09:20:36 06:11:59 6.0 3492 6 2008-04-14 09:20:36 09:21:09 6.0 3493 6 2008-04-14 09:20:36 11:04:39 6.0 3494 6 2008-04-14 09:20:36 19:55:11 6.0 3495 7 2007-11-27 00:19:33 00:00:00 7.0 3496 7 2007-11-27 00:19:33 00:00:00 7.0 3497 7 2002-04-05 00:00:00 00:00:00 7.0 3498 7 2002-04-05 00:00:00 00:00:00 7.0 3499 7 2007-11-27 00:19:33 03:56:10 7.0 3500 7 2002-04-05 00:00:00 03:56:10 7.0 3501 7 2007-11-27 00:19:33 12:04:50 7.0 3502 7 2002-04-05 00:00:00 12:04:50 7.0 3503 7 2007-11-27 00:19:33 17:01:57 7.0 3504 7 2002-04-05 00:00:00 17:01:57 7.0 3505 7 2002-04-05 00:00:00 20:17:57 7.0 3506 7 2007-11-27 00:19:33 20:17:57 7.0 3507 8 1900-01-01 00:00:00 00:00:00 8.0 3508 8 2000-11-07 15:28:31 00:00:00 8.0 3509 8 2007-09-19 02:35:12 00:00:00 8.0 3510 8 2004-12-17 11:22:46 00:00:00 8.0 3511 8 2001-10-20 06:42:39 00:00:00 8.0 3512 8 2000-11-07 15:28:31 05:49:51 8.0 3513 8 2004-12-17 11:22:46 05:49:51 8.0 3514 8 2007-09-19 02:35:12 05:49:51 8.0 3515 8 1900-01-01 00:00:00 05:49:51 8.0 3516 8 2001-10-20 06:42:39 05:49:51 8.0 3517 8 1900-01-01 00:00:00 07:09:22 8.0 3518 8 2001-10-20 06:42:39 07:09:22 8.0 3519 8 2004-12-17 11:22:46 07:09:22 8.0 3520 8 2007-09-19 02:35:12 07:09:22 8.0 3521 8 2000-11-07 15:28:31 07:09:22 8.0 3522 8 1900-01-01 00:00:00 08:06:08 8.0 3523 8 2004-12-17 11:22:46 08:06:08 8.0 3524 8 2000-11-07 15:28:31 08:06:08 8.0 3525 8 2007-09-19 02:35:12 08:06:08 8.0 3526 8 2001-10-20 06:42:39 08:06:08 8.0 3527 8 2004-12-17 11:22:46 12:51:37 8.0 3528 8 2000-11-07 15:28:31 12:51:37 8.0 3529 8 2007-09-19 02:35:12 12:51:37 8.0 3530 8 2001-10-20 06:42:39 12:51:37 8.0 3531 8 1900-01-01 00:00:00 12:51:37 8.0 3532 8 2000-11-07 15:28:31 13:39:32 8.0 3533 8 2007-09-19 02:35:12 13:39:32 8.0 3534 8 1900-01-01 00:00:00 13:39:32 8.0 3535 8 2001-10-20 06:42:39 13:39:32 8.0 3536 8 2004-12-17 11:22:46 13:39:32 8.0 3537 8 1900-01-01 00:00:00 17:04:35 8.0 3538 8 2001-10-20 06:42:39 17:04:35 8.0 3539 8 2004-12-17 11:22:46 17:04:35 8.0 3540 8 2007-09-19 02:35:12 17:04:35 8.0 3541 8 2000-11-07 15:28:31 17:04:35 8.0 3542 8 2007-09-19 02:35:12 23:02:05 8.0 3543 8 1900-01-01 00:00:00 23:02:05 8.0 3544 8 2004-12-17 11:22:46 23:02:05 8.0 3545 8 2000-11-07 15:28:31 23:02:05 8.0 3546 8 2001-10-20 06:42:39 23:02:05 8.0 3547 9 2006-10-12 19:52:02 00:00:00 9.0 3548 9 2006-10-12 19:52:02 04:56:37 9.0 3549 9 2006-10-12 19:52:02 08:09:49 9.0 3550 9 2006-10-12 19:52:02 11:18:35 9.0 3551 9 2006-10-12 19:52:02 19:03:57 9.0 3552 select `c`.col_int,`dd`.col_time, min(`dd`.col_int) over (partition by `c`.col_int 3553 order by `dd`.col_time) as `rank` from `dd` left join `c` on `c`.col_int = `dd`.col_int; 3554 col_int col_time rank 3555 null 00:00:00 2 3556 null 00:00:00 2 3557 null 00:00:00 2 3558 null 00:27:13 2 3559 null 02:20:01 2 3560 null 08:28:55 2 3561 null 09:40:42 2 3562 null 10:20:49 2 3563 null 11:26:20 2 3564 null 14:34:36 2 3565 null 14:58:44 2 3566 null 18:50:24 2 3567 null 20:12:00 2 3568 null 20:55:45 2 3569 null 23:23:35 2 3570 0 00:00:00 0 3571 0 00:00:00 0 3572 0 08:16:28 0 3573 0 11:25:58 0 3574 1 00:00:00 1 3575 1 01:41:35 1 3576 1 03:05:06 1 3577 1 10:10:42 1 3578 1 17:42:13 1 3579 4 06:00:42 4 3580 4 06:00:42 4 3581 4 13:02:05 4 3582 4 13:02:05 4 3583 4 15:32:00 4 3584 4 15:32:00 4 3585 4 16:15:34 4 3586 4 16:15:34 4 3587 5 00:00:00 5 3588 5 00:59:59 5 3589 5 02:16:04 5 3590 5 09:25:59 5 3591 5 14:38:03 5 3592 5 15:11:27 5 3593 5 16:20:06 5 3594 6 00:00:00 6 3595 6 00:00:00 6 3596 6 00:00:00 6 3597 6 05:15:45 6 3598 6 06:03:27 6 3599 6 06:11:59 6 3600 6 09:21:09 6 3601 6 11:04:39 6 3602 6 19:55:11 6 3603 7 00:00:00 7 3604 7 00:00:00 7 3605 7 00:00:00 7 3606 7 00:00:00 7 3607 7 03:56:10 7 3608 7 03:56:10 7 3609 7 12:04:50 7 3610 7 12:04:50 7 3611 7 17:01:57 7 3612 7 17:01:57 7 3613 7 20:17:57 7 3614 7 20:17:57 7 3615 8 00:00:00 8 3616 8 00:00:00 8 3617 8 00:00:00 8 3618 8 00:00:00 8 3619 8 00:00:00 8 3620 8 05:49:51 8 3621 8 05:49:51 8 3622 8 05:49:51 8 3623 8 05:49:51 8 3624 8 05:49:51 8 3625 8 07:09:22 8 3626 8 07:09:22 8 3627 8 07:09:22 8 3628 8 07:09:22 8 3629 8 07:09:22 8 3630 8 08:06:08 8 3631 8 08:06:08 8 3632 8 08:06:08 8 3633 8 08:06:08 8 3634 8 08:06:08 8 3635 8 12:51:37 8 3636 8 12:51:37 8 3637 8 12:51:37 8 3638 8 12:51:37 8 3639 8 12:51:37 8 3640 8 13:39:32 8 3641 8 13:39:32 8 3642 8 13:39:32 8 3643 8 13:39:32 8 3644 8 13:39:32 8 3645 8 17:04:35 8 3646 8 17:04:35 8 3647 8 17:04:35 8 3648 8 17:04:35 8 3649 8 17:04:35 8 3650 8 23:02:05 8 3651 8 23:02:05 8 3652 8 23:02:05 8 3653 8 23:02:05 8 3654 8 23:02:05 8 3655 9 00:00:00 9 3656 9 04:56:37 9 3657 9 08:09:49 9 3658 9 11:18:35 9 3659 9 19:03:57 9 3660 drop table `c`; 3661 drop table `dd`; 3662 drop table `e`; 3663 drop table if exists t2; 3664 CREATE TABLE t2 (a int, b int); 3665 insert into t2 values(1,1); 3666 insert into t2 values(2,2); 3667 insert into t2 values(3,3); 3668 insert into t2 values(4,4); 3669 insert into t2 values(1,1); 3670 insert into t2 values(5,5); 3671 insert into t2 values(6,6); 3672 select a,min(a) over (partition by a) ,sum(a+1) over (partition by a) from t2; 3673 a min(a) over (partition by a) sum(a + 1) over (partition by a) 3674 1 1 4 3675 1 1 4 3676 2 2 3 3677 3 3 4 3678 4 4 5 3679 5 5 6 3680 6 6 7 3681 drop table t2; 3682 drop table if exists int_8; 3683 create table int_8 (id tinyint, sex varchar(10)); 3684 insert into int_8 values (-1, 'moolol'); 3685 insert into int_8 values (-128, 'fdhsajhd'); 3686 insert into int_8 values (32, 'fdhsajhd'); 3687 insert into int_8 values (-1, 'fdhsajhd'); 3688 insert into int_8 values (23, 'moolol'); 3689 insert into int_8 values (127, null); 3690 insert into int_8 values (-128, null); 3691 select * from int_8; 3692 id sex 3693 -1 moolol 3694 -128 fdhsajhd 3695 32 fdhsajhd 3696 -1 fdhsajhd 3697 23 moolol 3698 127 null 3699 -128 null 3700 select row_number() over (partition by sex order by id rows between unbounded preceding and unbounded following) as a, id, 3701 sum(id) over (partition by sex order by id rows between 1 following and 2 following) as b from int_8; 3702 a id b 3703 1 -128 127 3704 2 127 null 3705 1 -128 31 3706 2 -1 32 3707 3 32 null 3708 1 -1 23 3709 2 23 null 3710 select row_number() over (partition by sex order by id rows between 1 following and 2 following) as a, sum(id) over (partition by sex order by id 3711 rows between 1 following and 2 following) as b from int_8; 3712 a b 3713 1 127 3714 2 null 3715 1 31 3716 2 32 3717 3 null 3718 1 23 3719 2 null 3720 drop table int_8; 3721 drop table if exists int_16; 3722 create table int_16(col1 smallint,col2 bool,col3 datetime); 3723 insert into int_16 values(-32768, true, '2023-05-16 00:12:12'); 3724 insert into int_16 values(22201, false, '1997-01-13 12:12:00'); 3725 insert into int_16 values(-32768, true, '2000-10-10 11:11:11'); 3726 insert into int_16 values(4, false, '1020-10-01 01:01:01'); 3727 insert into int_16 values(32767, null, null); 3728 select max(col1) over (partition by col2 order by col1 rows between 2 preceding and 3 following) as col1 from int_16; 3729 col1 3730 32767 3731 22201 3732 22201 3733 -32768 3734 -32768 3735 select dense_rank() over (partition by col2 order by col1 rows between 2 preceding and 3 following) as col1 from int_16; 3736 col1 3737 1 3738 1 3739 2 3740 1 3741 1 3742 drop table int_16; 3743 drop table if exists int_32; 3744 create table int_32(i int, j int, k int); 3745 insert into int_32 values (-2147483648, 1, 1); 3746 insert into int_32 values (-2147483648, 1, 2); 3747 insert into int_32 values (2147483647, 1, 2); 3748 insert into int_32 values (2147483647, 2, 1); 3749 insert into int_32 values (13289392, 2, 2); 3750 insert into int_32 values (23289483, 1, 1); 3751 insert into int_32 values (-3824, 1, 1); 3752 insert into int_32 values (2438294, 1, 2); 3753 insert into int_32 values (-3824, 2, 1); 3754 select * from int_32; 3755 i j k 3756 -2147483648 1 1 3757 -2147483648 1 2 3758 2147483647 1 2 3759 2147483647 2 1 3760 13289392 2 2 3761 23289483 1 1 3762 -3824 1 1 3763 2438294 1 2 3764 -3824 2 1 3765 select *, rank() over (order by i,j,k rows between 2 preceding and 3 following) as o_ijk, 3766 min(i) over (order by j rows between 4 preceding and 5 following) as o_j, 3767 rank() over (order by k,j rows between 1 preceding and 1 following) as o_kj from int_32 order by i,j,k; 3768 i j k o_ijk o_j o_kj 3769 -2147483648 1 1 1 -2147483648 1 3770 -2147483648 1 2 2 -2147483648 6 3771 -3824 1 1 3 -2147483648 1 3772 -3824 2 1 4 -3824 4 3773 2438294 1 2 5 -2147483648 6 3774 13289392 2 2 6 -3824 9 3775 23289483 1 1 7 -2147483648 1 3776 2147483647 1 2 8 -2147483648 6 3777 2147483647 2 1 9 -3824 4 3778 drop table int_32; 3779 drop table if exists int_64; 3780 create table int_64(i bigint unsigned, j int, k int); 3781 insert into int_64 values (18446744073709551614, 1, 1); 3782 insert into int_64 values (18446744073709551614, 1, 2); 3783 insert into int_64 values (2147483647, 1, 2); 3784 insert into int_64 values (2147483647, 2, 1); 3785 insert into int_64 values (0, 2, 2); 3786 insert into int_64 values (0, 1, 1); 3787 select * from int_64; 3788 i j k 3789 18446744073709551614 1 1 3790 18446744073709551614 1 2 3791 2147483647 1 2 3792 2147483647 2 1 3793 0 2 2 3794 0 1 1 3795 select *, rank() over (order by i,j,k rows between 2 preceding and 3 following) as o_ijk, 3796 min(i) over (order by j rows between 4 preceding and 5 following) as o_j, 3797 rank() over (order by k,j rows between 1 preceding and 1 following) as o_kj from int_64 order by i,j,k; 3798 i j k o_ijk o_j o_kj 3799 0 1 1 1 0 1 3800 0 2 2 2 0 6 3801 2147483647 1 2 3 0 4 3802 2147483647 2 1 4 0 3 3803 18446744073709551614 1 1 5 0 1 3804 18446744073709551614 1 2 6 0 4 3805 drop table int_64; 3806 drop table if exists uint_8; 3807 create table uint_8 (col1 tinyint unsigned, col2 varchar(10)); 3808 insert into uint_8 values (1, 'moolol'); 3809 insert into uint_8 values (128, 'fdhsajhd'); 3810 insert into uint_8 values (32, 'fdhsajhd'); 3811 insert into uint_8 values (1, 'fdhsajhd'); 3812 insert into uint_8 values (23, 'moolol'); 3813 insert into uint_8 values (255, null); 3814 insert into uint_8 values (128, null); 3815 select * from uint_8; 3816 col1 col2 3817 1 moolol 3818 128 fdhsajhd 3819 32 fdhsajhd 3820 1 fdhsajhd 3821 23 moolol 3822 255 null 3823 128 null 3824 select row_number() over (partition by col2 order by col1 rows between unbounded preceding and unbounded following) as a, 3825 sum(col1) over (partition by col2 order by col1 rows between 1 following and 2 following) as b from uint_8; 3826 a b 3827 1 255 3828 2 null 3829 1 160 3830 2 128 3831 3 null 3832 1 23 3833 2 null 3834 select dense_rank() over (partition by col2 order by col1) as col1, sum(col1) over (partition by col2 order by col1) as col2 from uint_8; 3835 col1 col2 3836 1 128 3837 2 383 3838 1 1 3839 2 33 3840 3 161 3841 1 1 3842 2 24 3843 drop table uint_8; 3844 drop table if exists uint_16; 3845 create table uint_16(col1 smallint unsigned,col2 bool,col3 datetime); 3846 insert into uint_16 values(0, true, '2023-05-16 00:12:12'); 3847 insert into uint_16 values(0, false, '1997-01-13 12:12:00'); 3848 insert into uint_16 values(65535, true, '2000-10-10 11:11:11'); 3849 insert into uint_16 values(4, false, '1020-10-01 01:01:01'); 3850 insert into uint_16 values(null, null, null); 3851 insert into uint_16 values(65535, null, '1997-11-10 10:10:10'); 3852 select * from uint_16; 3853 col1 col2 col3 3854 0 true 2023-05-16 00:12:12 3855 0 false 1997-01-13 12:12:00 3856 65535 true 2000-10-10 11:11:11 3857 4 false 1020-10-01 01:01:01 3858 null null null 3859 65535 null 1997-11-10 10:10:10 3860 select max(col1) over (partition by col2 order by col1 rows between unbounded preceding and unbounded following) as col1 from uint_16; 3861 col1 3862 65535 3863 65535 3864 4 3865 4 3866 65535 3867 65535 3868 select dense_rank() over (partition by col2 order by col1 rows between unbounded preceding and unbounded following) as col1 from uint_16; 3869 col1 3870 1 3871 2 3872 1 3873 2 3874 1 3875 2 3876 drop table uint_16; 3877 drop table if exists uint_32; 3878 create table uint_32(i int unsigned, j int, k int); 3879 insert into uint_32 values (4294967295, 1, 1); 3880 insert into uint_32 values (4294967295, 1, 2); 3881 insert into uint_32 values (2147483647, 1, 2); 3882 insert into uint_32 values (2147483647, 2, 1); 3883 insert into uint_32 values (13289392, 2, 2); 3884 insert into uint_32 values (23289483, 1, 1); 3885 insert into uint_32 values (3824, 1, 1); 3886 insert into uint_32 values (2438294, 1, 2); 3887 insert into uint_32 values (3824, 2, 1); 3888 select * from uint_32; 3889 i j k 3890 4294967295 1 1 3891 4294967295 1 2 3892 2147483647 1 2 3893 2147483647 2 1 3894 13289392 2 2 3895 23289483 1 1 3896 3824 1 1 3897 2438294 1 2 3898 3824 2 1 3899 select max(i) over (order by i,j,k rows between 1 preceding and 2 following) as o_ijk, 3900 dense_rank() over (order by i rows between unbounded preceding and unbounded following) as o_j, 3901 rank() over (order by k,j rows between unbounded preceding and unbounded following) as o_kj from uint_32 order by i,j,k; 3902 o_ijk o_j o_kj 3903 2438294 1 1 3904 13289392 1 4 3905 23289483 2 6 3906 2147483647 3 9 3907 2147483647 4 1 3908 4294967295 5 6 3909 4294967295 5 4 3910 4294967295 6 1 3911 4294967295 6 6 3912 drop table uint_32; 3913 drop table if exists uint_64; 3914 create table uint_64(i bigint unsigned, j bigint unsigned, k decimal); 3915 insert into uint_64 values (18446744073709551615, 2147483647, 123213.99898); 3916 insert into uint_64 values (4294967295, 2147483647, 2); 3917 insert into uint_64 values (18446744073709551615, 1, 2); 3918 insert into uint_64 values (2147483647, 23289483, 123213.99898); 3919 insert into uint_64 values (13289392, 2, 2); 3920 insert into uint_64 values (18446744073709551615, 23289483, 1); 3921 insert into uint_64 values (3824, 13289392, 123213.99898); 3922 insert into uint_64 values (2438294, 1, 2); 3923 insert into uint_64 values (3824, 13289392, 1); 3924 select * from uint_64; 3925 i j k 3926 18446744073709551615 2147483647 123214 3927 4294967295 2147483647 2 3928 18446744073709551615 1 2 3929 2147483647 23289483 123214 3930 13289392 2 2 3931 18446744073709551615 23289483 1 3932 3824 13289392 123214 3933 2438294 1 2 3934 3824 13289392 1 3935 select rank() over (order by i,j,k rows between unbounded preceding and unbounded following) as o_ijk, 3936 max(i) over (order by i rows between 10 preceding and 2 following) as o_j, 3937 rank() over (order by k,j rows between unbounded preceding and unbounded following) as o_kj from uint_64 order by i,j,k; 3938 o_ijk o_j o_kj 3939 1 2438294 1 3940 2 13289392 7 3941 3 2147483647 3 3942 4 4294967295 5 3943 5 18446744073709551615 8 3944 6 18446744073709551615 6 3945 7 18446744073709551615 3 3946 8 18446744073709551615 2 3947 9 18446744073709551615 9 3948 drop table uint_64; 3949 drop table if exists decimal_64; 3950 create table decimal_64(col1 decimal(18,10), col2 char(10)); 3951 insert into decimal_64 values (23189723.2314892238902, 'male'); 3952 insert into decimal_64 values (23189723.2314892238902, 'female'); 3953 insert into decimal_64 values (3278.3243214124242, 'male'); 3954 insert into decimal_64 (col1, col2) values (32134243.2143243242142, 'male'); 3955 insert into decimal_64 values (-23189723.2314892238902, 'male'); 3956 insert into decimal_64 values (-3278.3243214124242, 'female'); 3957 insert into decimal_64 (col1, col2) values (32134243.2143243242142, 'male'); 3958 select * from decimal_64; 3959 col1 col2 3960 23189723.2314892239 male 3961 23189723.2314892239 female 3962 3278.3243214124 male 3963 32134243.2143243242 male 3964 -23189723.2314892239 male 3965 -3278.3243214124 female 3966 32134243.2143243242 male 3967 select max(col1) over (order by col1 rows between 1 preceding and 0 following) as newcol1 from decimal_64; 3968 newcol1 3969 -23189723.2314892239 3970 -3278.3243214124 3971 3278.3243214124 3972 23189723.2314892239 3973 23189723.2314892239 3974 32134243.2143243242 3975 32134243.2143243242 3976 select min(col1) over (partition by col2 order by col1 rows between unbounded preceding and unbounded following) as newcol from decimal_64 limit 4; 3977 newcol 3978 -3278.3243214124 3979 -3278.3243214124 3980 -23189723.2314892239 3981 -23189723.2314892239 3982 drop table decimal_64; 3983 drop table if exists decimal_128; 3984 create table decimal_128(col1 decimal(38,10), col2 char(10)); 3985 insert into decimal_128 values (2318972338274832748378887878.2314892238902, 'male'); 3986 insert into decimal_128 values (2318972338274832748378887878.23148922389, 'female'); 3987 insert into decimal_128 values (-3278234242342349090943024982.3243214124242, 'male'); 3988 insert into decimal_128 (col1, col2) values (32134243.2143243242142, 'male'); 3989 insert into decimal_128 values (-23189723.2314892238902, 'male'); 3990 insert into decimal_128 values (-3278234242342349090943024982.3243214124242, 'female'); 3991 insert into decimal_128 (col1, col2) values (32134243.2143243242142, 'male'); 3992 select * from decimal_128; 3993 col1 col2 3994 2318972338274832748378887878.2314892239 male 3995 2318972338274832748378887878.2314892239 female 3996 -3278234242342349090943024982.3243214124 male 3997 32134243.2143243242 male 3998 -23189723.2314892239 male 3999 -3278234242342349090943024982.3243214124 female 4000 32134243.2143243242 male 4001 select min(col1) over (order by col1 rows between 1 preceding and 0 following) as newcol1 from decimal_128; 4002 newcol1 4003 -3278234242342349090943024982.3243214124 4004 -3278234242342349090943024982.3243214124 4005 -3278234242342349090943024982.3243214124 4006 -23189723.2314892239 4007 32134243.2143243242 4008 32134243.2143243242 4009 2318972338274832748378887878.2314892239 4010 select dense_rank() over (order by col1 rows between 1 preceding and 2 following) as newcol2 from decimal_128; 4011 newcol2 4012 1 4013 1 4014 2 4015 3 4016 3 4017 4 4018 4 4019 select row_number() over (partition by col2 order by col1 rows between unbounded preceding and unbounded following) as newcol from decimal_128; 4020 newcol 4021 1 4022 2 4023 1 4024 2 4025 3 4026 4 4027 5 4028 drop table decimal_128; 4029 drop table if exists time01; 4030 create table time01 (col1 time, col2 timestamp); 4031 insert into time01 values ('12:1b2:12', '2023-05-16 00:12:12'); 4032 invalid input: invalid time value 12:1b2:12 4033 insert into time01 values ('23:23:59', '2019-05-16 23:23:59'); 4034 insert into time01 values ('12:12:12', '1997-01-13 01:02:03'); 4035 insert into time01 values ('23:23:59', '2023-05-16 00:12:12'); 4036 insert into time01 values ('01:02:03', '2019-05-16 23:23:59'); 4037 insert into time01 values (null, null); 4038 select * from time01; 4039 col1 col2 4040 23:23:59 2019-05-16 23:23:59 4041 12:12:12 1997-01-13 01:02:03 4042 23:23:59 2023-05-16 00:12:12 4043 01:02:03 2019-05-16 23:23:59 4044 null null 4045 select rank() over (order by col1 rows between 1 preceding and 0 following) as newcol1 from time01; 4046 newcol1 4047 1 4048 2 4049 3 4050 4 4051 4 4052 select dense_rank() over (order by col1 rows between 1 preceding and 2 following) as newcol2 from time01; 4053 newcol2 4054 1 4055 2 4056 3 4057 4 4058 4 4059 select row_number() over (partition by col2 order by col1 rows between unbounded preceding and unbounded following) as newcol from time01; 4060 newcol 4061 1 4062 1 4063 1 4064 2 4065 1 4066 select rank() over (partition by col1 order by col2 rows between 0 preceding and 0 following) as newcol from time01; 4067 newcol 4068 1 4069 1 4070 1 4071 1 4072 2 4073 select max(col2) over (partition by col1 order by col2 rows between 3 preceding and 3 following) as newcol from time01; 4074 newcol 4075 null 4076 2019-05-16 23:23:59 4077 1997-01-13 01:02:03 4078 2023-05-16 00:12:12 4079 2023-05-16 00:12:12 4080 drop table time01; 4081 drop table if exists window01; 4082 create table window01(col1 int, col2 varchar(20)); 4083 insert into window01 values(1,'老师'); 4084 insert into window01 values(2,'医生'); 4085 insert into window01 values(3,'工人'); 4086 insert into window01 values(10,'学生'); 4087 insert into window01 values(20,'学生'); 4088 insert into window01 values(12,'学生'); 4089 insert into window01 values(21,'老师'); 4090 insert into window01 values(100,'老师'); 4091 insert into window01 values(200,'工人'); 4092 select col2, avg(col1) over (partition by col2 order by col1 desc) from window01; 4093 col2 avg(col1) over (partition by col2 order by col1 desc) 4094 医生 2.0 4095 学生 20.0 4096 学生 16.0 4097 学生 14.0 4098 工人 200.0 4099 工人 101.5 4100 老师 100.0 4101 老师 60.5 4102 老师 40.666666666666664 4103 select col2, col1, sum(col1) over (partition by col2 order by col1 desc) from window01; 4104 col2 col1 sum(col1) over (partition by col2 order by col1 desc) 4105 医生 2 2 4106 学生 20 20 4107 学生 12 32 4108 学生 10 42 4109 工人 200 200 4110 工人 3 203 4111 老师 100 100 4112 老师 21 121 4113 老师 1 122 4114 select col2, col1, min(col1) over (partition by col2 order by col1 desc) from window01; 4115 col2 col1 min(col1) over (partition by col2 order by col1 desc) 4116 医生 2 2 4117 学生 20 20 4118 学生 12 12 4119 学生 10 10 4120 工人 200 200 4121 工人 3 3 4122 老师 100 100 4123 老师 21 21 4124 老师 1 1 4125 select col2, col1, max(col1) over (partition by col2 order by col1 desc) from window01; 4126 col2 col1 max(col1) over (partition by col2 order by col1 desc) 4127 医生 2 2 4128 学生 20 20 4129 学生 12 20 4130 学生 10 20 4131 工人 200 200 4132 工人 3 200 4133 老师 100 100 4134 老师 21 100 4135 老师 1 100 4136 select col2, col1, count(col1) over (partition by col2 order by col1 desc) from window01; 4137 col2 col1 count(col1) over (partition by col2 order by col1 desc) 4138 医生 2 1 4139 学生 20 1 4140 学生 12 2 4141 学生 10 3 4142 工人 200 1 4143 工人 3 2 4144 老师 100 1 4145 老师 21 2 4146 老师 1 3 4147 drop database test;