团结一小时 - 神谕 - 什么是错的

时间:2014-10-21 13:45:27

标签: oracle

这个查询有什么问题。 我需要花24个小时的所有时间来计算每小时的点击次数。 我想在0-24时提出一个案例,但我不能做第一件事

SELECT
    (
    CASE WHEN (
           CAST(TO_CHAR(dg_user_acess.dt_user_acess, 'HH24') >= '00')
         AND
          CAST(TO_CHAR(dg_user_acess.dt_user_acess, 'HH24') < '01')
         )
        THEN ('00:00 as 01:00')
        ELSE '0' END
    ) as hour,        
    count(*)
FROM dg_user_acess
GROUP by hour

我收到错误

Erro de SQL:ORA-00905:palavra-chavenãenconconda 00905. 00000 - “缺少关键字”

表格数据是这样的

pk| dt_user_acess
01| 2014-10-21 08:30:12
02| 2014-10-21 08:40:27
03| 2014-10-21 08:56:18
04| 2014-10-21 09:15:19
05| 2014-10-21 10:42:48
...

结果我想要这样的事情(当所有的查询都没有污染代码时,不写这个案例)

'00:00 as 01:00' 0
'01:00 as 02:00' 0
'02:00 as 03:00' 0
'03:00 as 04:00' 0
'04:00 as 05:00' 0
'05:00 as 06:00' 0
'06:00 as 07:00' 0
'07:00 as 08:00' 0
'08:00 as 09:00' 3
'09:00 as 10:00' 1
'10:00 as 11:00' 1
'11:00 as 12:00' 0
'12:00 as 13:00' 0 
'13:00 as 14:00' 0 
'14:00 as 15:00' 0 
'15:00 as 16:00' 0 
'16:00 as 17:00' 0 
'17:00 as 18:00' 0 
'18:00 as 19:00' 0 
'19:00 as 20:00' 0 
'20:00 as 21:00' 0 
'21:00 as 22:00' 0 
'22:00 as 23:00' 0 
'23:00 as 23:59' 0 

2 个答案:

答案 0 :(得分:2)

  

这个查询有什么问题[?]

您的查询存在许多(或多或少严重)问题 - 无特定顺序:

  • CAST的正确语法为CAST(value AS type)
  • 您使用hour作为列名,但它是保留字
  • 您将字符串数字进行比较
  • 你似乎期待一个密集的&#34;结果集,但由于您没有使用连接,因此结果集中没有机会比原始表具有更多行(实际上可能更少,因为您{{1} })

鉴于您的需求,您可能错过了EXTRACT(HOUR FROM ...)功能。为了提取所有&#34;命中&#34;一个小时,你会写下:

GROUP BY

由于您需要SELECT EXTRACT(HOUR FROM dg_user_acess.dt_user_acess) as "hour" FROM dg_user_acess 它们(并且因为您不能在COUNT子句中使用别名),您可以使用子查询。不要过于担心:Oracle非常聪明,可以优化这种请求:

GROUP BY

最后,由于您需要密集输出(即:所有小时数在0-23范围内),我的选择是在生成的表上使用连接:

SELECT "hour", count(*) "cnt"
FROM (
    SELECT
      EXTRACT(HOUR FROM dg_user_acess.dt_user_acess) as "hour"        
    FROM dg_user_acess
) V
GROUP BY "hour";

所有这些都会导致相当复杂的嵌套查询。但这样做的好处是可以轻松地逐步编写,就像你刚才看到的那样。现在我展示了基本的构建模块,也许我们可以做得更好。也许那样:

SELECT "hour", NVL("cnt",0) "cnt"
FROM (
    SELECT LEVEL "hour" FROM DUAL
    CONNECT BY LEVEL < 24
) CLOCK
LEFT JOIN (
    SELECT "hour", count(*) "cnt"
    FROM (
        SELECT
          EXTRACT(HOUR FROM dg_user_acess.dt_user_acess) as "hour"        
        FROM dg_user_acess
    ) V
    GROUP BY "hour"
) G
USING ("hour")
ORDER BY "hour";

根据您的架构和数据,这可能会或可能不会比以前的版本执行得更好。您可以花些时间在http://sqlfiddle.com/#!4/678a5/17

上试验所有这些查询

答案 1 :(得分:0)

  

我得到一些结果改变组和案件的条件(并删除演员)。

     
    
      

我不知道这是否是最好的方法,但它有效

    
  
 SELECT 
  (
    CASE WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '00:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '01:00' ) THEN ('00:00 as 01:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '01:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '02:00' ) THEN ('01:00 as 02:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '02:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '03:00' ) THEN ('02:00 as 03:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '03:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '04:00' ) THEN ('03:00 as 04:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '04:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '05:00' ) THEN ('04:00 as 05:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '05:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '06:00' ) THEN ('05:00 as 06:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '06:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '07:00' ) THEN ('06:00 as 07:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '07:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '08:00' ) THEN ('07:00 as 08:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '08:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '09:00' ) THEN ('08:00 as 09:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '09:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '10:00' ) THEN ('09:00 as 10:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '10:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '11:00' ) THEN ('10:00 as 11:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '11:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '12:00' ) THEN ('11:00 as 12:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '12:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '13:00' ) THEN ('12:00 as 13:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '13:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '14:00' ) THEN ('13:00 as 14:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '14:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '15:00' ) THEN ('14:00 as 15:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '15:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '16:00' ) THEN ('15:00 as 16:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '16:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '17:00' ) THEN ('16:00 as 17:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '17:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '18:00' ) THEN ('17:00 as 18:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '18:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '19:00' ) THEN ('18:00 as 19:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '19:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '20:00' ) THEN ('19:00 as 20:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '20:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '21:00' ) THEN ('20:00 as 21:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '21:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '22:00' ) THEN ('21:00 as 22:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '22:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '23:00' ) THEN ('22:00 as 23:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '23:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '23:59' ) THEN ('23:00 as 23:59')         
        ELSE '0' END
    ) as hora,
    count(*) as total
  FROM dg_usuario_acesso  
  JOIN dg_usuario_sessao 
  ON dg_usuario_sessao.cd_usuario_sessao     = dg_usuario_acesso.cd_usuario_sessao
  WHERE dg_usuario_acesso.dt_usuario_acesso >= to_date('2014-09-19 00:00', 'YYYY-MM-DD HH24:Mi')
  AND dg_usuario_acesso.dt_usuario_acesso   <= to_date('2014-09-20 15:07', 'YYYY-MM-DD HH24:Mi')
  AND dg_usuario_sessao.cd_usuario NOT      IN (0,1,138)
  group by (CASE WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '00:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '01:00' ) THEN ('00:00 as 01:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '01:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '02:00' ) THEN ('01:00 as 02:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '02:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '03:00' ) THEN ('02:00 as 03:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '03:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '04:00' ) THEN ('03:00 as 04:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '04:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '05:00' ) THEN ('04:00 as 05:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '05:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '06:00' ) THEN ('05:00 as 06:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '06:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '07:00' ) THEN ('06:00 as 07:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '07:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '08:00' ) THEN ('07:00 as 08:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '08:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '09:00' ) THEN ('08:00 as 09:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '09:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '10:00' ) THEN ('09:00 as 10:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '10:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '11:00' ) THEN ('10:00 as 11:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '11:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '12:00' ) THEN ('11:00 as 12:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '12:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '13:00' ) THEN ('12:00 as 13:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '13:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '14:00' ) THEN ('13:00 as 14:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '14:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '15:00' ) THEN ('14:00 as 15:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '15:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '16:00' ) THEN ('15:00 as 16:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '16:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '17:00' ) THEN ('16:00 as 17:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '17:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '18:00' ) THEN ('17:00 as 18:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '18:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '19:00' ) THEN ('18:00 as 19:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '19:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '20:00' ) THEN ('19:00 as 20:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '20:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '21:00' ) THEN ('20:00 as 21:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '21:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '22:00' ) THEN ('21:00 as 22:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '22:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '23:00' ) THEN ('22:00 as 23:00')
         WHEN (TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') >= '23:00' AND TO_CHAR(dg_usuario_acesso.DT_USUARIO_ACESSO, 'HH24:Mi') < '23:59' ) THEN ('23:00 as 23:59')
        ELSE '0' END
    )
order by 1

我得到了我想要的结果

hour            Total hits
00:00 as 01:00  10
04:00 as 05:00  8
07:00 as 08:00  4
08:00 as 09:00  14
09:00 as 10:00  84
10:00 as 11:00  25
11:00 as 12:00  14
12:00 as 13:00  7
13:00 as 14:00  5
14:00 as 15:00  7
15:00 as 16:00  22
16:00 as 17:00  19
17:00 as 18:00  28
18:00 as 19:00  7
19:00 as 20:00  16
21:00 as 22:00  2
23:00 as 23:59  2