我继承了一个包含两个相关表的数据库,一个用于客户端,一个用于事务。客户端可能有多个问题,这两个表由客户端ID字段链接。
作为去识别过程的一部分,文件关闭七年后,我们需要删除客户端表中客户的名字,姓氏和出生日期,但我们要保留记录所以我们能够跟踪我们过去做过的工作。
我想创建一个更新查询,当客户端满足以下条件时,将使用去标识数据替换标识数据:
然而,我无法弄清楚如何让它发挥作用。设置一个更新查询,如果它具有超过七年的相应事项,将更新客户端表中的信息似乎是可能的,但我无法弄清楚如何以这样的方式执行此操作。 ;如果客户仍有事宜,或者年龄小于七年,也会更新信息。有人能指出我正确的方向吗?
为了进一步扩展,到目前为止,我的查询涉及两个表Clients
和Matters
。文件关闭日期位于Matters
,客户端的标识详细信息位于Clients
。据我所知,这更像是一个加入问题,而不是一个标准问题。
Client | MatterID | MatterClosureDate
-------|----------|------------------
Alice | 1 | 2008
Bruce | 2 | 2009
Carrol | 3 | 2009
Bruce | 250 | 2012
来说明这两个表有四个相关结果。爱丽丝和卡罗尔都是没有任何事情仍未开放或最近关闭的客户,但布鲁斯有一件事应该取消他的资格。目前虽然我的查询不够复杂,无法实现这一点,并返回此信息:
Client | MatterID | MatterClosureDate
-------|----------|------------------
Alice | 1 | 2008
Bruce | 2 | 2009
Carrol | 3 | 2009
答案 0 :(得分:0)
您可以将< #9/14/2009# And Is Not Null
放在文件关闭日期字段中的条件
答案 1 :(得分:0)
考虑为每个列出的条件使用与每个 ClientID 逐行匹配的相关子查询:
SELECT c.FirstName, m.MatterID, m.MatterClosureDate
FROM Clients c
INNER JOIN Matters m ON c.ID = m.ClientID
WHERE
(SELECT Count(*) FROM Matters sub
WHERE sub.ClientID = c.ID
AND sub.MatterClosureDate IS NULL) = 0
AND
(SELECT Count(*) FROM Matters sub
WHERE sub.ClientID = c.ID
AND sub.MatterClosureDate > Year(Date()) - 7) = 0
AND
(SELECT Count(*) FROM Matters sub
WHERE sub.ClientID = c.ID
AND sub.MatterClosureDate <= Year(Date()) - 7) > 0;
-- FirstName MatterID MatterClosureDate
-- Alice 1 2008
-- Carrol 3 2009
对于更新查询,请将SELECT
转换为UPDATE
语句:
UPDATE Clients c
SET c.FirstName = 'XXXXX',
c.LastName = 'XXXXX',
c.DOB = NULL
WHERE
(SELECT Count(*) FROM Matters sub
WHERE sub.ClientID = c.ID
AND sub.MatterClosureDate IS NULL) = 0
AND
(SELECT Count(*) FROM Matters sub
WHERE sub.ClientID = c.ID
AND sub.MatterClosureDate > Year(Date()) - 7) = 0
AND
(SELECT Count(*) FROM Matters sub
WHERE sub.ClientID = c.ID
AND sub.MatterClosureDate <= Year(Date()) - 7) > 0;
或者,您可以使用Access&#39;域聚合,DCount()
:
UPDATE Clients c
SET c.FirstName = 'XXXXX',
c.LastName = 'XXXXX',
c.DOB = NULL
WHERE
DCount("*", "Matters", "ClientID = " & c.ID & "
AND MatterClosureDate IS NULL") = 0
AND
DCount("*", "Matters", "ClientID = " & c.ID & "
AND MatterClosureDate > Year(Date()) - 7") = 0
AND
DCount("*", "Matters", "ClientID = " & c.ID & "
AND MatterClosureDate <= Year(Date()) - 7") > 0