MySQL分割数据
引言一般来说讲,提到数据拆分,可以归结为两个层面:一是垂直拆分,二是水平拆分。这里我们来讨论下垂直拆分。垂直拆分是以数据库、表、列等为单位进行拆分的方法。正文MySQL里垂直拆分可以细分为:垂直拆库(...
2024.11.15原始数据
select d.waybill_no,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code1:=picked_by AS picked_by1,
MIN(spd.picked_date) start_time,
MAX(spd.picked_date) end_time,
TIMESTAMPDIFF(HOUR,MIN(spd.picked_date),MAX(spd.picked_date)) times,
spd.picked_by
FROM `shipping_order` d
LEFT JOIN shipping_pick_detail spd
ON d.`SHIPPING_ORDER_ID`=spd.SHIPPING_ORDER_ID
GROUP BY spd.picked_by,d.waybill_no
如图:
原始需要求和数据
添加分组行号:
select
start_time,
end_time,
picked_by,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code:=picked_by AS parent_code
FROM (
select d.waybill_no,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code1:=picked_by AS picked_by1,
MIN(spd.picked_date) start_time,
MAX(spd.picked_date) end_time,
TIMESTAMPDIFF(HOUR,MIN(spd.picked_date),MAX(spd.picked_date)) times,
spd.picked_by
FROM `shipping_order` d
LEFT JOIN shipping_pick_detail spd
ON d.`SHIPPING_ORDER_ID`=spd.SHIPPING_ORDER_ID
GROUP BY spd.picked_by,d.waybill_no) iop
LEFT JOIN ( select @group_row:=1, @parent_code:=‘‘,@parent_code1:=‘‘ ) b
ON 1=1
ORDER BY picked_by
添加分组行号结果
分组汇总效果
select
picked_by,
SUM(t.duration) - SUM(t.overlap) AS filtered_duration
FROM
(
select
t1.picked_by,
t1.start_time,
t1.end_time,
TIMESTAMPDIFF(HOUR,t1.start_time,t1.end_time) AS duration,
SUM(
IF(t2.start_time < t1.start_time AND t2.end_time > t1.end_time , TIMESTAMPDIFF(HOUR,t1.start_time,t1.end_time), 0) -- t2 completely around t1
+ IF(t2.start_time >= t1.start_time AND t2.end_time t1.start_time AND t2.end_time < t1.end_time , TIMESTAMPDIFF(HOUR,t1.start_time,t2.end_time), 0) -- t2 starts before t1 starts and overlaps partially
+ IF(t2.start_time < t1.end_time AND t2.end_time > t1.end_time AND t2.start_time > t1.start_time, TIMESTAMPDIFF(HOUR,t2.start_time,t1.end_time), 0) -- t2 starts before t1 ends and overlaps partially
) AS overlap
FROM
( select
start_time,
end_time,
picked_by,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code:=picked_by AS parent_code
FROM (
select d.waybill_no,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code1:=picked_by AS picked_by1,
MIN(spd.picked_date) start_time,
MAX(spd.picked_date) end_time,
TIMESTAMPDIFF(HOUR,MIN(spd.picked_date),MAX(spd.picked_date)) times,
spd.picked_by
FROM `shipping_order` d
LEFT JOIN shipping_pick_detail spd
ON d.`SHIPPING_ORDER_ID`=spd.SHIPPING_ORDER_ID
GROUP BY spd.picked_by,d.waybill_no) iop
LEFT JOIN ( select @group_row:=1, @parent_code:=‘‘,@parent_code1:=‘‘ ) b
ON 1=1
ORDER BY picked_by ) t1
LEFT JOIN ( select
start_time,
end_time,
picked_by,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code:=picked_by AS parent_code
FROM (
select d.waybill_no,
@group_row:=CASE WHEN @parent_code=picked_by THEN @group_row+1 ELSE 1 END AS id,
@parent_code1:=picked_by AS picked_by1,
MIN(spd.picked_date) start_time,
MAX(spd.picked_date) end_time,
TIMESTAMPDIFF(HOUR,MIN(spd.picked_date),MAX(spd.picked_date)) times,
spd.picked_by
FROM `shipping_order` d
LEFT JOIN shipping_pick_detail spd
ON d.`SHIPPING_ORDER_ID`=spd.SHIPPING_ORDER_ID
GROUP BY spd.picked_by,d.waybill_no) iop
LEFT JOIN ( select @group_row:=1, @parent_code:=‘‘,@parent_code1:=‘‘ ) b
ON 1=1
ORDER BY picked_by ) t2
ON t1.picked_by=t2.picked_by
AND t2.id > t1.id
AND (
(t2.start_time < t1.start_time AND t2.end_time > t1.end_time )
OR (t2.start_time >= t1.start_time AND t2.end_time t1.start_time)
OR (t2.start_time < t1.end_time AND t2.end_time > t1.end_time )
)
GROUP BY
t1.start_time,
t1.end_time,t1.picked_by
) AS t
GROUP BY picked_by
已经过滤重复时间段
注意,时间差取的是小时。
引言一般来说讲,提到数据拆分,可以归结为两个层面:一是垂直拆分,二是水平拆分。这里我们来讨论下垂直拆分。垂直拆分是以数据库、表、列等为单位进行拆分的方法。正文MySQL里垂直拆分可以细分为:垂直拆库(...
2024.11.15mysql数据库,当我们按日期展示数据时,经常碰到日期时间段内某一天数据不存在,为空。这时候我们查询数据往如下图一样,时间数据发生间断,造成数据缺失,无法连续展示所以可以加上下面的代码,进行一个链接查...
2024.11.13这是mysql系列第2篇文章。环境:mysql5.7.25,cmd命令中进行演示。主要内容介绍mysql中常用的数据类型mysql类型和java类型对应关系数据类型选择的一些建议MySQL的数据类型主...
2024.11.15普通索引普通索引是最基本的索引类型,而且它没有唯一性之类的限制。普通索引可以通过以下几种方式创建:创建索引,例如CREATE INDEX ON tablename (列的列表);修改表,例如ALTE...
2024.11.13文章来源:https://blog.csdn.net/horses/article/details/107028750原文作者:不剪发的Tony老师来源平台:CSDN12.1 聚合函数在 SQL 中,...
2024.11.15