来自2列的重复数据的计数器增量

时间:2017-09-19 06:40:29

标签: sql sql-server-2012 sql-server-2008-r2

我在表中有2列:column1,priority。
他们的数据如下所示。

+--------------------------+
|code | column1 | Priority |
+--------------------------+
| 1001|  1      | 1        |
| 1001|  2      | 1        |
| 1002|  1      | 2        |
| 1002|  2      | 2        |
| 1003|  1      | 3        |
| 1004|  2      | 4        |
| 1005|  1      | 5        |
| 1005|  2      | 5        |
| 1006|  1      | 5        |
| 1006|  2      | 5        |
| 1007|  1      | 5        |
| 1007|  2      | 5        |
+--------------------------+

如果我按

连接这两列
 select 'con' + column1 + Priority 
 from T1

我得到以下结果:

+---------------------------------------------+
|code | column1 | Priority | (No column name) |
+--------------------------+------------------+
| 1001|  1      | 1        | con11            |
| 1001|  2      | 1        | con21            |
| 1002|  1      | 2        | con12            |
| 1002|  2      | 2        | con22            |
| 1003|  1      | 3        | con13            |
| 1004|  2      | 4        | con24            |
| 1005|  1      | 5        | con15            |
| 1005|  2      | 5        | con25            |
| 1006|  1      | 5        | con15            |
| 1006|  2      | 5        | con25            |
| 1007|  1      | 5        | con15            |
| 1007|  2      | 5        | con25            |
+--------------------------+------------------+

但我要求的价值如下: 比如连接值已经存在而不是下一个优先级列值中的增量,而不是连接它与column1。 e:g如果已经存在15而不是优先级值的增量使其为6而不是连接,并且对于25到26则获得16相同。

+---------------------------------------------+
|code | column1 | Priority | (No column name) |
+--------------------------+------------------+
| 1001|  1      | 1        | con11            |
| 1001|  2      | 1        | con21            |
| 1002|  1      | 2        | con12            |
| 1002|  2      | 2        | con22            |
| 1003|  1      | 3        | con13            |
| 1004|  2      | 4        | con24            |
| 1005|  1      | 5        | con15            |
| 1005|  2      | 5        | con25            |
| 1006|  1      | 5        | con16            |
| 1006|  2      | 5        | con26            |
| 1007|  1      | 5        | con17            |
| 1007|  2      | 5        | con27            |
+--------------------------+------------------+

2 个答案:

答案 0 :(得分:1)

好的,我有一个只在某些情况下有效的解决方案(例如您的样本数据)。

request

当您将select column1, priority, 'con' + CAST(column1 AS VARCHAR(10)) + CAST((row_number() over (partition by column1, priority order by priority) + priority - 1) AS VARCHAR(10)) from tab 插入数据时会出现问题,那么您将有两次con16。 Here是有问题数据的解决方案示例。

答案 1 :(得分:1)

试试这个

select code,column1, Priority,  
   concat('con', column1, Priority - 1 + row_number() over(partition by Priority, column1 order by code))
from myTable 
order by code, column1;

这将返回一行

1007    3   5   con35

何时

1007    3   5

添加到myTable。不知道它是否正确。

编辑如果您的数据库兼容级别为2008或更低,则使用

'con' + cast (column1 as varchar(20)) + cast(Priority - 1 + row_number() over(partition by Priority, column1 order by code) as varchar(20))

如果在添加con37时需要1007 3 5,请尝试使用此

select code,column1, Priority, cn ='con' + cast (column1 as varchar(20)) + cast(max(n) over (partition by code) as varchar(20))
from (
   select code,column1, Priority, n=Priority - 1 +  row_number() over(partition by Priority, column1 order by code)
   from myTable 
) t
order by code, column1