我想显示今年每月的总访问量。但是以下查询返回缺少表达式的错误代码。我错过了什么?
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应用程序构建器创建应用程序。但是在我创建页面并想要编辑表单中的记录之后,会显示一条错误消息,表明可以获取数据。这是什么意思?我错过了任何步骤?
答案 0 :(得分:4)
此查询都可以缩减为:
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');
缺少表达式错误的原因是将关键字date
放在to_date
函数的extract
前面。这是不必要的。你可以使用:
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
答案 1 :(得分:1)
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')