3

以下是我自己的练习:

我创建了一个包含一些值的表,如:

CREATE TABLE my_test AS
SELECT ROWNUM ID, TRUNC(SYSDATE)+(LEVEL*5/24/60/60)date_time , 111 person_id
FROM dual CONNECT BY LEVEL <= (24*60*60)/5 
ORDER BY 1;

现在,我更新了 table ,person_id = 222for date_timebetween 5:30 amand6:30 am

UPDATE my_test
SET  person_id   = 222
WHERE date_time >= trunc(SYSDATE)+ 5/24 + 30/24/60
 AND DATE_TIME  <= trunc(sysdate)+ 6/24 + 30/24/60;

我需要每小时计算人数,所以我编写了如下代码:

SELECT time_range,count(distinct person_id) PERSON_COUNT
FROM (
select person_id , date_time,
case TO_CHAR(date_time,'hh24') when '00' then '12 AM - 01 AM' 
             when '01' then '01 AM - 02 AM'
             when '02' then '02 AM - 03 AM'
             when '03' then '03 AM - 04 AM'
             when '04' then '04 AM - 05 AM'
             when '05' then '05 AM - 06 AM'
             when '06' then '06 AM - 07 AM'
             when '07' then '07 AM - 08 AM'
             when '08' then '08 AM - 09 AM'
             when '09' then '09 AM - 10 AM'
             when '10' then '10 AM - 11 AM'
             when '11' then '11 AM - 12 PM'
             when '12' then '12 PM - 01 PM'
             when '13' then '01 PM - 02 PM'
             when '14' then '02 PM - 03 PM'
             when '15' then '03 PM - 04 PM'
             when '16' then '04 PM - 05 PM'
             when '17' then '05 PM - 06 PM'
             when '18' then '06 PM - 07 PM'
             when '19' then '07 PM - 08 PM'
             when '20' then '08 PM - 09 PM'
             when '21' then '09 PM - 10 PM'
             when '22' then '10 PM - 11 PM'
             WHEN '23' THEN '11 PM - 12 AM' end time_range
FROM my_test
WHERE date_time >= trunc(SYSDATE)
AND date_time < trunc(SYSDATE)+1
ORDER BY date_time)
GROUP BY time_range;

它工作正常,没有预期的订购。( time_range 的顺序没有给出预期的结果,因为它是外部查询中的字符串)。

我需要按 time_range 订购,以上任何简单易行的代码都值得赞赏。提前致谢。

4

3 回答 3

2

ORDER BY子句始终是在单个查询中被解释的最后一个,但在这里它位于子查询中,因此主查询GROUP BY将“覆盖”您的ORDER BY.

例如,您可以编写:

SELECT time_range, COUNT(DISTINCT person_id) PERSON_COUNT
  FROM (SELECT person_id,
               date_time,
               CASE TO_CHAR(date_time, 'hh24')
                  WHEN '00' THEN
                   '12 AM - 01 AM'
               /*[snip]*/
                  WHEN '23' THEN
                   '11 PM - 12 AM'
               END time_range
          FROM my_test
         WHERE date_time >= trunc(SYSDATE)
           AND date_time < trunc(SYSDATE) + 1)
 GROUP BY time_range, TO_CHAR(date_time, 'hh24')
 ORDER BY TO_CHAR(date_time, 'hh24');

我也不喜欢你的time_range表达方式。您可以简单地将其重写为:

to_char(date_time, 'HH PM - ') || to_char(date_time + 1/24, 'HH PM') time_range

编辑:显然你需要完整的查询:

SELECT time_range, COUNT(DISTINCT person_id) PERSON_COUNT
  FROM (SELECT person_id,
               date_time,
               to_char(date_time, 'HH PM - ') 
               || to_char(date_time + 1/24, 'HH PM')  time_range
          FROM my_test
         WHERE date_time >= trunc(SYSDATE)
           AND date_time < trunc(SYSDATE) + 1)
 GROUP BY time_range, TO_CHAR(date_time, 'hh24')
 ORDER BY TO_CHAR(date_time, 'hh24');
于 2013-09-11T11:15:42.670 回答
1

首先,您应该order by外部查询而不是内部查询中使用。 order by在内部查询中通常不能保证工作。

但是,即使你输入:

order by date_time;

最后,你还是得不到你想要的。为此,请尝试date_time自行订购,如下所示:

order by max(date_time);

以下是关于子查询的Oracle 文档示例order by

ORDER BY 子句允许您指定行在结果集中出现的顺序。在子查询中,ORDER BY 子句是没有意义的,除非它带有结果偏移量和 fetch first 子句中的一个或两个,或者与 ROW_NUMBER 函数一起使用,因为不能保证在外部结果集中保留顺序。允许将外部查询中的 ORDER BY 与子查询中的 ORDER BY 结合起来。

于 2013-09-11T11:14:13.300 回答
0
select case TO_CHAR(date_time,'hh24') when '00' then '12 AM - 01 AM' 
         when '01' then '01 AM - 02 AM'
         when '02' then '02 AM - 03 AM'
         when '03' then '03 AM - 04 AM'
         when '04' then '04 AM - 05 AM'
         when '05' then '05 AM - 06 AM'
         when '06' then '06 AM - 07 AM'
         when '07' then '07 AM - 08 AM'
         when '08' then '08 AM - 09 AM'
         when '09' then '09 AM - 10 AM'
         when '10' then '10 AM - 11 AM'
         when '11' then '11 AM - 12 PM'
         when '12' then '12 PM - 01 PM'
         when '13' then '01 PM - 02 PM'
         when '14' then '02 PM - 03 PM'
         when '15' then '03 PM - 04 PM'
         when '16' then '04 PM - 05 PM'
         when '17' then '05 PM - 06 PM'
         when '18' then '06 PM - 07 PM'
         when '19' then '07 PM - 08 PM'
         when '20' then '08 PM - 09 PM'
         when '21' then '09 PM - 10 PM'
         when '22' then '10 PM - 11 PM'
         WHEN '23' THEN '11 PM - 12 AM' end time_range,
count(person_id) person_count
FROM my_test
WHERE date_time >= trunc(SYSDATE)
AND date_time < trunc(SYSDATE)+1 
group by TO_CHAR(date_time,'hh24')
ORDER BY 1
于 2013-09-11T11:24:52.177 回答