删除没有最高版本和修订版本的数据集

时间:2018-11-22 13:32:24

标签: sql ms-access

我想完成MS Access SQL: Get datasets with highest versions and revisions的对立面

我有一个数据库表,该表包含(针对此问题)四列:

  1. ID
  2. 文档编号
  3. 修订版
  4. 版本

每个文档都有1..n版本,每个版本都有1..n版本。

我的删除文档的SQL语句是什么

  • 相同的文档编号和相同的修订版,但是存在更高的版本
  • 或相同的文档编号和更高版本(包括所有版本)

    ID  Doc-No Rev Vers Should be deleted?
    1   Dok1   01  01   yes, because same Doc-No higher rev exists
    2   Dok1   01  02   yes, because same Doc-No higher rev exists
    3   Dok1   01  03   yes, because same Doc-No higher rev exists
    4   Dok1   02  01   yes, because same Doc-No and a higher vers for this rev exists
    5   Dok1   02  02   no, because Rev 02 / V02 is the highest for Doc-No Dok1
    6   Dok2   01  01   yes, because same Doc-No higher rev exists
    7   Dok2   02  01   yes, because same Doc-No higher rev exists
    8   Dok2   03  01   no, because Rev 03 / V01 is the highest for Doc-No Dok2
    

3 个答案:

答案 0 :(得分:1)

您可以这样做:

delete from t
    where t.id <> (select top (1) t2.id
                   from t as t2
                   where t2.doc_no = t.doc_num
                   order by t2.version desc, t2.revision desc, t2.id desc
                  );

当然,在尝试使用delete之前先备份表。

答案 1 :(得分:1)

使用INNER JOIN在几个针对修订版本级别的汇总查询中使用SELECT d.* FROM (documents d INNER JOIN (SELECT sub_d.[Doc-No], MAX(sub_d.Rev) AS max_rev FROM documents sub_d GROUP BY sub_d.[Doc-No]) AS agg1 ON d.[Doc-No] = agg1.[Doc-No] AND d.Rev = agg1.max_rev) INNER JOIN (SELECT sub_d.[Doc-No], sub_d.Rev, MAX(sub_d.Ver) AS max_ver FROM documents sub_d GROUP BY sub_d.[Doc-No], sub_d.Rev) AS agg2 ON d.[Doc-No] = agg2.[Doc-No] AND d.Rev = agg2.rev AND d.Ver = agg2.max_ver 会比使用原始查询更有效。相关子查询。前者对外部查询中的所有行计算一次,而后者对行计算:

DELETE

DELETE DISTINCTROW d.* FROM (documents d INNER JOIN (SELECT sub_d.[Doc-No], MAX(sub_d.Rev) AS max_rev FROM documents sub_d GROUP BY sub_d.[Doc-No]) AS agg1 ON d.[Doc-No] = agg1.[Doc-No] AND d.Rev = agg1.max_rev) INNER JOIN (SELECT sub_d.[Doc-No], sub_d.Rev, MAX(sub_d.Ver) AS max_ver FROM documents sub_d GROUP BY sub_d.[Doc-No], sub_d.Rev) AS agg2 ON d.[Doc-No] = agg2.[Doc-No] AND d.Rev = agg2.rev AND d.Ver = agg2.max_ver 的翻译为:

@Service
@Slf4j
public class DbApiClientImpl implements DbApiClient {

  private final String URL_DELIMITER = "/";
  private RestTemplate restTemplate;
  private String url;

  public DbApiClientImpl(
      RestTemplateBuilder restTemplate,
      @Value("${dbapi.namespace}") String namespace,
      @Value("${dbapi.url}") String uri,
      @Value("${dbapi.username}") String username,
      @Value("${dbapi.password}") String password) {

  this.restTemplate = restTemplate.basicAuthorization(username, 
                      password).build();
  this.url = namespace.concat(uri);
}

   @Override
   @Async("asyncExecutor")
   public Merchant fetchMerchant(String id) {
       ResponseEntity<Merchant> response = 
        restTemplate.getForEntity(url.concat(URL_DELIMITER).concat(id), 
        Merchant.class);
   return response.getBody();
   }
}

答案 2 :(得分:0)

以下应该达到预期的结果:

delete from Table1 t1 
where exists
(
    select 1 from Table1 t2 
    where
    t1.[Doc-No] = t2.[Doc-No] and 
    (
        t1.Rev < t2.Rev or 
       (t1.Rev = t2.Rev and t1.Vers < t2.Vers)
   )
)

(将Table更改为表格名称以及与数据不匹配的其他任何字段)

在运行delete查询之前始终备份数据-不能撤消!