I am normally working with MS SQL. I have a piece of code which I might have to translate to Oracle SQL. In this piece of code I update two variables for each row in order to find the max value of concurrent users.
My question is if it is possible to do the same as what is being done in the MS SQL code below in Oracle?
-- The creation of this test table is not relevant to my question.
-- It is just there to make a working example
SELECT {ts '2012-09-03 10:12:00'} AS eventTime, 1 AS change
INTO #EVENTS
UNION ALL SELECT {ts '2012-09-03 10:24:00'}, 1
UNION ALL SELECT {ts '2012-09-03 10:25:00'}, 1
UNION ALL SELECT {ts '2012-09-03 11:02:00'}, -1
UNION ALL SELECT {ts '2012-09-03 11:25:00'}, 1
UNION ALL SELECT {ts '2012-09-03 11:34:00'}, 1
UNION ALL SELECT {ts '2012-09-03 12:15:00'}, -1
UNION ALL SELECT {ts '2012-09-03 13:50:00'}, -1
UNION ALL SELECT {ts '2012-09-03 14:20:00'}, -1
UNION ALL SELECT {ts '2012-09-03 14:44:00'}, -1
DECLARE @count int, @maxUsers int
SET @count = 0
SET @maxUsers = 0
-- This part is what is in question. Can you do something similar in Oracle?
-- Having calculation done for each row and store it in a variable for
-- the next row.
SELECT
@count = @count + change,
@maxUsers = CASE WHEN @count > @maxUsers THEN @count ELSE @maxUsers END
FROM #EVENTS
ORDER BY eventTime ASC
PRINT @maxUsers -- Prints the value 4
DROP TABLE #EVENTS