I am looking for some guidance on an Oracle SQL Query I have been struggling with.
I have a data table that contains Time Stamps and Help-Desk Ticket numbers...
Every time a technician makes a change to the ticket or logs a note the corresponding time stamp will log in the database along with the ticket number. What i am trying to accomplish is to count the first occurrence of the ticket number in each series and i cant use distinct because it will not count the ticket again if the technician works the ticket again later in the day. Please reference the attached picture.
http://s13.postimg.org/i5gx4l4nb/sample.jpg
Using the lag function i can flag the tickets at the row level but i cant add this to a SUM clause to accomplish my ultimate goal.
CASE WHEN LAG(IPCS_AUDIT_HISTORY.ENTITYID,1) over (ORDER BY IPCS_AUDIT_HISTORY.HISTORYDATE)=IPCS_AUDIT_HISTORY.ENTITYID THEN 0 ELSE 1 END
Any ideas or thoughts on what direction to take this in?
SELECT
UPPER(IPCS_PLANNER.PLANNERSURNAME || ', ' || IPCS_PLANNER.PLANNERFIRSTNAME) AS FULL_NAME,
COUNT(DISTINCT IPCS_AUDIT_HISTORY.ENTITYID) AS TICKETS_COUNT,
SUM(CASE WHEN LAG(IPCS_AUDIT_HISTORY.ENTITYID,1) over (ORDER BY IPCS_AUDIT_HISTORY.HISTORYDATE)=IPCS_AUDIT_HISTORY.ENTITYID THEN 0 ELSE 1 END) AS TOUCH_COUNT
FROM
IPCS_AUDIT_HISTORY,
IPCS_PLANNER
WHERE
IPCS_AUDIT_HISTORY.PLANNERID=IPCS_PLANNER.PLANNERID and
TRUNC(IPCS_AUDIT_HISTORY.HISTORYDATE, 'IW')='30-DEC-13'
GROUP BY
UPPER(IPCS_PLANNER.PLANNERSURNAME || ', ' || IPCS_PLANNER.PLANNERFIRSTNAME)