简体   繁体   中英

Count number of records logged by week - Oracle SQL

I know this is relatively simple but I can't find a solution within the archives although i see similar ones but my SQL isn't adequate enough to reverse engineer them.

All I am trying to do is count the number of calls raised within a team grouped by weeks from a call management table. Here is an example of some code which returns a count of 30,000 plus entries. I'd like to see that number divided down into weeks and also perhaps days based on a friday to friday range if possible?

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

The key fields are

logdate

Table name is

opencall

So I can then create a list of the number of calls logged by that suppgroup per week so

Week      Calls Logged
Week 1       134
Week 2       135
Week 3       189
...
...

So quite simple but I could then use this code to do searches on all sorts of different fields to identify some patterns.

All our date fields are VARCHAR2(20 BYTE)

I can group this count by day simply enough using

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

Hopefully someone can help.

Just remove closedby from the query:

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;

Note that I changed the date arithmetic. Presumably closedate is stored as a date/time value in the database. If so, you don't need to convert it to a string for comparisons.

For get count by week you must group by trunc(your_date, 'IW') , but if you want for friday to friday you must use next_day function and then group by trunc(NEXT_DAY(your_date,'FRIDAY'), 'IW') so, your query should be something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM