2017-01-27 3 views
0

날짜 목록에서 가장 많이 사용되는 시간을 추출하는 쿼리.다른 열로 확장되는 기본 동작을 피하는 STATS_MODE와 같은 Oracle 통계

표 : ID PARENT_ID 는 기간

이 꽤 잘 작동
select STATS_MODE(extract(HOUR from started)) as most_pop_call_start, 
     avg(duration) as avg_duration 
from table where parent_id = 'xxx'; 

을 시작했지만 우리가 같은 추출물 (시간)와 레코드가 경우에 STATS_MODE는 기본적 따라 최소 값을 취할 것입니다.

고유 한 결과가없는 가장자리의 경우 대신 기간을 사용하여 필터를 확장하고 싶습니다.

예. 그래서 계산이 시나리오에서

| **ID** | **PARENT_ID** | **STARTED** | **DURATION** | 
| test_01 | P_1 | 2017-01-12 10:21:53.000000 | 32 | 
| test_02 | P_1 | 2017-01-12 10:22:53.000000 | 50 | 
| test_03 | P_1 | 2017-01-12 11:23:53.000000 | 19 | 
| test_04 | P_1 | 2017-01-12 11:24:53.000000 | 39 | 
| test_05 | P_1 | 2017-01-12 12:25:53.000000 | 49 | 
| test_06 | P_1 | 2017-01-12 12:26:53.000000 | 59 | 
| test_07 | P_1 | 2017-01-12 13:27:53.000000 | 69 | 
| test_08 | P_1 | 2017-01-12 13:28:53.000000 | 79 | 
| test_09 | P_1 | 2017-01-12 14:29:53.000000 | 98 | 
| test_10 | P_1 | 2017-01-12 15:30:53.000000 | 99 | 

I는 most_pop_call_start 값 추출물 (HOUR)에 의한 최대 수 (*) 기 창 (2) 및 기록의 더 다음 1 개 세트 (2)를 가지고 있으므로 '13'로 가지고 싶은 하위 집합 (10, 11, 12, 13)의 최대 값 79를 취하는 지속 기간 열을 평가합니다.

+0

이봐, 무슨 일이야? 다른 사람이 오늘 정확히 같은 질문을 게시했습니다. 너 지금 시험 중이 니? – mathguy

+0

안녕하세요 @mathguy 실제로 스레드를 만들기 전에도 Sameer에게 나를 도울 것을 요청했습니다. 나는 그가 여기서도 물을 예정인지 몰랐다. – irotech

답변

0

시도 :

select min(hr) keep (dense_rank last order by cnt,max_duration) as most_pop_call_start, 
     sum(duration)/sum(cnt) as avg_duration 
from (
    select extract(HOUR from started) as hr, 
      count(*) as cnt, 
      sum(duration) as duration, 
      max(duration) as max_duration 
    from table 
    where parent_id = 'xxx' 
    group by extract(HOUR from started) 
); 
0

나는 krokodilko 제안에서 시작하여 해결했다. 이는 전체 쿼리의

을 :

SELECT tp.d_id , u.email , ta.email_subject AS headline , ta.start_date , ta.closing_date , tp.call_cost , SUM(tpc.duration) AS duration , COUNT(tpc.id) AS call_count , ta.currency 
    , (SELECT MIN(hr) KEEP (DENSE_RANK LAST ORDER BY cnt, max_duration, l_id) AS most_pop_call_start FROM (
     SELECT EXTRACT(HOUR FROM started) AS hr, count(*) AS cnt , SUM(duration) AS duration , MAX(duration) AS max_duration , phone_number_id AS l_id FROM table_phone_call GROUP BY EXTRACT(HOUR FROM started), phone_number_id 
    ) where l_id = tpc.phone_number_id 
    ) AS most_pop_call_start 
    --, STATS_MODE(extract(HOUR from tpc.started)) AS most_pop_call_start 
    , AVG(tpc.duration) AS avg_duration 
    , tb.name AS business_name 
FROM table_a ta 
    JOIN table_phone tp ON ta.id = tp.d_id 
    JOIN table_phone_call tpc ON tp.phone_id = tpc.phone_number_id 
    JOIN table_b tb ON ta.business_id = tb.id 
    JOIN users u ON tb.id = u.business_id 
WHERE phone_id IS NOT NULL 
     AND TRUNC(ta.closing_date) = to_date('#{jobParameters['dateCriteria']}', 'dd-mm-yyyy') 
GROUP BY tp.d_id , ta.email_subject , ta.start_date , ta.closing_date , tp.call_cost , tpc.phone_number_id , u.email , ta.currency , tb.name 

많은 감사