在有任何重复实例的值旁边添加星号

时间:2014-04-15 16:32:35

标签: sql sql-server-2008

我正在尝试将这些表合并在一起,并将星号放在任何COLORWORK旁边,这些副本只是同一个人的重复实例。

TABLE1

NAME     COLOR     WORK
----------------------------
Jane     purple    eDisco
Gianne   Red       Fashion
Daya     pink      Education
Sheryl   pink      eDisco
Jannelle green     Fasion

TABLE2

NAME     COLOR     WORK
----------------------------------
Jane     gray      Technology
Gianne   Orange    Sales
Daya     Purple    Education
Sheryl   pink      FM
Jannelle pink      Fashion

SQL:

SELECT *
INTO TEMP_MERGEALL
FROM TABLE1 
UNION ALL
SELECT * TABLE2 
ORDER BY NAME
GO

;WITH CTE as 
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY COLOR ORDER BY COLOR) AS rno,
        COLOR
    FROM TEMP_MERGEALL
    )
UPDATE CTE 
SET COLOR = COLOR +' ***** '
WHERE rno = 2
GO

;WITH CTE as 
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY Work ORDER BY Work) AS rno,
        Work
    FROM TEMP_MERGEALL
    )
UPDATE CTE 
SET WORK = WORK +' ***** '
WHERE rno = 2
GO

我得到了这个(它只识别COLOR和WORK的重复实例,而不是名称)

Daya        pink           Education
Daya        purple         Education *****
Gianne      red            Fashion
Gianne      orange         Sales
Jane        purple *****   eDisco
Jane        gray           Technology
Jannelle    green          Fashion *****
Janelle     pink *****     Fashion
Sheryl      pink           FM
Sheryl      pink           eDisco

但是,我希望得到我的结果(当有重复的名称重复时,在COLORWORK旁边添加星号)

Daya        pink           Education
Daya        purple         Education *****
Gianne      red            Fashion
Gianne      orange         Sales
Jane        purple         eDisco
Jane        gray           Technology
Janelle     green          Fashion 
Janelle     pink           Fashion *****
Sheryl      pink           FM
Sheryl      pink *****     eDisco

有什么想法?我不知道我在哪里添加GROUP BY Name以使脚本正常工作。

0 个答案:

没有答案