重新计算价值

时间:2017-06-13 02:41:58

标签: sql-server-2008

我有一个带有ORG列的WHONET_ORGANISMOS表,此列显示了一些重复值。

ORG 弧 ACK ABC ABC

我想在reapeted的结尾处给出一个连续的值。

我希望它变得像这样

ORG 弧 ACK ABC ABC1

我正在尝试使用此查询,但无法正常工作,您能帮帮我吗?

DECLARE @conta INT
DECLARE @conta2 INT
SET @conta = 1
SET @conta2 = 1

WHILE (@conta>=1)
BEGIN
    UPDATE WHONET_ORGANISMOS
SET ORG = {fn concat (ORG, @conta2)}
WHERE ID IN
(
SELECT MAX(ID) 
FROM WHONET_ORGANISMOS 
GROUP BY ORG
HAVING count(*) > 1
);
    SET @conta2 = @conta2 +1;
    SET @conta = SELECT COUNT(*)(SELECT ORG, COUNT(*) TotalCount FROM WHONET_ORGANISMOS GROUP BY ORG HAVING COUNT(*) > 1)
END
GO

1 个答案:

答案 0 :(得分:0)

尝试以下内容来实现您的要求:

I have craeted a sample table temp:
create table temp(id int,ORG varchar(10))
insert into temp values(1,'ack'),(2,'abc'),(3,'abc')


SELECT
    id,
    ORG + (CASE rno WHEN 1 THEN '' ELSE CAST((rno-1) AS VARCHAR(10)) END) AS ORG
FROM 
(
    SELECT 
        ROW_NUMBER() OVER( PARTITION BY ORG  ORDER BY id) as rno,
        id, 
        ORG 
    FROM 
        temp
) T

结果是:

2   abc
3   abc1
1   ack

这是你要找的东西吗?