用名称代替代码

我有一个写出来的SQL报告,我正在拉入一个Excel工作簿(因此多个标签)与患者信息,我想过滤到其他值。 例如我的工作簿看起来像这样:

Name Acct # Date Shmo,Joe 12345 1/1/15 Shmo,Joe 49738 1/2/15 Shmo,Joe 19725 2/1/15 Smith,Jane 59785 1/5/15 Smith,Jane 36740 3/2/15 

到目前为止,这一切都很好,当他们希望过滤这些数据来“移除”患者姓名,但保留多个账户与患者相关联。 例如,我想要一段将改变上面的代码:

 Name Acct # Date Patient 1 12345 1/1/15 Patient 1 49738 1/2/15 Patient 1 19725 2/1/15 Patient 2 59785 1/5/15 Patient 2 36740 3/2/15 

我希望如果可以在SQL或Excel中完成。 即使只有前面没有“病人”的唯一编号(1,2等)也会很好。

回答Excel。 这会在D.中添加一个新列。如有必要,请更改为不同的列。

 Sub Zach() Dim lastRow As Long lastRow = WorkSheets("Sheet6").Cells(WorkSheets("Sheet6").Rows.Count, "A").End(xlUp).Row 'change sheet n = 1 S = 2 For i = 2 To lastRow If Cells(i + 1, 1) <> Cells(i, 1) Then Range(Cells(S, 4), Cells(i, 4)).Value = "Patient " & n n = n + 1 S = i + 1 End If Next End Sub 

如果您使用的是Sql Server,那么以下代码将提供所需的别名:

 concat('Patient ',DENSE_RANK() over(order by name)) 

如果你的DBMS支持窗口集合函数,你可以使用DENSE_RANK,如:

 'Patient ' || CAST(DENSE_RANK() OVER (ORDER BY Name) AS VARCHAR(10))