查询以获取该ID的当前日期和最小日期之间的差异

时间:2016-04-20 08:48:28

标签: sql oracle

我写过这个sql查询。它没有用,它说

  

从关键字NOT找到不合预期的地方

我哪里错了?我的日期格式为DD-MM-YY HH24:MI:SS。我正在使用Oracle数据库。

 SELECT dr1.pod_id, current_date() - dr1.daily_reading_date 
 FROM (
     SELECT pod_id, 
            MIN(daily_reading_date) mindate 
     FROM  daily_data_reading
     GROUP BY pod_id
      ) mini
 INNER JOIN daily_data_reading dr1
     ON  dr1.pod_id = mini.pod_id 
     AND dr1.daily_reading_date = mini.mindate

2 个答案:

答案 0 :(得分:1)

Oracle支持CURRENT_DATE / SYSDATE而非current_date()

SELECT dr1.pod_id, SYSDATE - dr1.daily_reading_date 
 FROM (
     SELECT pod_id, 
            MIN(daily_reading_date) mindate 
     FROM  daily_data_reading
     GROUP BY pod_id
      ) mini
 INNER JOIN daily_data_reading dr1
     ON  dr1.pod_id = mini.pod_id 
     AND dr1.daily_reading_date = mini.mindate

(OR)

SELECT dr1.pod_id, CURRENT_DATE - dr1.daily_reading_date 
 FROM (
     SELECT pod_id, 
            MIN(daily_reading_date) mindate 
     FROM  daily_data_reading
     GROUP BY pod_id
      ) mini
 INNER JOIN daily_data_reading dr1
     ON  dr1.pod_id = mini.pod_id 
     AND dr1.daily_reading_date = mini.mindate

答案 1 :(得分:0)

没有自我加入。 FIRST, LAST

select pod_id,
 CURRENT_DATE - MIN(daily_reading_date) KEEP (DENSE_RANK FIRST ORDER BY daily_reading_date) OVER (PARTITION BY pod_id) "LowestDateGroupedBYID"
 from 
 daily_data_reading