查询以在特定时间获取数据

时间:2014-12-24 10:42:34

标签: oracle join

我需要写一个出勤查询,它给出了当前时间和前10分钟的出勤率。共有20条记录。 我有以下查询。它为我提供了当前时间的正确数据。但之前的出席率还在下降。 例如:在下午12:30,出勤率为2000.但是当时间到达下午1:30时,对应于12:30的出勤率显示得非常少。

SELECT   TO_CHAR (each_sec, 'HH24:MI') p_time, COUNT (each_sec) cnt
FROM (SELECT *
        FROM (SELECT d.userid, d.logdate
                FROM atendance_data d
                     JOIN
                     (SELECT   userid, MAX (logdate) logdate
                          FROM atendance_data
                         WHERE TRUNC (logdate) = TRUNC (SYSDATE)
                      GROUP BY userid) m
                     ON d.userid = m.userid AND d.logdate = m.logdate
               WHERE d.c1 NOT IN ('Check-Out', 'Break-Out')) c
             JOIN
             (SELECT     SYSDATE + (1 - LEVEL) / 24 / 3600 * 600 each_sec
                    FROM DUAL
              CONNECT BY LEVEL <= 20) s ON  c.logdate <= s.each_sec
             )
 GROUP BY each_sec
   ORDER BY each_sec

我必须写一个where条件来取最大值logdate,说小于each_sec。我想这就是我出错的地方。但我不知道该怎么做。

任何人都可以帮我这样做吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我已经完成了这样的事情。谢谢大家的回复

SELECT   COUNT (*), each_sec
FROM (SELECT a.userid, a.logdate, a.c1, each_sec
        FROM atendance_data a,
             (SELECT   userid, MAX (logdate) logdate, each_sec
                  FROM (SELECT userid, logdate, c1, each_sec
                          FROM (SELECT a.USERID,A.LOGDATE,A.C1, b.each_sec
                                  FROM atendance_data a
                                       JOIN
                                       (SELECT       SYSDATE
                                                   +   (1 - LEVEL)
                                                     / 24
                                                     / 3600
                                                     * 600 each_sec
                                              FROM DUAL
                                        CONNECT BY LEVEL <= 20) b
                                       ON a.logdate <= b.each_sec
                                     AND trunc(a.logdate) =trunc(sysdate)                                                                 
                                       ))
              GROUP BY userid, each_sec) b
       WHERE a.userid = b.userid
         AND a.logdate = b.logdate
         AND Trunc (a.logdate) = trunc(sysdate))
WHERE c1 NOT IN ('Check-Out', 'Break-Out')
GROUP BY each_sec 
ORDER  BY each_sec;