I have a table that has records in it for customers and the days they shopped. The fields are
Customer
DayShopped
Spend
I want to sort the table by customer, then by date and loop through the data adding a field that is the number of days between shopping trips for each customer. Obviously for the first shopping trip of each customer there's no calcualtion to do.
I've done a similar thing in Access using VBA and loops and recordsets, but I want to do it with a stored procedure in SQL Server. Am I correct in assuming that it would be a similar sort of process to;
1) Create a record set with the required sorts applied
2) Loop through the data comparing each record to the last and carrying out the calculataion
3) Insert each record into a new table
Any help with the code would be great. Pointers to web sites covering the right topics would be enough. I've tried searching in google for sql server loops, but there's a lot of junk to wade through.
Thanks.
Customer
DayShopped
Spend
I want to sort the table by customer, then by date and loop through the data adding a field that is the number of days between shopping trips for each customer. Obviously for the first shopping trip of each customer there's no calcualtion to do.
I've done a similar thing in Access using VBA and loops and recordsets, but I want to do it with a stored procedure in SQL Server. Am I correct in assuming that it would be a similar sort of process to;
1) Create a record set with the required sorts applied
2) Loop through the data comparing each record to the last and carrying out the calculataion
3) Insert each record into a new table
Any help with the code would be great. Pointers to web sites covering the right topics would be enough. I've tried searching in google for sql server loops, but there's a lot of junk to wade through.
Thanks.