sql添加迭代列

时间:2018-07-08 21:21:25

标签: sql sql-server

我有这样的桌子

| Id | FirstName    | LastName  | UniqueNumber |
|----|--------------|-----------|--------------|
| 1  | Marie        | Roberts   |              |
| 2  | Howard       | Turner    |              |
| 3  | Larry        | Phillips  |              |
| 4  | Mildred      | Foster    |              |

有超过5万条记录,我需要根据名字的第一个字母和姓氏的第一个字母填充UniqueNumber列,此外,我需要添加特定的4位数字。例如,看看第一行玛丽·罗伯茨(Marie Roberts),UniqueNumber列应像这样填充:MR0001,但是当下一条记录将以相同的首字母开头时,数字应为MR0002。现在我有这样的查询:

DECLARE @Iterator int = 1
SELECT Id       
        ,FirstName
        ,LastName
        ,CONCAT(SUBSTRING(FirstName, 1,1), SUBSTRING(LastName, 2,1)) AS [Initials]
        ,LAG(CONCAT(SUBSTRING(FirstName, 1,1), SUBSTRING(LastName, 2,1))) OVER (order by FirstName, LastName) AS [PreviousInitials]
        ,CASE
            WHEN CONCAT(SUBSTRING(FirstName, 1,1), SUBSTRING(LastName, 2,1)) = LAG(CONCAT(SUBSTRING(FirstName, 1,1), SUBSTRING(LastName, 2,1))) OVER (order by FirstName, LastName) THEN @Iterator+1
            ELSE 1
        END as [TempNumber]
  FROM [TEST].[dbo].[Person] 
  order by FirstName, LastName

结果看起来是

| Id    | FirstName | LastName  | Initials      | PreviousInitials      | TempNumber
-----------------------------------------------------------------------------------------
| 94    | Aaron     | Bailey    | AB            | NULL                  | 1
| 193   | Aaron     | Bailey    | AB            | AB                    | 2
| 292   | Aaron     | Bailey    | AB            | AB                    | 2
| 391   | Aaron     | Bailey    | AB            | AB                    | 2
| 490   | Aaron     | Bailey    | AB            | AB                    | 2
| 589   | Aaron     | Bailey    | AB            | AB                    | 2
| 688   | Aaron     | Bailey    | AB            | AB                    | 2
| 787   | Aaron     | Bailey    | AB            | AB                    | 2
| 886   | Aaron     | Bailey    | AB            | AB                    | 2
| 985   | Aaron     | Bailey    | AB            | AB                    | 2
| 938   | Adam      | Russell   | AR            | AB                    | 1
| 740   | Adam      | Russell   | AR            | AR                    | 2

问题出现在case语句中,我无法递增@Iterator变量。

2 个答案:

答案 0 :(得分:1)

您将使用row_number()

select p.*,
       (left(firstname) + left(lastname) +
        right('0000' +
              convert(varchar(255),
                      row_number() over (partition by left(firstname) + left(lastname) order by id
                     ), 4
              )
       ) as uniquenumber
from [TEST].[dbo].[Person] p

答案 1 :(得分:0)

谢谢,最初此解决方案不起作用,但是当我按firstName,lastName末尾添加订单时,它按预期工作了:

  select p.*,
       (left(p.FirstName, 1) + SUBSTRING(LastName, 2,1) +
        right(('0000' +
              convert(varchar(255),
                      row_number() over (partition by left(firstname, 4) + left(lastname, 4) order by id
                     ), 4
              )), 4
       )) as uniqueUi
from [TEST].[dbo].[Person] p
order by p.FirstName, p.LastName