我正在寻找有关我一直在努力的Oracle SQL查询的一些指导。
我有一个数据表,其中包含时间戳和帮助台票号…
每次技术人员对票证进行更改或记录便笺时,相应的时间戳将与票证编号一起登录到数据库中。我要完成的工作是计算每个系列中票号的首次出现,并且我不能使用distinct,因为如果技术人员在当天晚些时候再次使用票号,它将不会再次计算票号。请参考所附图片。
http://s13.postimg.org/i5gx4l4nb/sample.jpg
使用滞后函数,我可以在行级别标记票证,但不能将其添加到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
关于采取什么方向的任何想法或想法?
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)
更新:以 这种方式尝试
SELECT full_name, COUNT(DISTINCT entityid) ticket_count, COUNT(DISTINCT series) - COUNT(DISTINCT entityid) touch_count FROM ( SELECT plannerid, full_name, entityid, rnum - ROW_NUMBER() OVER (PARTITION BY plannerid, entityid ORDER BY rnum) series FROM ( SELECT p.plannerid, UPPER(p.plannersurname || ', ' || p.plannerfirstname) full_name, h.historydate, h.entityid, ROW_NUMBER() OVER (PARTITION BY p.plannerid ORDER BY h.historydate) rnum FROM ipcs_audit_history h JOIN ipcs_planner p ON h.plannerid = p.plannerid WHERE h.historydate >= TO_DATE('30-DEC-13') AND h.historydate < TO_DATE('30-DEC-13') + 1 ) q ) p GROUP BY plannerid, full_name
样本输出:
| FULL_NAME | TICKET_COUNT | TOUCH_COUNT | | ----------- | -------------- || ------------- | | 美国能源部,约翰| 4 | 1 |
这是 SQLFiddle 演示