Question

I'm newbie in Oracle SQL. I tried to display Name of tickets, No. of close and open tickets and the date. But I can't get the right query.

Desired output:

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

Query:

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;

And if I want to group the data?

Example: In one column, let's say I have:

 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

All name that has E-mail management will group as E-mail management and all name that has POS will group as POS.

Desires output:

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

How to do that?

Please help me. Thank you in advance.

Was it helpful?

Solution

You could do it like this:

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

To aggregate by the part before the colon:

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

or

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

Of course, you will have to fix the select clause to be compatible with the group by clause.

EDIT:

To count the number in the groups, you would do something like:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top