I have just started diving in to SQL analytic functions and am a little hung up on FIRST_VALUE, maybe it is just specific to the example query from this site:
-- How many days after the first hire of each department were the next
-- employees hired?
SELECT empno, deptno, hiredate ? FIRST_VALUE(hiredate)
OVER (PARTITION BY deptno ORDER BY hiredate) DAY_GAP
FROM emp
WHERE deptno IN (20, 30)
ORDER BY deptno, DAY_GAP;
EMPNO DEPTNO DAY_GAP
---------- ---------- ----------
7369 20 0
7566 20 106
7902 20 351
7788 20 722
7876 20 756
7499 30 0
7521 30 2
7698 30 70
7844 30 200
7654 30 220
7900 30 286
I am struggling with two parts of this query. First, what is the '?' doing here? I have only seen it used for parameterized queries before. Second, how is the DAY_GAP actually being calculated? How does the query know to calculate the difference in days based on this query? Does it have something to do with the data type of hiredate?
First, what is the '?' doing here?
It's a mistake; it should be a -
(minus sign). I'm guessing that Basu copied-and-pasted his Oracle transcript into a program that silently converted the ASCII minus sign into a Unicode en dash, and from there into a program that silently converted non-ASCII characters into ?
.
Second, how is the DAY_GAP actually being calculated? How does the query know to calculate the difference in days based on this query? Does it have something to do with the data type of hiredate?
See previous; once you write it as hiredate - FIRST_VALUE(hiredate) OVER (...)
, it suddenly makes sense! :-)