Sean Lange (7/21/2014)
First you have to define what you mean by previous. I am guessing you mean InvoiceID? If you can post some consumable ddl and sample data I can help you figure this out. Please take a few minutes and read the first article in my signature.
OK so assuming that InvoiceID is what you would use to determine "previous" this should work.
with MySortedData as
(
select InvoiceID
, CustomerID
, Total
, ROW_NUMBER() over(partition by CustomerID order by InvoiceID) as RowNum
from #invoices
)
select s2.InvoiceID
, s2.CustomerID
, s2.Total
from MySortedData s1
inner join MySortedData s2 on s2.CustomerID = s1.CustomerID and s2.RowNum = s1.RowNum + 1
where s2.Total - s1.Total >= 10
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/