简体   繁体   English

从另一个表更新具有多个条件的表

[英]Updating a table with mutliple criteria from another table

I have inherited a database that contains among others, two relevant tables, one for Clients and one for Matters. 我继承了一个数据库,其中包含两个相关的表,一个用于客户,一个用于事务。 Clients may have more than one matter, and these two tables are linked by a Client ID field. 客户可能有多个事务,这两个表通过“客户ID”字段链接。

As part of a de-identifying process, seven years after a file has been closed we are required to remove the first names, last names and date of birth of clients in the Clients table, however we want to otherwise retain the record so we are able to keep track of what work we have done in the past. 作为取消标识过程的一部分,在关闭文件七年后,我们需要在“客户”表中删除客户的名字,姓氏和出生日期,但是我们希望保留该记录,因此我们能够跟踪我们过去所做的工作。

I want to create an update query that will replace the identifying data with de-identified data when a client fulfills these conditions: 我想创建一个更新查询,当客户端满足以下条件时,它将用取消标识的数据替换标识数据:

  • The client has no matters that are currently open (ie that have a 'null' in the file closure date field in the Matters table). 客户端没有当前打开的事务(即,在Matters表的文件关闭日期字段中为“ null”)。
  • The client has no matters that have closed less than seven years ago. 客户没有不到七年前就关闭的事务。
  • The client has one or more matters that have closed more than seven years ago. 客户有一项或多项已于七年前关闭的事务。

However I am unable to figure out how I can get this to work. 但是,我无法弄清楚如何使它工作。 Setting up an update query that would update the info in the Clients table if it has a corresponding matter that is older than seven years seems possible, but I can't figure out how to do it in such a way that wouldn't also update the info if the client still has matters open, or has matters younger than seven years. 设置一个更新查询可以更新Clients表中的信息(如果它具有的相关事项早于七年)似乎是可行的,但是我无法弄清楚如何以不会更新的方式进行操作如果客户仍有未解决的问题或未满7年的问题,则提供此信息。 Could anyone point me in the right direction? 有人能指出我正确的方向吗?

To expand further, so far my query involves two tables, Clients and Matters . 为了进一步扩展,到目前为止,我的查询涉及两个表ClientsMatters The file closure date is in Matters and the client's identifying details are in Clients . 文件关闭日期在Matters ,客户的标识详细信息在“ Clients So as far as I understand, it's more of a join problem than a criteria problem. 据我了解,与其说是标准问题,不如说是联接问题。

Client | MatterID | MatterClosureDate
-------|----------|------------------
Alice  | 1        | 2008
Bruce  | 2        | 2009
Carrol | 3        | 2009
Bruce  | 250      | 2012

to illustrate, there are four relevant results from the two tables. 为了说明这一点,两个表有四个相关结果。 Alice and Carrol are both clients who have no matters that are still open or recently closed, however Bruce has one matter that should disqualify him. 爱丽丝(Alice)和卡罗尔(Carrol)都是没有事务的客户,这些事务仍未解决或最近未解决,但是布鲁斯(Bruce)有一件事应取消他的资格。 At the moment though my query is not sophisticated enough to realize this, and returns this: 目前,虽然我的查询还不够复杂,无法实现,但返回了:

Client | MatterID | MatterClosureDate
-------|----------|------------------
Alice  | 1        | 2008
Bruce  | 2        | 2009
Carrol | 3        | 2009

您可以在文件关闭日期字段的条件中输入< #9/14/2009# And Is Not Null

Consider using correlated subqueries matched row-wise to each ClientID for each listed condition: 考虑为每个列出的条件使用与每个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

And for the update query, convert above SELECT into UPDATE statement: 对于更新查询,将上面的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;

Alternatively, you can use Access' domain aggregate, DCount() : 另外,您可以使用Access的域聚合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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM