我们正在尝试从大量的样本中创建用于测试目的的样本。 具体的数据如下所示
CustomerId Customer_name PurchaseId Purchase date
1234 Robert Benson ABC123 12/07/2012
1218 Gary Thomas PP122 26/01/2013
1254 Robert Sharma PML563 27/04/2012
1218 Gary Thomas PR124 06/03/2013
1234 Benson Cruiso LML123 14/07/2012
1234 Martha Cruiso FMPL123 15/07/2012
我希望结果数据为:
GroupID CustomerId Customer_name PurchaseId Purchase date
1 1218 Gary Thomas PR124 06/03/2013
1 1218 Gary Thomas PP122 26/01/2013
2 1234 Robert Benson ABC123 12/07/2012
2 1234 Benson Cruiso LML123 14/07/2012
2 1234 Martha Cruiso FMPL123 15/07/2012
3 1254 Robert Sharma PML563 27/04/2012
基本上我想再增加一个列,它将基于一列来识别一组的行,即CustomerId。
答案 0 :(得分:0)
这是当前客户ID在唯一客户ID的排序列表中的位置。要获取排序列表中的位置,请计算有多少个较小的值:
SELECT (SELECT COUNT(*)
FROM (SELECT DISTINCT CustomerID
FROM MyTable) AS UniqueIDs
WHERE UniqueIDs.CustomerID <= MyTable.CustomerID
) AS GroupID,
*
FROM MyTable;