2014-02-17 5 views
0

저는 Oracle SQL의 초보자입니다. 나는 티켓의 이름, 가까운 열린 티켓의 수와 날짜를 표시하려고 노력했다. 하지만 올바른 쿼리를 얻을 수 없습니다.하나의 열에 데이터를 그룹화하는 방법은 무엇입니까?

원하는 출력 :

01/20/2014, User management, 20, 15 

검색어 :

SELECT 'Data' 
||','||TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy') 
||','||q.name -- as ticket 
||','||NVL(o.CNT_OPENED,0) --as cnt_opened 
||','||NVL(c.CNT_CLOSED,0) --as cnt_closed 
FROM OWNER_DWH.DC_DATE d 
LEFT JOIN (
SELECT q.name, count(q.name), TRUNC(t.CREATE_TIME) as report_date, count(*) as cnt_opened 
FROM APP_ACCOUNT.OTRS_TICKET t 
left join app_account.otrs_queue q 
ON q.ID = t.QUEUE_ID 
WHERE t.CREATE_TIME BETWEEN SYSDATE -7 AND SYSDATE -1 
and t.queue_id not in (63, 61, 69, 59, 58, 60, 56, 64, 65, 23, 67, 68, 57) 
GROUP BY q.name, TRUNC(t.CREATE_TIME) 
) o ON d.DTIME_DAY=o.REPORT_DATE 
LEFT JOIN (
SELECT q.name, count(q.name), TRUNC(t.CLOSE_TIME) as report_date,count(*) AS cnt_closed 
FROM APP_ACCOUNT.OTRS_TICKET t 
left join app_account.otrs_queue q 
ON q.ID = t.QUEUE_ID 
WHERE t.CLOSE_TIME BETWEEN SYSDATE -7 AND SYSDATE -1 
and t.queue_id not in (63, 61, 69, 59, 58, 60, 56, 64, 65, 23, 67, 68, 57) 
GROUP BY q.name, TRUNC(t.CLOSE_TIME) 
) c ON D.DTIME_DAY=c.REPORT_DATE 
WHERE d.DTIME_DAY BETWEEN SYSDATE -7 AND TRUNC(SYSDATE) -1 
AND TRUNC(d.DTIME_DAY)= d.DTIME_DAY 
ORDER BY D.DTIME_DAY; 

내가 데이터를 그룹화 할 경우?

예 : 한 열에서의 내가 있다고 가정 해 봅시다 :

E-mail management 
E-mail management::Add user e-mail 
E-mail management::Add new Outlook distribution list 
E-mail management::Add new Outlook shared mailbox 
E-mail management::Add new SA e-mail (Zimbra account) 
POS::POS issue - need paper 
POS::POS issue - internet connection problems 

전자 메일 관리를 가지고 모든 이름을 것입니다 전자 메일 관리 및 POS로 POS 것 그룹이 모든 이름과 그룹.

욕망 출력 :

02/10/2014, E-mail, 4 
    02/10/2014, POS, 2 

어떻게 그렇게 할 수?

도와주세요. 미리 감사드립니다.

답변

0

이처럼 할 수있는 :

group by substr(col, 1, instr(col, ':') - 1) 

또는 물론

group by (case when col like '%:%' then substr(col, 1, instr(col, ':') - 1) 
       else col 
      end) 

, 당신은 수정해야합니다 :

group by (case when col like 'E-mail management:%' then 'E-mail management' 
       when col like 'pos:%' then 'pos' 
       else col 
      end) 

는 콜론 앞 부분에 의해 집계하기 select 절은 group by 절과 호환되어야합니다.

편집 :

select (case when col like '%:%' then substr(col, 1, instr(col, ':') - 1) 
      else col 
     end), count(*) 
from table t 
group by (case when col like '%:%' then substr(col, 1, instr(col, ':') - 1) 
       else col 
      end) 
+0

어떻게 내가 날짜 자체를 표시 할 수 있습니다

이 그룹의 수를 계산하려면, 당신은 같은 일을 할 것인가? – user3317540

+0

'min()'이나'list_agg()'와 같은 집계 함수를 사용해야합니다. –

+0

어떻게 할 수 있습니까? – user3317540