2017-06-13 144 views
1

我有一個WHONET_ORGANISMOS列與ORG列,並且此列提供了一些重複的值。計算重複值

ORG 弧 ACK ABC ABC

我想在reapeted那些年底assing連續值。

而且我希望它變成這個樣子

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 

回答

0

嘗試一些東西像下面acheive您的要求:

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 

這是不是你正在尋找?