SELECT
User
,Date
,Count(*) AS Jobs
-- new jobs today
,Sum(firstdate) AS Incoming
-- finished jobs today
,Sum(lastdate)
-- finished jobs the day before
,Lag(Sum(lastdate),1,0) Over (PARTITION BY User ORDER BY Date) AS Outgoing
FROM
(
SELECT
User
,Job
,Date
-- flag indicating job is present on the current day but absent the day before
,CASE WHEN Date = Lag(Date) Over (PARTITION BY User, job ORDER BY Date) + 1 THEN 0 ELSE 1 END AS firstdate
-- flag indicating job is present on the current day but absent the day after
,CASE WHEN Date = Lead(Date) Over (PARTITION BY User, job ORDER BY Date) - 1 THEN 0 ELSE 1 END AS lastdate
FROM your_table
-- to remove duplicate rows add
-- GROUP BY 1,2,3
) AS dt
GROUP BY 1,2
ORDER BY 1,2
Если ваша версия Teradata не поддерживает LAG / LEAD (т. Е. <16.10), вы должны переписать ее: </p>
SELECT
User
,Date
,Count(*) AS Jobs
-- new jobs today
,Sum(firstdate) AS Incoming
-- finished jobs today
,Sum(lastdate)
-- finished jobs the day before
,Coalesce(Min(Sum(lastdate)) Over (PARTITION BY User ORDER BY Date ROWS BETWEEN 1 Preceding AND 1 Preceding), 0) AS Outgoing
FROM
(
SELECT
User
,Job
,Date
-- flag indicating job is present on the current day but absent the day before
,CASE WHEN Date = Min(Date) Over (PARTITION BY User, job ORDER BY Date ROWS BETWEEN 1 Preceding AND 1 Preceding) + 1 THEN 0 ELSE 1 END AS firstdate
-- flag indicating job is present on the current day but absent the day after
,CASE WHEN Date = Min(Date) Over (PARTITION BY User, job ORDER BY Date ROWS BETWEEN 1 Following AND 1 Following ) - 1 THEN 0 ELSE 1 END AS lastdate
FROM your_table
-- to remove duplicate rows add
-- GROUP BY 1,2,3
) AS dt
GROUP BY 1,2
ORDER BY 1,2