2014-12-03 204 views
1

我知道這是相對簡單的,但我無法在檔案中找到解決方案,雖然我看到類似的但我的SQL不足以對它們進行逆向工程。每週記錄的記錄數 - Oracle SQL

我所要做的就是統計由呼叫管理表按星期分組的呼叫次數。下面是一些代碼的例子,它返回了30,000個條目的計數。如果可能的話,我希望看到這個數字劃分爲幾個星期,也許基於星期五到星期五的範圍內的天數?

 
select COUNT(*) from opencall 
where 
trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) BETWEEN '01-JAN-14' AND '31-DEC-14' 

的關鍵領域是

 
logdate 

表名是

 
opencall 

所以我可以創建由每週所以

是suppgroup記錄的電話號碼列表
 
Week  Calls Logged 
Week 1  134 
Week 2  135 
Week 3  189 
... 
... 

所以非常簡單,但我可以使用此代碼在各種不同的字段上執行搜索以識別某些模式。

我們所有的日期字段是VARCHAR2(20字節)

我這組由數天根本不夠用

 
    select COUNT(*), trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'))
from opencall where trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) BETWEEN '01-JAN-14' AND '31-DEC-14' group by trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) order by trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'))

希望有人可以提供幫助。

回答

0

爲了獲取計數的一週,你必須通過trunc(your_date, 'IW')組,但如果你想爲週五至週五必須使用next_day功能,然後通過trunc(NEXT_DAY(your_date,'FRIDAY'), 'IW')組,這樣,您的查詢應該是這樣的:

select 
'WEEK '|| row_number() over(order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'FRIDAY'), 'IW')) WEEK, 
count(*) Calls Logged 
from opencall 
where trunc(to_date(substr(LOGDATE,1,10), 'DD-MM-YY')) BETWEEN '01-JAN-14' AND '31-DEC-14' 
group by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'FRIDAY'), 'IW') 
order by trunc(NEXT_DAY(to_date(substr(LOGDATE,1,10), 'DD-MM-YY'),'FRIDAY'), 'IW'); 
+0

卓越而輝煌的解決方案。我可以將此作爲現在過濾各種字段的衆多呼叫的基礎。非常感謝。 – 2014-12-08 11:35:26

0

只是從查詢中刪除closedby

with closed_calls as (
     select to_number(to_char(closedate, 'IW') as week_number, oc.* 
     from opencall oc 
     where ((status > 15 or status = 6) and 
       closedby in ('analyst1', 'analyst2', 'analyst3', 'analyst4', 'analyst5', 'analyst6', 'analyst7') and 
       trunc(closeddate) between date '2014-10-01' and date '2014-12-31' 
    ) 
select week_number as "Week number", count(*) as "Calls closed" 
from closed_calls 
group by week_number 
order by week_number desc; 

注意,我改變了日期運算。推測closedate作爲日期/時間值存儲在數據庫中。如果是這樣,您不需要將其轉換爲字符串進行比較。

+0

嘿在那裏,謝謝你的貢獻。這似乎在抱怨右括號缺失。我嘗試添加一個.... ORA-00907:缺少右括號 00907. 00000 - 「缺少右括號」 *原因: *操作: 錯誤在行:2列:50 – 2014-12-03 12:21:31

+0

closedate實際上是一個VARCHAR2 (20 BYTE) – 2014-12-03 12:24:46