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