简体   繁体   English

如何在 Access 2013 中创建计算列以检测重复项

[英]How to create a calculated column in access 2013 to detect duplicates

I'm recreating a tool I made in Excel as it's getting bigger and performance is getting out of hand.我正在重新创建我在Excel中制作的工具,因为它越来越大,性能也越来越失控。

The issue is that I only have MS Access 2013 on my work laptop and I'm fairly new to the Expression Builder in Access 2013 , which has a very limited function base to be honest.问题是我的工作笔记本电脑上只有MS Access 2013 ,而且我对Access 2013中的Expression Builder还很陌生,老实说,它的 function 基础非常有限。

My data has duplicates on the [Location] column, meaning that, I have multiple SKUs on that warehouse location.我的数据在[Location]列中有重复项,这意味着我在该仓库位置有多个 SKU。 However, some of my calculations need to be done only once per [Location] .但是,我的一些计算只需要为每个[Location]完成一次。 My solution to that, in Excel , was to make a formula (see below) putting 1 only on the first appearance of that location, putting 0 on next appearances.我的解决方案是在Excel中制作一个公式(见下文)仅在该位置的第一次出现时放置 1,在下一次出现时放置 0。 Doing that works like a charm because summing over that [Duplicate] column while imposing multiple criteria returns the number of occurrences of the multiple criteria counting locations only once.这样做就像一个魅力,因为在施加多个条件的同时对该[Duplicate]列求和返回多个条件计数位置的出现次数仅一次。

Now, MS Access 2013 Expression Builder has no SUM nor COUNT functions to create a calculated column emulating my [Duplicate] column from Excel .现在, MS Access 2013 Expression Builder没有 SUM 也没有 COUNT 函数来创建一个计算列来模拟我的[Duplicate]来自Excel的列。 Preferably, I would just input the raw data and let Access populate the calculated fields vs also inputting the calculated fields as well, since that would defeat my original purpose of reducing the computational cost of creating my dashboard.我最好只输入原始数据并让Access填充计算字段,而不是同时输入计算字段,因为这会破坏我降低创建仪表板的计算成本的初衷。

The question is, how would you create a calculated column, in MS Access 2013 Expression Builder to recreate the below Excel function:问题是,您将如何在MS Access 2013 Expression Builder中创建计算列以重新创建以下Excel function:

= IF($D$2:$D3=$D4,0,1) = IF($D$2:$D3=$D4,0,1)

In the sake of reducing the file size (over 100K rows) I even replace the 0 by a blank character "" .为了减小文件大小(超过 100K 行),我什至将 0 替换为空白字符""

Thanks in advance for your help在此先感谢您的帮助

Y

First and foremost, understand MS Access' Expression Builder is a convenience tool to build an SQL expression.首先,了解 MS Access 的表达式生成器是一种用于构建 SQL 表达式的便捷工具。 Everything in Query Design ultimately is to build an SQL query.查询设计中的一切最终都是为了构建一个 SQL 查询。 For this reason, you have to use a set-based mentality to see data in whole sets of related tables and not cell-by-cell mindset.出于这个原因,您必须使用基于集合的思维方式来查看整组相关表中的数据,而不是逐个单元格的思维方式。

Specifically, to achieve:具体实现:

putting 1 only on the first appearance of that location, putting 0 on next appearances仅在该位置的第一次出现时放 1,在下一次出现时放 0

Consider a whole set-based approach by joining on a separate, aggregate query to identify the first value of your needed grouping, then calculate needed IIF expression.考虑一种基于整体的方法,通过加入一个单独的聚合查询来识别所需分组的第一个值,然后计算所需的IIF表达式。 Below assumes you have an autonumber or primary key field in table (a standard in relational databases):下面假设您在表中有一个自动编号或主键字段(关系数据库中的标准):

Aggregate Query (save as a separate query, adjust columns as needed)聚合查询(保存为单独的查询,根据需要调整列)

SELECT ColumnD, MIN(AutoNumberID) As MinID
FROM myTable
GROUP BY ColumnD

Final Query (join to original table and build final IIF expression)最终查询(连接到原始表并构建最终IIF表达式)

SELECT m.*, IIF(agg.MinID = AutoNumberID, 1, 0) As Dup_Indicator
FROM myTable m
INNER JOIN myAggregateQuery agg
   ON m.[ColumnD] = agg.ColumnD

To demonstrate with random data:用随机数据进行演示:

Original原来的

| ID | GROUP  | INT | NUM          | CHAR | BOOL  | DATE       |
|----|--------|-----|--------------|------|-------|------------|
| 1  | r      | 9   | 1.424490258  | B6z  | TRUE  | 7/4/1994   |
| 2  | stata  | 10  | 2.591235683  | h7J  | FALSE | 10/5/1971  |
| 3  | spss   | 6   | 0.560461966  | Hrn  | TRUE  | 11/27/1990 |
| 4  | stata  | 10  | -1.499272175 | eXL  | FALSE | 4/17/2010  |
| 5  | stata  | 15  | 1.470269177  | Vas  | TRUE  | 6/13/2010  |
| 6  | r      | 14  | -0.072238898 | puP  | TRUE  | 4/1/1994   |
| 7  | julia  | 2   | -1.370405263 | S2l  | FALSE | 12/11/1999 |
| 8  | spss   | 6   | -0.153684675 | mAw  | FALSE | 7/28/1977  |
| 9  | spss   | 10  | -0.861482674 | cxC  | FALSE | 7/17/1994  |
| 10 | spss   | 2   | -0.817222582 | GRn  | FALSE | 10/19/2012 |
| 11 | stata  | 2   | 0.949287754  | xgc  | TRUE  | 1/18/2003  |
| 12 | stata  | 5   | -1.580841322 | Y1D  | TRUE  | 6/3/2011   |
| 13 | r      | 14  | -1.671303816 | JCP  | FALSE | 5/15/1981  |
| 14 | r      | 7   | 0.904181025  | Rct  | TRUE  | 7/24/1977  |
| 15 | stata  | 10  | -1.198211174 | qJY  | FALSE | 5/6/1982   |
| 16 | julia  | 10  | -0.265808162 | 10s  | FALSE | 3/18/1975  |
| 17 | r      | 13  | -0.264955027 | 8Md  | TRUE  | 6/11/1974  |
| 18 | r      | 4   | 0.518302149  | 4KW  | FALSE | 9/12/1980  |
| 19 | r      | 5   | -0.053620183 | 8An  | FALSE | 4/17/2004  |
| 20 | r      | 14  | -0.359197116 | F8Q  | TRUE  | 6/14/2005  |
| 21 | spss   | 11  | -2.211875193 | AgS  | TRUE  | 4/11/1973  |
| 22 | stata  | 4   | -1.718749471 | Zqr  | FALSE | 2/20/1999  |
| 23 | python | 10  | 1.207878576  | tcC  | FALSE | 4/18/2008  |
| 24 | stata  | 11  | 0.548902226  | PFJ  | TRUE  | 9/20/1994  |
| 25 | stata  | 6   | 1.479125922  | 7a7  | FALSE | 3/2/1989   |
| 26 | python | 10  | -0.437245299 | r32  | TRUE  | 6/7/1997   |
| 27 | sas    | 14  | 0.404746106  | 6NJ  | TRUE  | 9/23/2013  |
| 28 | stata  | 8   | 2.206741458  | Ive  | TRUE  | 5/26/2008  |
| 29 | spss   | 12  | -0.470694096 | dPS  | TRUE  | 5/4/1983   |
| 30 | sas    | 15  | -0.57169507  | yle  | TRUE  | 6/20/1979  |

SQL (uses aggregate in subquery but can be a stored query) SQL (在子查询中使用聚合但可以是存储查询)

SELECT r.*, IIF(sub.MinID = r.ID,1, 0) AS Dup
FROM Random_Data r
LEFT JOIN 
   (
      SELECT r.GROUP, MIN(r.ID) As MinID
      FROM Random_Data r
      GROUP BY r.Group
   )  sub

ON r.[Group] = sub.[GROUP] 

Output (notice the first GROUP value is tagged 1, all else 0) Output (注意第一个GROUP值标记为 1,其他所有标记为 0)

| ID | GROUP  | INT | NUM          | CHAR | BOOL  | DATE       | Dup |
|----|--------|-----|--------------|------|-------|------------|-----|
| 1  | r      | 9   | 1.424490258  | B6z  | TRUE  | 7/4/1994   | 1   |
| 2  | stata  | 10  | 2.591235683  | h7J  | FALSE | 10/5/1971  | 1   |
| 3  | spss   | 6   | 0.560461966  | Hrn  | TRUE  | 11/27/1990 | 1   |
| 4  | stata  | 10  | -1.499272175 | eXL  | FALSE | 4/17/2010  | 0   |
| 5  | stata  | 15  | 1.470269177  | Vas  | TRUE  | 6/13/2010  | 0   |
| 6  | r      | 14  | -0.072238898 | puP  | TRUE  | 4/1/1994   | 0   |
| 7  | julia  | 2   | -1.370405263 | S2l  | FALSE | 12/11/1999 | 1   |
| 8  | spss   | 6   | -0.153684675 | mAw  | FALSE | 7/28/1977  | 0   |
| 9  | spss   | 10  | -0.861482674 | cxC  | FALSE | 7/17/1994  | 0   |
| 10 | spss   | 2   | -0.817222582 | GRn  | FALSE | 10/19/2012 | 0   |
| 11 | stata  | 2   | 0.949287754  | xgc  | TRUE  | 1/18/2003  | 0   |
| 12 | stata  | 5   | -1.580841322 | Y1D  | TRUE  | 6/3/2011   | 0   |
| 13 | r      | 14  | -1.671303816 | JCP  | FALSE | 5/15/1981  | 0   |
| 14 | r      | 7   | 0.904181025  | Rct  | TRUE  | 7/24/1977  | 0   |
| 15 | stata  | 10  | -1.198211174 | qJY  | FALSE | 5/6/1982   | 0   |
| 16 | julia  | 10  | -0.265808162 | 10s  | FALSE | 3/18/1975  | 0   |
| 17 | r      | 13  | -0.264955027 | 8Md  | TRUE  | 6/11/1974  | 0   |
| 18 | r      | 4   | 0.518302149  | 4KW  | FALSE | 9/12/1980  | 0   |
| 19 | r      | 5   | -0.053620183 | 8An  | FALSE | 4/17/2004  | 0   |
| 20 | r      | 14  | -0.359197116 | F8Q  | TRUE  | 6/14/2005  | 0   |
| 21 | spss   | 11  | -2.211875193 | AgS  | TRUE  | 4/11/1973  | 0   |
| 22 | stata  | 4   | -1.718749471 | Zqr  | FALSE | 2/20/1999  | 0   |
| 23 | python | 10  | 1.207878576  | tcC  | FALSE | 4/18/2008  | 1   |
| 24 | stata  | 11  | 0.548902226  | PFJ  | TRUE  | 9/20/1994  | 0   |
| 25 | stata  | 6   | 1.479125922  | 7a7  | FALSE | 3/2/1989   | 0   |
| 26 | python | 10  | -0.437245299 | r32  | TRUE  | 6/7/1997   | 0   |
| 27 | sas    | 14  | 0.404746106  | 6NJ  | TRUE  | 9/23/2013  | 1   |
| 28 | stata  | 8   | 2.206741458  | Ive  | TRUE  | 5/26/2008  | 0   |
| 29 | spss   | 12  | -0.470694096 | dPS  | TRUE  | 5/4/1983   | 0   |
| 30 | sas    | 15  | -0.57169507  | yle  | TRUE  | 6/20/1979  | 0   |

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

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