Subject Re: [Firebird-Architect] Improving CTE with ANSI SQL2003 Window Functions
Author Philippe Makowski
Adriano dos Santos Fernandes [08-04-08 18.58] :
> Do you have a sample query using WINDOW clause?
>
> I suppose it's like CTE WITH, but I never see one...
>
> I know only the syntax:
> select n, count(*) over ()
> from table
>
yes that's it

for example :

Calculating Median Value per Employee by Using a CTE and ROW_NUMBER

WITH OrdersRN AS
(
SELECT EmployeeID, Value,
ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY Value) AS RowNum,
COUNT(*) OVER(PARTITION BY EmployeeID) AS Cnt
FROM VOrders
)
SELECT EmployeeID, AVG(Value) AS Median
FROM OrdersRN
WHERE RowNum IN((Cnt + 1) / 2, (Cnt + 2) / 2)
GROUP BY EmployeeID;