日期:2014-05-17 浏览次数:20752 次
select name, to_char(min(ny), 'yyyymm') || '--' || to_char(max(ny), 'yyyymm'), danwei from (select name, ny, to_char(ny, 'yyyymm') - rownum rn, danwei from t order by name,danwei,ny) where name = '张三' group by rn, name, danwei order by 2;
------解决方案--------------------
WITH t AS ( SELECT '张三' name, to_date('200001','YYYYMM') ny,'一队' danwei FROM dual UNION ALL SELECT '张三' name, to_date('200201','YYYYMM') ny,'一队' danwei FROM dual UNION ALL SELECT '张三' name, to_date('200202','YYYYMM') ny,'二队' danwei FROM dual UNION ALL SELECT '张三' name, to_date('200301','YYYYMM') ny,'二队' danwei FROM dual UNION ALL SELECT '张三' name, to_date('200302','YYYYMM') ny,'三队' danwei FROM dual UNION ALL SELECT '李四' name, to_date('200001','YYYYMM') ny,'一队' danwei FROM dual ) select a.name, to_char(a.ny,'YYYYMM') || '--' || to_char(b.ny,'YYYYMM') ny, a.danwei from (select name, min(ny) ny, danwei from t GROUP BY NAME,danwei) a LEFT JOIN (select name, MAX(ny) ny, danwei from t GROUP BY NAME,danwei) b ON a.name = b.name AND a.danwei = b.danwei group by a.name ,to_char(a.ny,'YYYYMM') || '--' || to_char(b.ny,'YYYYMM') ,a.danwei order by 1,2,3;