同一帐户具有不同的不同状态,而其他则具有相同的状态。如何在T-SQl中做到这一点
Account Status
1000001185 A
1000001185 E
1000001185 E
1000001185 D
1000001777 E
1000001777 E
1000001777 E
1000001185 E
答案 0 :(得分:1)
您可以:
select Account, (case when min(Status) = max(Status)
then min(Status)
else 'other'
end)
from table t
group by Account;
Here是一个SQL Fiddle,表明此解决方案是正确的。
答案 1 :(得分:0)
试试这个,我使用Lead()函数
;WITH CTE(Account, [Status])
AS
(
SELECT '1000001185','A' UNION ALL
SELECT '1000001185','E' UNION ALL
SELECT '1000001185','E' UNION ALL
SELECT '1000001185','D' UNION ALL
SELECT '1000001777','E' UNION ALL
SELECT '1000001777','E' UNION ALL
SELECT '1000001777','E' UNION ALL
SELECT '1000001185','E'
)
SELECT Account,[Status],CASE WHEN [Status]<>LEAD([Status],1)OVER(PARTITION BY Account ORDER BY Account)
THEN 'Other' ELSE [Status] END [NewStatus]
FROM Cte
ORDER BY cte.[Status]
结果
Account Status NewStatus
-------------------------------------
1000001185 A A
1000001185 D Other
1000001185 E Other
1000001185 E E
1000001185 E Other
1000001777 E E
1000001777 E E
1000001777 E E