简体   繁体   中英

Oracle GROUP BY and date precision

I've got a table with two date fields : BEGIN_DATE and END_DATE

When I subtract these two fields, I get a number in days.
I want this number in seconds because the difference between these two fields is very tiny (~ 1s).
So I proceed by doing :

SELECT ROUND(AVG((END_DATE-BEGIN_DATE)*3600*24),2) AS DELTA,
       TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24:mi:ss') AS DEB, 
       TO_CHAR(END_DATE, 'yyyy-mm-dd hh24:mi:ss') AS FIN
FROM MYTABLE
GROUP BY TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24:mi:ss'), 
         TO_CHAR(END_DATE, 'yyyy-mm-dd hh24:mi:ss');

Here is the result (same precision with group by minutes ) :
在此处输入图片说明


Well.
Then if I group the results by hour or by day :

SELECT ROUND(AVG((END_DATE-BEGIN_DATE)*3600*24),2) AS DELTA,
       TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24') AS DEB, 
       TO_CHAR(END_DATE, 'yyyy-mm-dd hh24') AS FIN
FROM MYTABLE
GROUP BY TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24'), 
         TO_CHAR(END_DATE, 'yyyy-mm-dd hh24');

I've got this result :
在此处输入图片说明

The DELTA precision is better and I can't understand why !
Could someone explain me ?

My bad, don't go further, the problem is relatively simple.

Floating results are given by : SUM(DELTA) / COUNT(Grouped rows).

So if I've got 20 values at 2015-11-02 19.
9 of these values are equal to 1, remainder equals to 0.
We've got 9/20 = 0.45 which is absolutely logic. I just verified it.

Thank you anyway for your time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM