根据符合条件的 1 条记录更新多条记录
Update multiple records based on 1 record meeting a criteria
我正在尝试 Select 数据集中的所有记录,其中 1 条记录符合条件。我有一个 table 数据,其中包含一个人每天的工作时间。如果该周内的一条记录的 'logged' 标志设置为 'N',我需要 select 一个人那一周的所有记录。这是一个数据示例:
t_hours:
Name Week Weekday Hours Logged
===============================
Jim 1 Mon 8 Y
Jim 1 Wed 8 Y
Jim 1 Fri 8 Y
Jim 2 Mon 8 Y
Jim 2 Wed 8 Y
Bill 1 Mon 8 N
Bill 1 Tue 8 Y
Bill 1 Wed 8 Y
Bill 1 Thu 8 Y
Bill 2 Mon 8 Y
Bill 2 Tue 8 Y
我想编写一个查询,将一个人工作周的所有记录更新为 Logged='N',如果他们有一天 Logged='N'。但我不知道如何 select 记录。以下是我要更新的记录:
Name Week Weekday Hours Logged
===============================
Bill 1 Mon 8 N
Bill 1 Tue 8 Y
Bill 1 Wed 8 Y
Bill 1 Thu 8 Y
我试过正常的 select,但无法弄清楚如何在 where 子句中有两个相关的子查询:
SELECT * FROM t_hours
WHERE (Name = (SELECT t1.Name FROM t_hours t1
where t1.Name = t2.Name and t1.Week = t2.Week and
t1.Logged = 'N') and
Week = (SELECT t2.Week FROM t_hours t2
where t1.Name = t2.Name and t1.Week = t2.Week and
t2.Logged = 'N')
但这不起作用,非常感谢任何帮助。
像下面这样尝试 - 使用 exists
select t1.* from table_name t1
where exists( select 1 from table_name t2 where t1.week=t2.week
and t2.logged='N' and t1.name=t2.name
)
这是你想要的吗?
Update table set Logged='N' where
Exists (Select 1 from(Select
Name,Workweek group by Name,
Workweek having count(Logged='N')
>=1)t1 where Name=t1.name and
Workweek=t1.Workweek)
Return数据:
SELECT *
FROM t_hours t1
WHERE EXISTS (SELECT 1
FROM t_hours t2
WHERE t1.NAME = t2.NAME
AND t1.week = t2.week
AND t2.logged = 'N');
更新数据:
UPDATE t_hours
SET logged = 'N'
WHERE EXISTS (SELECT 1
FROM t_hours t2
WHERE t1.NAME = t2.NAME
AND t1.week = t2.week
AND t2.logged = 'N');
如果数据集足够大,您可以按名称、周和记录类型分组。
您可以使用可更新的 CTE:
with toupdate as (
select t.*,
min(logged) over (partition by name, week) as min_logged
from t
)
update toupdate
set logged = min_logged
where min_logged = 'N' and min_logged <> logged;
如果任何 logged
值是 'N'
,min(logged)
表达式将 return 'N'
(如果有的话,其余的是 'Y'
) 对于 person/week.
我正在尝试 Select 数据集中的所有记录,其中 1 条记录符合条件。我有一个 table 数据,其中包含一个人每天的工作时间。如果该周内的一条记录的 'logged' 标志设置为 'N',我需要 select 一个人那一周的所有记录。这是一个数据示例:
t_hours:
Name Week Weekday Hours Logged
===============================
Jim 1 Mon 8 Y
Jim 1 Wed 8 Y
Jim 1 Fri 8 Y
Jim 2 Mon 8 Y
Jim 2 Wed 8 Y
Bill 1 Mon 8 N
Bill 1 Tue 8 Y
Bill 1 Wed 8 Y
Bill 1 Thu 8 Y
Bill 2 Mon 8 Y
Bill 2 Tue 8 Y
我想编写一个查询,将一个人工作周的所有记录更新为 Logged='N',如果他们有一天 Logged='N'。但我不知道如何 select 记录。以下是我要更新的记录:
Name Week Weekday Hours Logged
===============================
Bill 1 Mon 8 N
Bill 1 Tue 8 Y
Bill 1 Wed 8 Y
Bill 1 Thu 8 Y
我试过正常的 select,但无法弄清楚如何在 where 子句中有两个相关的子查询:
SELECT * FROM t_hours
WHERE (Name = (SELECT t1.Name FROM t_hours t1
where t1.Name = t2.Name and t1.Week = t2.Week and
t1.Logged = 'N') and
Week = (SELECT t2.Week FROM t_hours t2
where t1.Name = t2.Name and t1.Week = t2.Week and
t2.Logged = 'N')
但这不起作用,非常感谢任何帮助。
像下面这样尝试 - 使用 exists
select t1.* from table_name t1
where exists( select 1 from table_name t2 where t1.week=t2.week
and t2.logged='N' and t1.name=t2.name
)
这是你想要的吗?
Update table set Logged='N' where
Exists (Select 1 from(Select
Name,Workweek group by Name,
Workweek having count(Logged='N')
>=1)t1 where Name=t1.name and
Workweek=t1.Workweek)
Return数据:
SELECT *
FROM t_hours t1
WHERE EXISTS (SELECT 1
FROM t_hours t2
WHERE t1.NAME = t2.NAME
AND t1.week = t2.week
AND t2.logged = 'N');
更新数据:
UPDATE t_hours
SET logged = 'N'
WHERE EXISTS (SELECT 1
FROM t_hours t2
WHERE t1.NAME = t2.NAME
AND t1.week = t2.week
AND t2.logged = 'N');
如果数据集足够大,您可以按名称、周和记录类型分组。
您可以使用可更新的 CTE:
with toupdate as (
select t.*,
min(logged) over (partition by name, week) as min_logged
from t
)
update toupdate
set logged = min_logged
where min_logged = 'N' and min_logged <> logged;
如果任何 logged
值是 'N'
,min(logged)
表达式将 return 'N'
(如果有的话,其余的是 'Y'
) 对于 person/week.