显示今年每月的访问次数

Posted

技术标签:

【中文标题】显示今年每月的访问次数【英文标题】:display number of visits per month this year 【发布时间】:2012-02-15 08:36:20 【问题描述】:

我想显示今年每月的总访问次数。但以下查询返回缺少表达式的错误代码。我错过了什么?

 SELECT CASE 
     WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '),     'mm/dd/yyyy')  = 1 THEN 'January '
     WHEN extract(month from date  TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '),   'mm/dd/yyyy')  = 2 THEN 'February'
WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 3 THEN 'March'
WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 4 THEN 'April'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 5 THEN 'May'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 6 THEN 'June'
WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 7 THEN 'July'
WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 8 THEN 'August'
WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 9 THEN 'September'
WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 10 THEN 'October'   
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 11 THEN 'November'   
 ELSE 'December'
    END AS "Month " , 
    COUNT(*) AS "Number of visits"
 FROM allrecord where  extract(year from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')= extract(year from date sysdate) 
 GROUP BY CASE 
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 1 THEN 'January '
     WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 2 THEN 'February'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 3 THEN 'March'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 4 THEN 'April'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 5 THEN 'May'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 6 THEN 'June'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 7 THEN 'July'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 8 THEN 'August'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 9 THEN 'September'
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 10 THEN 'October'   
 WHEN extract(month from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')  = 11 THEN 'November'   

       ELSE 'December'
          END;

还有一个问题要问,我正在使用 ORacle 应用程序生成器创建一个应用程序。但是在我创建了一个页面并想编辑表单中的记录后,一条错误消息显示可以获取数据。这是什么意思?我错过了什么步骤?

【问题讨论】:

我几乎没有使用 Oracle 的经验,但据我了解,您可以使用 TO_CHAR(visit_time, 'MONTH') 代替这种繁琐的案例陈述吗? 关于 Oracle Application Builder 的额外问题,您可能需要单独提问,并提供有关您遇到的错误的更多详细信息。 【参考方案1】:

这个查询可以全部浓缩为:

  select to_char(VISIT_TIME, 'fmMonth') as "Month"
        ,count(*) as "Number of Visits"
    from ALLRECORD
   where extract(year from VISIT_TIME) = extract(year from sysdate)
group by to_char(VISIT_TIME, 'fmMonth');

extract 函数中将关键字date 放在to_date 前面会导致缺少表达式错误。这是不必要的。您可以使用:

select extract (year from date '2011-01-01') from sys.dual

或:

select extract (year from to_date('2011-01-01')) from sys.dual

但从来没有:

select extract (year from date to_date('2011-01-01')) from sys.dual

【讨论】:

我没有提到的是 visit_time 是一个时间戳 没关系,to_charto_dateextract 等都适用于 timestamp 类型。【参考方案2】:

Oracle 没有从这样的日期中提取月份名称的功能吗:

SELECT TO_CHAR(visit_time 'MONTH') as month
COUNT(*) AS "Number of visits"
FROM allrecord 
WHERE  extract(year from date TO_DATE (TO_CHAR (visit_time, 'mm/dd/yyyy '), 'mm/dd/yyyy')= extract(year from date sysdate) 
GROUP BY TO_CHAR(visit_time 'MONTH')

【讨论】:

以上是关于显示今年每月的访问次数的主要内容,如果未能解决你的问题,请参考以下文章

Google Remarketing Network List 300+ 每天 Google Remarketing 展示次数 10 每月?

awstats日志分析

SVN统计开发人员提交次数

python统计apachenginx访问日志IP访问次数并且排序(显示前20条)

让 Wordpress 显示每月存档的代码?

每月信用卡付款计算器显示语法错误