C
Cameron
I am going back to a problem called "text then date ascending count" I tried
to address almost a year ago, here is the link
http://www.microsoft.com/office/com...4faf40-ff27-4cde-b94a-ed679a274fb0&sloc=en-us
Every solution has given me a different type of mis-sorting of the list. I
am trying to sort a list of sales records so that I can create a field
PurchaseRank. Each Sale has an alphanumeric customerID, PurchaseDate, and
PurchaseNumber. A Recordset could look like this:
customerID PurchaseDate PurchaseNumber
A10 7/29/2002 101
A10 8/20/2002 221
B20 3/19/2009 420
B20 4/23/2009 403
C30 6/15/2006 339
C30 6/15/2006 331
D40 7/22/2002 7671
D40 10/18/2004 20708
There are several issues creating a Purchase Rank for the set. One problem
is that the PrimaryKey PurchaseNumber isn't necessary in ascending order
based on
the PurchaeDate. This is because a PurchaseNumber can be placed on hold
once created until it is finalized, this is the case for customerID B12.
Another issue is there can be more than one purchase on the same day. In
this case I don't care which one is ranked first or second.
This is so simple to do for me in excel, I just sort by CustomerID and then
by PurchaseDate, then I use a countif formula to get the rank.
Example 1 that fails to Rank CustomerID B20 correctly:
SELECT T.customerID, T.PurchaseDate, (SELECT COUNT(*)
FROM [Sales] T1
WHERE T1.customerID = T.customerID AND T1.PurchaseNumber <=
T.PurchaseNumber) AS PurchaseRank, T.PurchaseNumber
FROM Sales AS T
ORDER BY T.customerID, T.PurchaseDate;
Results:
customerID PurchaseDate PurchaseNumber PurchaseRank
A10 7/29/2002 101 1
A10 8/20/2002 221 2
B20 3/19/2009 420 2
B20 4/23/2009 403 1
C30 6/15/2006 339 2
C30 6/15/2006 331 1
D40 7/22/2002 7671 1
D40 10/18/2004 20708 2
Example 2 that fails to Rank CustomerID B20 correctly:
SELECT T.customerID, T.PurchaseDate, (SELECT COUNT(*)
FROM [Sales] T1
WHERE T1.customerID = T.customerID AND T1.PurchaseNumber <=
T.PurchaseNumber) AS PurchaseRank, T.PurchaseNumber
FROM Sales AS T
ORDER BY T.customerID, T.PurchaseDate;
Results:
customerID PurchaseDate PurchaseRank PurchaseNumber
A10 7/29/2002 1 101
A10 8/20/2002 2 221
B20 3/19/2009 2 420
B20 4/23/2009 1 403
C30 6/15/2006 2 339
C30 6/15/2006 1 331
D40 7/22/2002 1 7671
D40 10/18/2004 2 20708
If anyone knows how to make this work, or a different way to do this, please
let me know. Thanks
to address almost a year ago, here is the link
http://www.microsoft.com/office/com...4faf40-ff27-4cde-b94a-ed679a274fb0&sloc=en-us
Every solution has given me a different type of mis-sorting of the list. I
am trying to sort a list of sales records so that I can create a field
PurchaseRank. Each Sale has an alphanumeric customerID, PurchaseDate, and
PurchaseNumber. A Recordset could look like this:
customerID PurchaseDate PurchaseNumber
A10 7/29/2002 101
A10 8/20/2002 221
B20 3/19/2009 420
B20 4/23/2009 403
C30 6/15/2006 339
C30 6/15/2006 331
D40 7/22/2002 7671
D40 10/18/2004 20708
There are several issues creating a Purchase Rank for the set. One problem
is that the PrimaryKey PurchaseNumber isn't necessary in ascending order
based on
the PurchaeDate. This is because a PurchaseNumber can be placed on hold
once created until it is finalized, this is the case for customerID B12.
Another issue is there can be more than one purchase on the same day. In
this case I don't care which one is ranked first or second.
This is so simple to do for me in excel, I just sort by CustomerID and then
by PurchaseDate, then I use a countif formula to get the rank.
Example 1 that fails to Rank CustomerID B20 correctly:
SELECT T.customerID, T.PurchaseDate, (SELECT COUNT(*)
FROM [Sales] T1
WHERE T1.customerID = T.customerID AND T1.PurchaseNumber <=
T.PurchaseNumber) AS PurchaseRank, T.PurchaseNumber
FROM Sales AS T
ORDER BY T.customerID, T.PurchaseDate;
Results:
customerID PurchaseDate PurchaseNumber PurchaseRank
A10 7/29/2002 101 1
A10 8/20/2002 221 2
B20 3/19/2009 420 2
B20 4/23/2009 403 1
C30 6/15/2006 339 2
C30 6/15/2006 331 1
D40 7/22/2002 7671 1
D40 10/18/2004 20708 2
Example 2 that fails to Rank CustomerID B20 correctly:
SELECT T.customerID, T.PurchaseDate, (SELECT COUNT(*)
FROM [Sales] T1
WHERE T1.customerID = T.customerID AND T1.PurchaseNumber <=
T.PurchaseNumber) AS PurchaseRank, T.PurchaseNumber
FROM Sales AS T
ORDER BY T.customerID, T.PurchaseDate;
Results:
customerID PurchaseDate PurchaseRank PurchaseNumber
A10 7/29/2002 1 101
A10 8/20/2002 2 221
B20 3/19/2009 2 420
B20 4/23/2009 1 403
C30 6/15/2006 2 339
C30 6/15/2006 1 331
D40 7/22/2002 1 7671
D40 10/18/2004 2 20708
If anyone knows how to make this work, or a different way to do this, please
let me know. Thanks