问题是,我们需要在给定行之前和之后获得行的总数由主键标识)。 " s/ N8 r! \/ \& r( ?& E我曾在T-SQL(MSSQL 2008)尝试了以下方法。它给出了正确的结果,但我不知道这是否是最好的方法。 ! @5 X7 l4 D4 L;WITH cte_before AS SELECT ROW_NUMBER() OVER ( Order By CustomerId ) [Row Number],customerid, firstName FROM SalesLT.Customer ),cte_nums AS SELECT ROW_NUMBER() OVER ( Order By CustomerId ) [Row Number FROM SalesLT.Customer)SELECT [Row Number]-1 [before],MAX([Row Number1]) - [Row Number] CustomerID,FirstName FROM cte_nums,cte_beforeGROUP BY [Row Number],CustomerID,FirstNameHAVING CustomerID = 55怎样才能在这里?T-SQL以及如何在其他方面改进它SQL例如,方言和服务器Oracle,MySQL,sqlite,FireBird等)完成它?$ \* e+ V/ w2 t; r
+ i Q/ ]4 g6 }; R# l! b解决方案: / |( c5 r* T! k& `) D5 F- \ 我认为这适用于任何方言: * x3 o1 ]( B( S M% Gselect (select count(*) from SalesLT.Customer where customerid 55) as rows_after, CustomerID,FirstNamefrom SalesLT.Customerwhere CustomerID = 55