在下表中,我想使用 SQL 來替換 aUserID當 aGroupID有<=1uniqueUserID與之關聯時:
| 組ID | 用戶身份 |
|---|---|
| 1 | 123 |
| 1 | 456 |
| 1 | 789 |
| 2 | 987 |
| 3 | 876 |
| 3 | 765 |
回傳的結果如下所示:
| 組ID | 用戶身份 |
|---|---|
| 1 | 123 |
| 1 | 456 |
| 1 | 789 |
| 2 | 編輯 |
| 3 | 876 |
| 3 | 765 |
這里的用例是防止基于組識別單個用戶的能力。如果一個組有多個用戶,則它認為匿名足以顯示。
這里的任何幫助將不勝感激。
uj5u.com熱心網友回復:
使用視窗函式的另一種選擇 sum() over()
例子
Select GroupID
,UserID = case when sum(1) over (partition by GroupID) = 1 then 'Redacted' else left(UserID,25) end
from YourTable
結果
GroupID UserID
1 123
1 456
1 789
2 Redacted
3 876
3 765
uj5u.com熱心網友回復:
您可以使用在視窗上磁區的聚合函式:
select GroupId,
case when Min(userid) over(partition by groupid) = Max(userid) over(partition by groupid)
then 'redacted'
else Cast(userid as varchar(10))
end as UserId
from t
注意這個假定的userId是一個整數資料型別,如果它已經是一個varchar你不需要轉換它。
uj5u.com熱心網友回復:
您可以使用派生表并應用如下給出的邏輯:
DECLARE @table table(GroupID int, UserID int)
insert into @table values
(1,123),
(1,456),
(1,789),
(2,987),
(3,876),
(3,765);
select groupid,case when distinctgroupcount =1 then 'redact' else cast(userid as varchar(10)) end as userid
from
(
select *, count(*) over(partition by groupid) as distinctgroupcount
from @table
) as t
| 組名 | 用戶身份 |
|---|---|
| 1 | 123 |
| 1 | 456 |
| 1 | 789 |
| 2 | 纂 |
| 3 | 876 |
| 3 | 765 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404954.html
標籤:
