Sök…


Syntax

  • ROW_NUMBER ()
  • ÖVER ([PARTITION BY value_expression, ... [n]] order_by_clause)

Radnummer utan partitioner

Inkludera ett radnummer enligt den angivna ordningen.

SELECT
  ROW_NUMBER() OVER(ORDER BY Fname ASC) AS RowNumber,
  Fname,
  LName
FROM Employees

Radnummer med partitioner

Använder ett partitionskriterium för att gruppera radnumret enligt det.

SELECT
  ROW_NUMBER() OVER(PARTITION BY DepartmentId ORDER BY DepartmentId ASC) AS RowNumber,
  DepartmentId, Fname, LName
FROM Employees

Ta bort alla men sista posten (1 till många tabeller)

WITH cte AS (
  SELECT ProjectID,
         ROW_NUMBER() OVER (PARTITION BY ProjectID ORDER BY InsertDate DESC) AS rn
  FROM ProjectNotes
)
DELETE FROM cte WHERE rn > 1;


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow