Skip to content
Advertisement

Want the count how many times row changes from -1 to 1 or vice-versa in SQL Server

SQL query to display the count, how many times row changes from -1 to 1 or vice-versa in SQL Server in a table.

Let the table name be A

Advertisement

Answer

For this question to have an answer, you need a column that specifies the ordering. SQL tables represent unordered (multi)sets. There is no inherent ordering. To answer your question, you can use lag() and conditional aggregation:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement