--获得时间区域
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
--获得时间区域表
select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag
--获得数据集合
select * from t2
union all
select * from t3
--获得所需要的数据
select dataset.type as datatype,dataset.num1,timesheet.startdate,timesheet.date as enddate,timesheet.tag from
(select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag) as timesheet
,(select * from t2 union all select * from t3) dataset
where dataset.date between timesheet.startdate and timesheet.date
--凑结构
select date,num,t1.tag,
sum(case when datatype='in' then num1 end) as numin,
sum(case when datatype='out' then num1 end) as numout from t1
left join (
select dataset.type as datatype,dataset.num1,timesheet.startdate,timesheet.date as enddate,timesheet.tag from
(select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag) as timesheet
,(select * from t2 union all select * from t3) dataset
where dataset.date between timesheet.startdate and timesheet.date
) as result on t1.tag=result.tag
group by date,num,t1.tag
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
--获得时间区域表
select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag
--获得数据集合
select * from t2
union all
select * from t3
--获得所需要的数据
select dataset.type as datatype,dataset.num1,timesheet.startdate,timesheet.date as enddate,timesheet.tag from
(select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag) as timesheet
,(select * from t2 union all select * from t3) dataset
where dataset.date between timesheet.startdate and timesheet.date
--凑结构
select date,num,t1.tag,
sum(case when datatype='in' then num1 end) as numin,
sum(case when datatype='out' then num1 end) as numout from t1
left join (
select dataset.type as datatype,dataset.num1,timesheet.startdate,timesheet.date as enddate,timesheet.tag from
(select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag) as timesheet
,(select * from t2 union all select * from t3) dataset
where dataset.date between timesheet.startdate and timesheet.date
) as result on t1.tag=result.tag
group by date,num,t1.tag
结果: