将NA替换为NA

时间:2018-06-11 09:43:12

标签: sql sql-server

我有一个select查询,它返回如下所示的结果,

ID  Status
1   Approved
2   Rejected
3   Pending
4   Pending

我们的目标是在拒绝后使用' NA'替换条目。我尝试使用案例陈述,但只有第三行被更改为NA。任何人都可以帮助我吗?

我使用的查询是,

   case when Lag(a.Status,1) over (order by a.ID)='Rejected'
   then 'NA'
   else a.Status 
   end  as Status`

预期的输出是,

Approved
Rejected
NA
NA

目标是在NA拒绝状态后替换所有待处理的暂停。

3 个答案:

答案 0 :(得分:0)

您可以在检查MAX()条件时使用窗口SUM()rejected

IF OBJECT_ID('tempdb..#Status') IS NOT NULL
    DROP TABLE #Status

CREATE TABLE #Status (
    ID INT IDENTITY,
    GroupID INT,
    Status VARCHAR(100))

INSERT INTO #Status (
    GroupID,
    Status)
VALUES
    (1, 'Approved'),
    (1, 'Rejected'),
    (1, 'Pending'),
    (1, 'Pending'),
    (1, 'Pending'),

    (2, 'Pending'),
    (2, 'Approved'),
    (2, 'Pending'),
    (2, 'Rejected'),
    (2, 'Pending'),
    (2, 'Pending')

;WITH WindowedMax AS
(
    SELECT
        S.ID,
        S.GroupID,
        S.Status,
        RejectedCue = MAX(CASE WHEN S.Status = 'Rejected' THEN 1 END) OVER (PARTITION BY S.GroupID ORDER BY S.ID ASC)
    FROM
        #Status AS S
)
UPDATE S SET
    Status = 'NA'
FROM
    WindowedMax AS W
    INNER JOIN #Status AS S ON W.ID = S.ID
WHERE
    W.RejectedCue = 1 AND W.Status <> 'Rejected'

SELECT * FROM #Status AS S ORDER BY S.ID

/* Results:

    ID  GroupID Status
    1   1   Approved
    2   1   Rejected
    3   1   NA
    4   1   NA
    5   1   NA

    6   2   Pending
    7   2   Approved
    8   2   Pending
    9   2   Rejected
    10  2   NA
    11  2   NA

*/

PARTITION BY将确定如何区分数据中的组,ORDER BY将确定每个分区中每个状态的顺序。

编辑:用作视图。

;WITH WindowedMax AS
(
    SELECT
        S.ID,
        S.GroupID,
        S.Status,
        RejectedCue = MAX(CASE WHEN S.Status = 'Rejected' THEN 1 END) OVER (PARTITION BY S.GroupID ORDER BY S.ID ASC)
    FROM
        #Status AS S
)
SELECT
    S.ID,
    S.GroupID,
    Status = CASE WHEN W.RejectedCue = 1 AND W.Status <> 'Rejected' THEN 'NA' ELSE S.Status END
FROM
    WindowedMax AS W
    INNER JOIN #Status AS S ON W.ID = S.ID

答案 1 :(得分:0)

这个问题的严格答案是:

select (case when id > max(case when status = 'Rejected' then id end) over (order by id) and
                  status = 'Pending'
             then 'NA' else status
        end) as new_status
from t;

这会转换所有&#34;待定&#34;最后一个&#34;拒绝&#34;之后的值到&#34; NA&#34;。如果表格中可能有更多行,我不确定这是否合理,但它会使用您提供的示例数据回答这个特定问题。

答案 2 :(得分:-1)

替换拒绝后的所有记录替换等待“

相同

无论如何这适用于第二种情况

SELECT REPLACE(MYCOLUMN, 'Pending', 'NA')

也检查这种方法:

declare @test table (ID int, Status nvarchar(50))
INSERT INTO @test VALUES
(1,   'Approved'),
(2,   'Pending'),
(3,   'Rejected'),
(4,   'Pending'),
(5,   'Pending'),
(6,   'Approved'),
(7,   'Pending'),
(8,   'Maybe Pending'),
(9,   'Pending'),
(10,  'Rejected'),
(11,  'Pending'),
(12,  'Pending')


SELECT  id, case
        when Lag(a.Status,1) over (order by a.ID) in ('Rejected', 'Pending')
        and A.STATUS = 'Pending'
        then 'NA'
        else a.Status
        end  as Status FROM @test a

输出:

id  Status
1   Approved
2   Pending
3   Rejected
4   NA
5   NA
6   Approved
7   Pending
8   Maybe Pending
9   Pending
10  Rejected
11  NA
12  NA