在行之间减去sql中的日期时间

时间:2013-12-31 06:27:08

标签: sql oracle

我有一个表的列表,其中包含程序,日期,组的列。我想找到同一组下的日期减法。 例如,我的表是:


SELECT maxList.program, (maxlist.DATE - minlist.DATE) RUNDATE, maxList 
GROUP FROM ( SELECT a.* FROM schedule a inner join ( SELECT program,Max(DATE) 
FROM schedule GROUP BY program ) b 
ON a.program = b.program )maxList 
inner join ( SELECT a.* FROM schedule a 
inner join ( SELECT program,Min(DATE) FROM schedule 
GROUP BY GROUP ) b 
ON a.program = b.program ) minList 
ON maxList.program = minList. program 


**program  | date                | group**
    a     | 04.12.2013 19:16:08 |   1
    b     | 27.12.2013 00:47:41 |   1
    c     | 30.12.2013 00:47:41 |   1
    d     | 26.12.2013 14:02:42 |   2  
    e     | 31.12.2013 12:03:42 |   2

我想要的是将同一组下的最新日期减去初始日期。 例如30.12.2013 ....- 04.12.2013,是否有任何编码可用于在sql中应用减法?

2 个答案:

答案 0 :(得分:2)

只需汇总MAXMIN

SELECT   group, 
         MIN(date) || '..' || MAX(date) AS date_range, 
         MAX(date) - MIN(date) AS days_between
FROM     mytable
GROUP BY group

答案 1 :(得分:1)

SELECT   group**, MAX(date) - MIN(date) as date_diff
FROM     your_table
GROUP BY group**