当同一表的其他列的多个记录匹配时,更新列

时间:2019-05-28 10:45:07

标签: sql-server sql-server-2017

我的表格如下:

            Input table:
            RequestNumber            TrackName         Date
            -----------------------------------------------------------
            02209                    Middle         2017-05-28 00:00:00
            0263                     Middle         2017-06-29 00:00:00
            0633                     Middle         2017-09-10 00:00:00
            0762                     Back           2017-06-23 00:00:00
            0762                     Front          2017-06-23 00:00:00
            0762                     Middle         2017-06-23 00:00:00
            01839                    Middle         2017-03-09 00:00:00

我需要检查“ TrackName”的“ RequestNumber”和“ Date”是否相同。 如果“ requestNumber”和“ Date”的多个记录匹配,则应将“ TrackName”列更新为“所有三个”,如下所示 输出示例(在此示例中,有3条记录匹配)

            Output table:
            RequestNumber            TrackName         Date
            -----------------------------------------------------------
            02209                    Middle         2017-05-28 00:00:00
            0263                     Middle         2017-06-29 00:00:00
            0633                     Middle         2017-09-10 00:00:00
            0762                     All three      2017-06-23 00:00:00
            01839                    Middle         2017-03-09 00:00:00

要获得上面指定的输出,这是我尝试过的SQL。 Howveer,将所有TrackName更新为全部三个。

            UPDATE a 
            SET a.[TrackName] = 'All three'
            FROM Table1 as a
            INNER JOIN 
            (SELECT [RequestNumber], row_number() OVER (ORDER BY [RequestNumber] DESC) as rowNumber
            FROM Table1 ) drRowNumbers ON drRowNumbers.[RequestNumber] = a.[RequestNumber] and drRowNumbers.[Date] = a.[Date]

希望我已经正确解释了这一点。我在做什么?有任何疑问要解决吗?

注意:记录是动态记录的,因此不能进行硬编码(如果有的话)。

谢谢。

4 个答案:

答案 0 :(得分:1)

尝试一下。这将返回“全部3”,而不是“全部3”-

SELECT RequestNumber,
CASE 
    WHEN COUNT(*) = 1 THEN MAX(TrackName) 
    ELSE 'All ' + CAST( COUNT(*) AS VARCHAR) 
END TrackName,
Date
FROM your_table
GROUP BY RequestNumber,Date

答案 1 :(得分:0)

我认为您想要SELECT声明:

SELECT t1.RequestNumber, (CASE WHEN COUNT(DISTINCT t1.TrackName) = t2.TrackNo 
                               THEN CONCAT('All ', t2.TrackNo)  
                               ELSE MIN(t1.TrackName) 
                          END) AS TrackName, t1.Date
FROM table1 t1 CROSS JOIN
     (SELECT COUNT(DISTINCT TrackName) AS TrackNo FROM table1) AS t2
GROUP BY t1.RequestNumber, t1.Date;

答案 2 :(得分:0)

这不仅是更新,还是删除。
这意味着您必须执行两个步骤,一个步骤是更新相关记录,另一个步骤是删除第一步创建的重复项。

当两个步骤都在事务中合并时,可以使用几个使用诸如count() over()row_number() over()之类的窗口函数的常用表表达式来完成此操作。

首先,创建并填充示例表(在您将来的问题中为我们保存此步骤):

DECLARE @T AS TABLE
(
    RequestNumber int,
    TrackName varchar(10),
    [Date] datetime2
);

INSERT INTO @T (RequestNumber, TrackName, Date) VALUES
(02209, 'Middle', '2017-05-28 00:00:00'),
(0263, 'Middle', '2017-06-29 00:00:00'),
(0633, 'Middle', '2017-09-10 00:00:00'),
(0762, 'Back', '2017-06-23 00:00:00'),
(0762, 'Front', '2017-06-23 00:00:00'),
(0762, 'Middle', '2017-06-23 00:00:00'),
(01839, 'Middle', '2017-03-09 00:00:00');

接下来,启动一个try块并进行一个事务:

BEGIN TRY
BEGIN TRANSACTION;

然后,识别并更新相关记录:

WITH CTE AS
(
    SELECT  RequestNumber, 
            TrackName, 
            Date,
            COUNT(TrackName) OVER(PARTITION BY RequestNumber, Date) As Cnt
    FROM @T
)

UPDATE CTE
SET TrackName = 'All Three'
WHERE Cnt = 3;

下一步,删除重复项:

WITH CTE AS
(
    SELECT  RequestNumber, 
            TrackName, 
            Date,
            ROW_NUMBER() OVER(PARTITION BY RequestNumber, Date ORDER BY TrackName) As Rn
    FROM @T
)

DELETE 
FROM CTE 
WHERE Rn > 1;

提交事务并关闭try块:

COMMIT TRANSACTION;
END TRY

使用catch块回滚事务:

BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION

    -- you might want to print the result of ERROR_MESSAGE() here...
END CATCH

最后,选择以查看更改:

SELECT  RequestNumber, 
        TrackName, 
        Date
FROM @T

结果:

RequestNumber   TrackName   Date
2209            Middle      28.05.2017 00:00:00
263             Middle      29.06.2017 00:00:00
633             Middle      10.09.2017 00:00:00
762             All Three   23.06.2017 00:00:00
1839            Middle      09.03.2017 00:00:00

You can see a live demo on rextester(减去那里不允许的交易部分和无论如何都没有交易的try ... catch)

答案 3 :(得分:0)

尝试一下

第1步将记录之一设置为全部三项

update table1
set TrackName = 'All Three'
where requestnumber in (select requestnumber
from table1
group by requestnumber,[date]
having count(*) = 3)
and trackname = 'Front'

第2步删除不再需要的数据

delete table1
where requestnumber in (select requestnumber
from table1
group by requestnumber,[date]
having count(*) = 3)
and trackname <> 'All Three'