根据一组记录更新数值。

huangapple go评论72阅读模式
英文:

Updating values based on a group of records

问题

我正在尝试对“Keys”列执行更新,其中Janitor值设置为“No_Name”。只有当所有Janitors在Building Name列中都具有NULL值时,才应将其更新为“None”。当Janitor(们)确实具有Key时,将“No_Name”设置为NULL。

Building Name Janitor Keys Notes
Building A Andrew Yes
Building A Mike Yes
Building A Bill NULL
Building A Phil Yes
Building A No_Name NULL --- 应保持为NULL
Building B Andrew NULL
Building B Mike NULL
Building B Bill NULL
Building B Phil NULL
Building B No_Name NULL --- 应更改为'NONE',因为没有Owners有Keys
英文:

I'm trying to carry out an update on the "Keys" column, where Janitor value is set to "No_Name". It should be updated to "None" only when all Janitors have a NULL value per Building Name. When a Janitor(s) do have a Key then set 'No_Name' as NULL still.

Building Name Janitor Keys Notes
Building A Andrew Yes
Building A Mike Yes
Building A Bill NULL
Building A Phil Yes
Building A No_Name NULL --- should stay NULL
Building B Andrew NULL
Building B Mike NULL
Building B Bill NULL
Building B Phil NULL
Building B No_Name NULL --- should change to 'NONE' bcs no Owners have the keys

Tried a bunch of AND/OR statements with no success.

答案1

得分: 2

你可以使用可更新的表达式,利用窗口聚合功能:

update t 
  set Keys = case when k is null then 'None' end
from (
  select *, Max(keys) over(partition by Building_Name) k
  from t
)t
where Janitor = 'No_Name';
英文:

You could use an updatable table expression making use of a window aggregate:

update t 
  set Keys = case when k is null then 'None' end
from (
  select *, Max(keys) over(partition by Building_Name) k
  from t
)t
where Janitor = 'No_Name';

答案2

得分: 0

你可以尝试使用一个 EXISTS 表达式。

UPDATE tab
SET Keys = 'None'
WHERE Janitor = 'No_Name'
  AND NOT EXISTS(SELECT 1 
                 FROM tab t2 
                 WHERE tab.BuildingName = t2.BuildingName AND t2.Keys IS NOT NULL);

输出

BuildingName Janitor Keys
Building A Andrew Yes
Building A Mike Yes
Building A Bill null
Building A Phil Yes
Building A No_Name null
Building B Andrew null
Building B Mike null
Building B Bill null
Building B Phil null
Building B No_Name None

查看演示 这里

英文:

You can try going with an EXISTS expression.

UPDATE tab
SET Keys = 'None'
WHERE Janitor = 'No_Name'
  AND NOT EXISTS(SELECT 1 
                 FROM tab t2 
                 WHERE tab.BuildingName = t2.BuildingName AND t2.Keys IS NOT NULL);

Output:

BuildingName Janitor Keys
Building A Andrew Yes
Building A Mike Yes
Building A Bill null
Building A Phil Yes
Building A No_Name null
Building B Andrew null
Building B Mike null
Building B Bill null
Building B Phil null
Building B No_Name None

Check the demo here.

答案3

得分: 0

我不知道您正在使用的确切数据库管理系统,但思想通常是相同的。
您可以使用聚合函数如MAX()来生成要更新为None的建筑列表。

select "Building name"
from No_Name
group by "Building name"
having max(keys) <> NULL

此查询将为您提供所有在keys字段中具有所有NULL值的建筑物,然后您可以将其整合到更新语句中。

英文:

I dont know the exact DBMS you're using, but the idea is generally the same.
You can use aggregation function like MAX() on Keys to generate list of buildings to update to None.

select &quot;Building name&quot;
from No_Name
group by &quot;Building name&quot;
having max(keys) &lt;&gt; NULL

this query will give you all building that have all NULL values in keys, then you can incorporate it in Update statement.

huangapple
  • 本文由 发表于 2023年6月1日 04:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定