2012-02-15 89 views
0

我想顯示今年每月的總訪問量。但以下查詢返回缺少表達式的錯誤代碼。我錯過了什麼?顯示今年每月的訪問量

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應用程序構建器創建一個應用程序。但在創建頁面並想要編輯表單中的記錄後,會顯示一條錯誤消息,指出可以提取數據。這是什麼意思?我錯過了什麼步驟?

+2

這是一個查詢? – 2012-02-15 08:37:41

+0

我對Oracle的經驗幾乎沒有,但據我瞭解,您可以使用'TO_CHAR(visit_time,'MONTH')'而不是這個繁瑣的案例陳述? – GarethD 2012-02-15 08:47:49

+0

對於Oracle Application Builder中的額外問題,您可能需要單獨提問,並提供有關您獲得的錯誤的更多詳細信息。 – 2012-02-15 09:25:14

回答

1

不甲骨文有一個函數從這樣的日期提取MONTHNAME:

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') 
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'); 

缺少表達錯誤將有來自將函數中的to_date前面的關鍵字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 
+0

我沒有提到的是visit_time是一個時間戳 – jannyeu 2012-02-15 11:10:31

+0

沒關係,'to_char','to_date','extract'等等都適用於'timestamp'類型。 – 2012-02-15 11:46:41