简体   繁体   English

ms access中的多个查询数据

[英]Multiple query data in ms access

I have a table in a accdb that consists of several columns.我在由几列组成的 accdb 中有一个表。 They include a social security number, several dates and monetary values.它们包括一个社会安全号码、几个日期和货币价值。 I am trying to query data in here ( there are over 600000 results in the accdb ) .我想在这里查询数据(accdb 中有超过 600000 个结果)。

Social security number can appear once or several times in a database.社会安全号码可以在数据库中出现一次或多次。 The dates and the monetary values that are on the same line ( in a different column ) can be different, or not.同一行(不同列)上的日期和货币值可以不同,也可以不同。

So let's say my table looks like this:因此,假设我的表如下所示:

Ssn      Date1         Date2       moneyvalue   PostDate
123455  12-01-20      03-04-20     5.21        (A datettime value )     

I am trying to do several things:我正在尝试做几件事:

First I want to only select the ssn that appear at least twice in the database (or more).首先,我只想选择在数据库中至少出现两次(或更多)的 ssn。

From those results i want to only get the ones where date1 is equal to date2.从这些结果中,我只想得到 date1 等于 date2 的结果。

From those results i want to get the results where there are different values in moneyvalue per ssn.从这些结果中,我想得到每个 ssn 的货币价值有不同值的结果。 I want to compare the moneyvalue from the ssn to the money value from the first time this ssn appears in the database ( so the one with the oldest datetime in postDate) and post this ssn if they moneyvalue is different.我想将 ssn 中的货币价值与此 ssn 首次出现在数据库中时的货币价值进行比较(因此是 postDate 中日期时间最早的那个),如果它们的货币价值不同,则发布此 ssn。

Is this possible?这可能吗? How would i go on about this?我该怎么做呢? I have to do this from within ms access sql window, i can't export the database to mssql as it is protected.我必须在 ms access sql 窗口中执行此操作,我无法将数据库导出到 mssql,因为它受到保护。

So to sum it up:所以总结一下:

I want to retrieve all ssn that appear twice or more in the database, where date1 is equal to date2, and where the monetary value in record x does not match the monetary value in the ssn with the oldest postDate.我想检索在数据库中出现两次或更多次的所有 ssn,其中 date1 等于 date2,并且记录 x 中的货币值与具有最旧 postDate 的 ssn 中的货币值不匹配。

Your question suggests aggegation and multiple having clauses:您的问题建议聚合和多个having条款:

select ssn
from mytable
group by ssn
having 
    count(*) > 1
    and sum(iif(date1 = date2, 1, 0)) > 1
    and count(distinct moneyvalue) > 1

Another interpretation is a where clause on condition date1 = date2 :另一种解释是条件date1 = date2上的where子句:

select ssn
from mytable
where date1 = date2
group by ssn
having 
    count(*) > 1
    and count(distinct moneyvalue) > 1

However both queries are not equivalent, and my understanding is that the first one is what you asked for.然而,这两个查询并不等效,我的理解是第一个是您所要求的。

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

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