Getting the previous or next row in SQL Server (T-SQL)

Veröffentlicht am: 07 März 2024
auf dem Kanal: SQL Server 101
808
36

How can you get the next or the previous row in SQL Server?
My SQL Server Udemy courses are:
70-461, 70-761 Querying Microsoft SQL Server with T-SQL: https://rebrand.ly/querying-microsoft...
98-364: Database Fundamentals (Microsoft SQL Server): https://rebrand.ly/database-fundamentals
70-462 SQL Server Database Administration (DBA): https://rebrand.ly/sql-server-dba
Microsoft SQL Server Reporting Services (SSRS): https://rebrand.ly/sql-server-ssrs
SQL Server Integration Services (SSIS): https://rebrand.ly/sql-server-ssis
SQL Server Analysis Services (SSAS): https://rebrand.ly/sql-server-ssas-mdx
Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): https://rebrand.ly/microsoft-powerpiv...
----
You can use the LEAD (to get the next row) or LAG (to get the previous row). You also need an OVER which must include, at the very list, an ORDER BY.

You can also use a second argument to get the 2nd next row, like this:

SELECT object_id, [name]
, LEAD(name, 2) OVER (ORDER BY object_id) AS NextName
, LAG(name) OVER (ORDER BY object_id) AS PreviousName
FROM sys.objects
ORDER BY object_id
----
Links to my website are:
70-461, 70-761 Querying Microsoft SQL Server with T-SQL: http://idodata.com/querying-microsoft...
98-364: Database Fundamentals (Microsoft SQL Server): http://idodata.com/database-fundament...
SQL Server Essential in an Hour: http://idodata.com/sql-server-essenti...
70-462 SQL Server Database Administration (DBA): http://idodata.com/sql-server-databas...
DP-300: Administering Relational Databases: http://idodata.com/dp-300-administeri...
Microsoft SQL Server Reporting Services (SSRS): http://idodata.com/microsoft-sql-serv...
SQL Server Integration Services (SSIS): http://idodata.com/sql-server-integra...
SQL Server Analysis Services (SSAS): http://idodata.com/sql-server-ssas-mu...
Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): https://rebrand.ly/microsoft-powerpiv...
1Z0-071 Oracle SQL Developer – certified associate: http://idodata.com/iz0-071-oracle-sql...
SQL for Microsoft Access: http://idodata.com/sql-for-microsoft-...
DP-900: Microsoft Azure Data Fundamentals: http://idodata.com/dp-900-microsoft-a...


Auf dieser Seite können Sie das Online-Video Getting the previous or next row in SQL Server (T-SQL) mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer SQL Server 101 07 März 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 808 Mal angesehen und es wurde von 36 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!