为了大家能看懂,模拟了一个数据集 如下:select 公司,产出,开始时间,结束时间 from ( select '国通(明细)' AS 公司,'200'as 产出,'2015-01-01' as 开始时间,'2015-02-05' as 结束时间 union all select '国通(明细)' AS 公司,'300'as 产出,'2015-02-05' as 开始时间,'2015-03-05' as 结束时间 union all select '凯联(明细)' AS 公司,'100'as 产出,'2015-01-01' as 开始时间,'2015-02-05' as 结束时间 UNION ALL select '凯联(明细)' AS 公司,'200'as 产出,'2015-02-05' as 开始时间,'2015-03-05' as 结束时间 union all select 公司,SUM(产出),MIN(开始时间),MAX(结束时间) from ( select '国通(合计)' AS 公司,200 as 产出,'2015-01-01' as 开始时间,'2015-02-05' as 结束时间 union all select '国通(合计)' AS 公司,300 as 产出,'2015-02-05' as 开始时间,'2015-03-05' as 结束时间 union all select '凯联(合计)' AS 公司,100 as 产出,'2015-01-01' as 开始时间,'2015-02-05' as 结束时间 union all select '凯联(合计)' AS 公司,200 as 产出,'2015-02-05' as 开始时间,'2015-03-05' as 结束时间 )as table1 GROUP BY 公司) as cc order by 公司,开始时间,结束时间 desc 该数据集返回的结果如下图: 最后要求的结果是每分类公司的末位行加空行 |