我必须根据以下条件更新每个ID的状态。 一个ID可以有多个状态,但我必须根据条件选择状态。
我的示例架构如下:
CREATE TABLE [dbo].[Table1](
[ID] [nvarchar](50) NULL,
[ApprovalDate] [datetime] NULL,
[CreateDate] [datetime] NULL,
[status] [nvarchar](50) NULL
)
;
Insert into Table1 values (1,null,null,'Approved')
Insert into Table1 values (1,null,null,'Modified')
Insert into Table1 values (1,null,null,'Later')
Insert into Table1 values (2,null,null,'Approved')
Insert into Table1 values (2,null,null,'Modified')
Insert into Table1 values (2,null,null,'Approved')
Insert into Table1 values (3,null,null,'Modified')
Insert into Table1 values (3,null,null,'Later')
Insert into Table1 values (4,null,null,'Approved')
如果ID 1已批准,稍后,修改我应该说“部分修改”,如果ID已经批准和修改,那么也是“部分修改”。但我得到的案例陈述不是按ID进行。它正在改变总体数据基于status.Please建议。我已经包含SQL fiddler,输出应该具有ID 1 - 部分修改,2部分修改,3修改,4批准..具有与表中相同的行数。
select ID,
CASE
WHEN status = 'Approved' AND status IN('Modified','Later')
THEN 'Partial Modified'
WHEN status = 'Approved'
THEN 'Approved'
WHEN status IN('Modified','Edited') THEN 'Modified'
ELSE status
END status
group by ID,Status
答案 0 :(得分:2)
以下是可能的解决方案。
SELECT ID,
CASE WHEN MAX( CASE WHEN [status] = 'Approved' THEN 'Approved' ELSE NULL END ) = 'Approved'
AND MAX( CASE WHEN [status] IN('Modified','Later') THEN 'Partial Modified' ELSE NULL END ) = 'Partial Modified'
THEN 'Partial Modified'
ELSE MAX([status]) -- this can go wrong if there are multiple status which don't fall into above condition.
END AS [Status]
FROM Table1
GROUP BY ID
方式:2 强> 如果您希望结果与查询中的表格使用相同的行数。
;WITH CTE AS (
SELECT ID,
CASE WHEN MAX( CASE WHEN [status] = 'Approved' THEN 'Approved' ELSE NULL END ) = 'Approved'
AND MAX( CASE WHEN [status] IN('Modified','Later') THEN 'Partial Modified' ELSE NULL END ) = 'Partial Modified'
THEN 'Partial Modified'
WHEN MAX( CASE WHEN [status] = 'Modified' THEN 'Modified' ELSE NULL END ) = 'Modified'
AND MAX( CASE WHEN [status] IN('Edited','Later') THEN 'Modified' ELSE NULL END ) = 'Modified'
THEN 'Modified'
ELSE NULL -- this can go wrong if there are multiple status which don't fall into above condition.
END AS [Status]
FROM Table1
GROUP BY ID
)
SELECT
t.ID,
ISNULL(c.[status],t.[status]) AS [status]
FROM CTE AS c
INNER JOIN Table1 AS t ON t.ID = c.ID
GROUP BY t.ID, t.[status],c.[status]
<强>输出强>
ID status
------- ----------------
1 Partial Modified
1 Partial Modified
1 Partial Modified
2 Partial Modified
2 Partial Modified
3 Modified
3 Modified
4 Approved
答案 1 :(得分:0)
您可以使用如下查询。
请注意,此方法允许您将所有用例预先映射为整数值的简单组合,而不是像WHEN status = 'Approved' AND status IN('Modified','Later')
的 See live demo 强>
select id,
case sum(v)
when 1 then 'approved'
when 2 then 'modified'
when 3 then 'partially modified'
when 4 then 'partially modified'
when 5 then 'partially modified'
when 6 then 'modified'
when 7 then 'partially modified'
end [status] from
Table1 t1 join
(values( 'approved',1),('Modified',2),('later',4)) as t(s,v)
on t1.[status]=t.s
group by id
希望ID行以相同的最终状态重复
所以在这种情况下解决方案将是
select
T1.*,
T.[status]
from
Table1 T1
Join
(
select id,
case sum(v)
when 1 then 'approved'
when 2 then 'modified'
when 3 then 'partially modified'
when 4 then 'partially modified'
when 5 then 'partially modified'
when 6 then 'modified'
when 7 then 'partially modified'
end [status] from
Table1 t1 join
(values( 'approved',1),('Modified',2),('later',4)) as t(s,v)
on t1.[status]=t.s
group by id
)T
on T.id=T1.id