当前位置:网站首页>sql 语句问题, 求计算相差10分钟以内的数据作为同一批次数据显示
sql 语句问题, 求计算相差10分钟以内的数据作为同一批次数据显示
2022-07-27 12:36:00 【CSDN问答】
数据源如下:
| id | order_type | pickup_time |
| 1 | AAA | 2022/6/7 6:42 |
| 2 | AAA | 2022/6/7 6:43 |
| 3 | AAA | 2022/6/7 6:55 |
| 4 | AAA | 2022/6/8 6:52 |
| 5 | BBB | 2022/6/7 6:55 |
| 6 | BBB | 2022/6/7 7:00 |
| 7 | BBB | 2022/6/7 7:00 |
| 8 | BBB | 2022/6/7 7:01 |
| 9 | BBB | 2022/6/7 7:08 |
| 10 | BBB | 2022/6/7 7:08 |
| 11 | BBB | 2022/6/7 7:08 |
| 12 | BBB | 2022/6/7 7:12 |
| 13 | BBB | 2022/6/7 7:13 |
根据order_type 和 pickup_time排序后,需要得到如下的lot_number列数据, 算法是:相同order_type 的情况下按pickup_time升序排序从第一条开始,时间相差在10分钟以内的为同一批次数据,后条的lot_number用前条相同的时间戳来标示;相差超过10分钟的用另外一个时间戳批次标示,以此类推;需要注意的是:
第9行开始,它的时间7:08 不是和第8行的 7:01分比较,而是和 5,6,7,8行计算的批次号06:55分比比较,这2个时间一比较就大于10分钟了; 因为7:08分和 6:55分 是没法划为10分钟以内同一批次的,必须重新划入下一个批次。
分别用select 和 update 更新到lot_number 列写一下SQL
| id | order_type | pickup_time | lot_number | |
| 1 | AAA | 2022/6/7 6:42 | 2022-06-07 06:42 | 同一批次 |
| 2 | AAA | 2022/6/7 6:43 | 2022-06-07 06:42 | |
| 3 | AAA | 2022/6/7 6:55 | 2022-06-07 06:55 | |
| 4 | AAA | 2022/6/8 6:52 | 2022-06-08 06:52 | |
| 5 | BBB | 2022/6/7 6:55 | 2022-06-07 06:55 | 同一批次 |
| 6 | BBB | 2022/6/7 7:00 | 2022-06-07 06:56 | |
| 7 | BBB | 2022/6/7 7:00 | 2022-06-07 06:55 | |
| 8 | BBB | 2022/6/7 7:01 | 2022-06-07 06:55 | |
| 9 | BBB | 2022/6/7 7:08 | 2022-06-07 07:08 | 同一批次 |
| 10 | BBB | 2022/6/7 7:08 | 2022-06-07 07:08 | |
| 11 | BBB | 2022/6/7 7:08 | 2022-06-07 07:08 | |
| 12 | BBB | 2022/6/7 7:12 | 2022-06-07 07:08 | |
| 13 | BBB | 2022/6/7 7:13 | 2022-06-07 07:08 |
边栏推荐
- MySQL common commands
- JVM memory layout detailed, illustrated, well written!
- JS date and time format (year, month, day, hour, minute, second, week, quarter, time difference acquisition, date and timestamp conversion function)
- Detail throw and throws
- QT | qcheckbox of control
- 多表查询
- 20210419 combined sum
- Switching value input and output module dam-5055
- nodejs body-parser中间件处理类型 multipart/form-data 的 POST 表单数据,req.body无法接收到数据
- Lambda 表达式
猜你喜欢

多表查询

J9 number theory: how long is the mainstreaming of decentralized identity?

XXL job parameter transfer

详述throw与throws

Openpyxl drawing area map

Mixin\ plug in \scoped style

Configuration files in MySQL

2022 global Vocational Education Industry Development Report

JVM memory layout detailed, illustrated, well written!

Self built personalized automatic quotation system to cope with changeable quotation mode
随机推荐
How to ask questions on the road for the first time - necessary skills for self-study (with live playback)
About the problem that the onapplicationevent method of the custom listener is executed multiple times
Cvpr22 | graph neural architecture search of relational consciousness
2021-3-22-tencent - minimum number of guards
Uniapp video video playback is not completed. It is forbidden to drag the progress bar fast forward
Overview of static inner classes and non static inner classes
HDU1698_Just a Hook
20210419 combined sum
CVPR22 | 关系意识的图神经架构搜索
时间工具类,得到当前时间,date转string
内涵语录
Map interface
Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)
Vi. analysis of makefile.build
堆
JS date and time format (year, month, day, hour, minute, second, week, quarter, time difference acquisition, date and timestamp conversion function)
P1321 word overlay restore [getting started]
C program debugging and exception handling (try catch)
redis分布式在线安装
Specify the add method of HashSet