简体   繁体   English

MS Access SQL如果行(n)=行(n-1)

[英]MS Access SQL If Row(n) = Row(n-1)

I am developing an access DB where in one of the table inputs, duplicates regularly appear in (what I would eventually like to be) the primary key due to the use of some old software for reasons as can be seen below: 我正在开发一个访问数据库,在其中一个表输入中,由于使用了一些旧软件,由于以下原因,重复项经常出现在主键中(我最终希望成为主键):

ID |   Fluid      |   Designer   |   Team        |
--------------------------------------------------
A  |   Water      |   John       |   Piping      |
A  |   Water      |   James      |   Piping      |
B  |   Steam      |   Sam        |   Piping      |
B  |   Steam      |   Sam        |   Modeling    |
C  |   Hydrogen   |   Joe        |   Piping      |
D  |   Steam      |   Joe        |   Piping      |

A duplicates query with reference to the ID field yields: 参考ID字段的重复查询产生:

ID |   Fluid      |   Designer   |   Team        |
--------------------------------------------------
A  |   Water      |   John       |   Piping      |
A  |   Water      |   James      |   Piping      |
B  |   Steam      |   Sam        |   Piping      |
B  |   Steam      |   Sam        |   Modeling    |

Deleting these duplicates automatically is not desired as it is important to know, say, that the software model changed designer or was transferred to another team. 不需要自动删除这些重复项,因为重要的是要知道,例如,软件模型已更改设计者或已转移给另一个团队。 As such, these are manually reviewed. 因此,这些都是手动检查的。

However, this table can contain as much as 30 fields and finding a single non-duplicate can be quite an eyesore. 但是,此表最多可以包含30个字段,查找单个非重复字段可能会很麻烦。 As such, what my desired output would be: 这样,我想要的输出将是:

ID |   Fluid      |   Designer   |   Team        |
--------------------------------------------------
A  |              |   John       |               |
A  |              |   James      |               |
B  |              |              |   Piping      |
B  |              |              |   Modeling    |

I have very little experience with SQL but have spent some time using visual basic so my guess would be some criteria like: 我对SQL的经验很少,但是花了一些时间使用Visual Basic,所以我的猜测可能是这样的标准:

if(record(n)=record(n-1), display("")

However from my basic understanding of access and SQL, I know that relating visual basic is by far, an apples to oranges comparison. 但是,从我对访问和SQL的基本理解中,我知道到目前为止,将可视化基础与苹果与橘子进行比较。

I'm running MS Access 2007-2010. 我正在运行MS Access 2007-2010。


Edit: Looking into normalization now (apologies, I'm a beginner). 编辑:现在研究规范化(抱歉,我是一个初学者)。 More specifics though on my DB issue. 虽然在我的数据库问题上有更多细节。 Also updated the final table above. 还更新了上面的决赛桌。

How I believe my DB should work: 我认为我的数据库应该如何工作:

  1. Import data from software (daily). 从软件导入数据(每天)。 This yields table 1. 这产生表1。
  2. Use a duplicate query to find any duplicates in the ID column. 使用重复查询在ID列中查找所有重复项。 This is table 2. 这是表2。
  3. Use a query to generate a table which will display the duplicated IDs along with the respective column that is not duplicated. 使用查询生成一个表,该表将显示重复的ID以及重复的相应列。

The errors in the software derive from the fact that it amends new data onto preexisting data but only removes exact duplicates. 该软件中的错误源于以下事实:该软件将新数据修改为先前存在的数据,但仅删除精确的重复项。 If say, a different user works on the model, then instead of updating the current ID, it adds a new row with all details the same except a different user. 如果说,不同的用户在模型上工作,那么除了更新当前的ID外,它会添加一个新行,除了其他用户外,所有其他细节都相同。

Conditional Formatting 条件格式

It sounds like what you are looking for is an easy way to visually spot the (few) differences in a long row of columns in similar records. 听起来您正在寻找的是一种简便的方法,可以直观地发现相似记录中较长列中的(少量)差异。 I had a very similar project that I did a few years back that produced a report to view changes a user made to their profile. 几年前,我有一个非常类似的项目,该项目产生了一个报告,以查看用户对其个人资料所做的更改。 The way I approached it was to use Conditional Formatting to highlight the fields that were different. 我采用的方法是使用条件格式来突出显示不同的字段。

In your comparison report, I would also group by the ID column with a group header to make an easy visual separation between the groups. 在您的比较报告中,我还将ID列与组标题分组 ,以便在组之间轻松实现视觉分隔。

For the comparison formula in your report, you can compare the Min() and Max() values for the field in the group. 对于报表中的比较公式,您可以比较组中字段的Min()Max()值。 If the Min and Max are the same, the fields match across the group. 如果最小值和最大值相同,则字段在整个组中匹配。 If not, you can highlight the field using conditional formatting. 如果不是,则可以使用条件格式突出显示该字段。

在此处输入图片说明

I suppose if you wanted to get really fancy, you could expand this expression to use a running sum to only highlight the change in the second record. 我想如果您真的想花哨的话,可以扩展此表达式以使用运行总和以仅突出显示第二条记录中的更改。

Hiding Duplicate Values 隐藏重复值

Another approach would be to hide the duplicate values for subsequent records in the group. 另一种方法是为组中的后续记录隐藏重复的值 This would involve the same grouping concept, but in the report you would set the Hide Duplicates property on the fields to True . 这将涉及相同的分组概念,但是在报告中,您需要将字段上的Hide Duplicates属性设置为True Taking this approach, you would see a complete record at the top of the list, but then the fields of subsequent records would only display when the value changed. 采用这种方法,您会在列表顶部看到完整的记录,但是随后的记录的字段仅在值更改时才会显示。

在此处输入图片说明

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

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